pybind / pybind/pybind11

[BUG]: slow compile if very long std::array being used with clang-18

Open
#5,765 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

3.0.0

Problem description

I've found that pybind11 compile time very slow if very long std::array being used

clang-18 spends 90 seconds to finish, clang-20 11 seconds, g++-13 13 seconds.

tried 2.16.3/3.0.0 both compile slow with clang-18. clang ftime-trace shows that most of time spent in codegen

for function

std::array<int, 102400ul> pybind11::detail::vector_to_array_impl<std::array<int, 102400ul>, std::vector<int, std::allocator<int>>, 0ul, 1ul, 2ul, 3ul, 4ul, 5ul, 6ul, 7ul, 8ul, 9ul, 10ul, 11ul, ....

I've created a repo to show this issue https://github.com/comicfans/pybind11_slow_clang/tree/main

clang-18 compile trace (which can be loaded into chrome://tracing)
basic.cpp.json

Image
Reproducible example code
#include <array>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
namespace py = pybind11;

struct SlowType {
  std::array<int, 102400> member{};
};

PYBIND11_MODULE(basic, module) {
  py::class_<SlowType>(module, "SlowType")
      .def(py::init<>())
      .def_readwrite("member", &SlowType::member);
}
Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal basic.cpp reproducer and the vector_to_array_impl instantiation reached through pybind11/stl.h; compare clang-18 with clang-20 and g++-13 while inspecting the supplied ftime trace. Done means the cause of the long std::array compile time is identified and a validated fix or documented limitation is agreed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design, performance
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.