Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions include/pybind11/stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ struct array_caster {
private:
std::unique_ptr<ArrayType> 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).

Expand All @@ -426,7 +426,37 @@ struct array_caster {
return true;
}

template <bool R = Resizable, enable_if_t<!R, int> = 0>
// Filling in place avoids the index_sequence expansion in vector_to_array,
// which is very slow to compile for large Size (#5765).
template <bool R = Resizable,
enable_if_t<!R && std::is_default_constructible<Value>::value
&& std::is_move_assignable<Value>::value,
int> = 0>
bool convert_elements(handle seq, bool convert) {
auto l = reinterpret_borrow<sequence>(seq);
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<ArrayType> result(new ArrayType());
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)[i] = cast_op<Value &&>(std::move(conv));
}
value = std::move(result);
return true;
}

template <bool R = Resizable,
enable_if_t<!R
&& !(std::is_default_constructible<Value>::value
&& std::is_move_assignable<Value>::value),
int> = 0>
bool convert_elements(handle seq, bool convert) {
auto l = reinterpret_borrow<sequence>(seq);
const size_t size = l.size();
Expand All @@ -435,9 +465,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<Value> temp;
temp.reserve(size);
for (size_t i = 0; i < size; i++) {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <pybind11/typing.h>

#include <array>
#include <numeric>
#include <string>
#include <vector>

Expand Down Expand Up @@ -620,6 +622,19 @@ TEST_SUBMODULE(stl, m) {
py::return_value_policy::take_ownership);

m.def("array_cast_sequence", [](std::array<int, 3> x) { return x; });
// #5765: large arrays used to be very slow to compile
m.def("sum_large_array", [](const std::array<int, 102400> &a) {
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_<ExplicitDefault>(m, "ExplicitDefault").def(py::init<int>());
m.def("sum_explicit_default_array",
[](const std::array<ExplicitDefault, 2> &a) { return a[0].value + a[1].value; });

/// test_issue_1561
struct Issue1561Inner {
Expand Down
13 changes: 13 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,19 @@ 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"])


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."""

Expand Down