From e26ab4a86d2e5184078f61e8ba3ef1c2052fc549 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 12 Nov 2026 21:11:00 -0500 Subject: [PATCH 1/3] fix: avoid slow compile for large std::array in array_caster Fill fixed-size arrays in place when the element type is default constructible and move assignable, instead of going through an index_sequence expansion of Size elements. A std::array member took 135 s to compile with clang; it now takes 1.5 s. Fixes #5765. Assisted-by: ClaudeCode:claude-opus-5-5 --- include/pybind11/stl.h | 33 +++++++++++++++++++++++++++++---- tests/test_stl.cpp | 6 ++++++ tests/test_stl.py | 8 ++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index b73d0e95b2..14c2aea230 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -426,7 +426,35 @@ struct array_caster { return true; } - template = 0> + // Filling in place avoids the index_sequence expansion in vector_to_array, + // which is very slow to compile for large Size (#5765). + template ::value + && std::is_move_assignable::value, + int> = 0> + bool convert_elements(handle seq, bool convert) { + auto l = reinterpret_borrow(seq); + if (l.size() != Size) { + return false; + } + std::unique_ptr result(new ArrayType{}); + size_t ctr = 0; + for (const auto &it : l) { + value_conv conv; + if (!conv.load(it, convert)) { + return false; + } + (*result)[ctr++] = cast_op(std::move(conv)); + } + value = std::move(result); + return true; + } + + template ::value + && std::is_move_assignable::value), + int> = 0> bool convert_elements(handle seq, bool convert) { auto l = reinterpret_borrow(seq); const size_t size = l.size(); @@ -435,9 +463,6 @@ struct array_caster { } // The `temp` storage is needed to support `Value` types that are not // default-constructible. - // Deliberate choice: no template specializations, for simplicity, and - // because the compile time overhead for the specializations is deemed - // more significant than the runtime overhead for the `temp` storage. std::vector temp; temp.reserve(size); for (size_t i = 0; i < size; i++) { diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index 9306b20670..d4b6d58874 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -18,6 +18,8 @@ #include +#include +#include #include #include @@ -620,6 +622,10 @@ TEST_SUBMODULE(stl, m) { py::return_value_policy::take_ownership); m.def("array_cast_sequence", [](std::array x) { return x; }); + // #5765: large arrays used to be very slow to compile + m.def("sum_large_array", [](const std::array &a) { + return std::accumulate(a.begin(), a.end(), 0LL); + }); /// test_issue_1561 struct Issue1561Inner { diff --git a/tests/test_stl.py b/tests/test_stl.py index 7c1f76daa6..27c1e50e3c 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -585,6 +585,14 @@ def test_array_cast_sequence(): assert m.array_cast_sequence((1, 2, 3)) == [1, 2, 3] +def test_large_array(): + assert m.sum_large_array(range(102400)) == 102399 * 102400 // 2 + with pytest.raises(TypeError): + m.sum_large_array(range(10)) + with pytest.raises(TypeError): + m.sum_large_array([0] * 102399 + ["x"]) + + class SequenceWithChangingLen: """Reports `lens[0]` when first asked for its length, `lens[1]` after that.""" From 6d5aca8cdd08e670ba09b60d76055cbf73f5ff25 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 21 Nov 2026 13:25:20 -0500 Subject: [PATCH 2/3] fix(stl): support explicit default constructors in std::array elements Assisted-by: ClaudeCode:claude-fable-5-2 --- include/pybind11/stl.h | 3 ++- tests/test_stl.cpp | 9 +++++++++ tests/test_stl.py | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 14c2aea230..7a24ff019e 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -437,7 +437,8 @@ struct array_caster { if (l.size() != Size) { return false; } - std::unique_ptr result(new ArrayType{}); + // Not `ArrayType{}`: that does not compile if `Value` has an explicit default constructor. + std::unique_ptr result(new ArrayType()); size_t ctr = 0; for (const auto &it : l) { value_conv conv; diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp index d4b6d58874..db7811d474 100644 --- a/tests/test_stl.cpp +++ b/tests/test_stl.cpp @@ -627,6 +627,15 @@ TEST_SUBMODULE(stl, m) { return std::accumulate(a.begin(), a.end(), 0LL); }); + // An explicit default constructor must not be used in copy-list-initialization + struct ExplicitDefault { + explicit ExplicitDefault(int v = 0) : value(v) {} + int value; + }; + py::class_(m, "ExplicitDefault").def(py::init()); + m.def("sum_explicit_default_array", + [](const std::array &a) { return a[0].value + a[1].value; }); + /// test_issue_1561 struct Issue1561Inner { std::string data; diff --git a/tests/test_stl.py b/tests/test_stl.py index 27c1e50e3c..dbd2603bf0 100644 --- a/tests/test_stl.py +++ b/tests/test_stl.py @@ -593,6 +593,11 @@ def test_large_array(): m.sum_large_array([0] * 102399 + ["x"]) +def test_array_explicit_default_constructor(): + items = [m.ExplicitDefault(1), m.ExplicitDefault(2)] + assert m.sum_explicit_default_array(items) == 3 + + class SequenceWithChangingLen: """Reports `lens[0]` when first asked for its length, `lens[1]` after that.""" From f36aef978176e45bace195e521436a4eb49f9ca8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 21 Nov 2026 13:25:49 -0500 Subject: [PATCH 3/3] fix(stl): index the sequence in the in-place std::array loader Assisted-by: ClaudeCode:claude-fable-5-2 --- include/pybind11/stl.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/include/pybind11/stl.h b/include/pybind11/stl.h index 7a24ff019e..ff2cdfb896 100644 --- a/include/pybind11/stl.h +++ b/include/pybind11/stl.h @@ -401,7 +401,7 @@ struct array_caster { private: std::unique_ptr value; - // Both `convert_elements` overloads index `l` up to a size that is determined only once, + // All `convert_elements` overloads index `l` up to a size that is determined only once, // rather than iterating over `l`: the accesses stay in bounds even if the length of `l` // changes underneath (e.g. if another thread resizes a list in a free-threaded build). @@ -434,18 +434,19 @@ struct array_caster { int> = 0> bool convert_elements(handle seq, bool convert) { auto l = reinterpret_borrow(seq); - if (l.size() != Size) { + const size_t size = l.size(); + if (size != Size) { return false; } // Not `ArrayType{}`: that does not compile if `Value` has an explicit default constructor. std::unique_ptr result(new ArrayType()); - size_t ctr = 0; - for (const auto &it : l) { - value_conv conv; - if (!conv.load(it, convert)) { + for (size_t i = 0; i < size; i++) { + value_conv conv; // Constructed before the item is fetched. + const object item = l[i]; // Keeps the item alive while `conv` is in use. + if (!conv.load(item, convert)) { return false; } - (*result)[ctr++] = cast_op(std::move(conv)); + (*result)[i] = cast_op(std::move(conv)); } value = std::move(result); return true;