mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-16 13:47:53 +00:00
chore: optimize dictionary access in strip_padding numpy (#3994)
* emplace field descriptors * reserve sufficient capacity * remove std::move * properly iterate through dict * make handle casting more explicit * Revert to old dict api
This commit is contained in:
parent
918892b97e
commit
e2dcd95407
@ -641,10 +641,14 @@ private:
|
|||||||
pybind11::str name;
|
pybind11::str name;
|
||||||
object format;
|
object format;
|
||||||
pybind11::int_ offset;
|
pybind11::int_ offset;
|
||||||
|
field_descr(pybind11::str &&name, object &&format, pybind11::int_ &&offset)
|
||||||
|
: name{std::move(name)}, format{std::move(format)}, offset{std::move(offset)} {};
|
||||||
};
|
};
|
||||||
|
auto field_dict = attr("fields").cast<dict>();
|
||||||
std::vector<field_descr> field_descriptors;
|
std::vector<field_descr> field_descriptors;
|
||||||
|
field_descriptors.reserve(field_dict.size());
|
||||||
|
|
||||||
for (auto field : attr("fields").attr("items")()) {
|
for (auto field : field_dict.attr("items")()) {
|
||||||
auto spec = field.cast<tuple>();
|
auto spec = field.cast<tuple>();
|
||||||
auto name = spec[0].cast<pybind11::str>();
|
auto name = spec[0].cast<pybind11::str>();
|
||||||
auto spec_fo = spec[1].cast<tuple>();
|
auto spec_fo = spec[1].cast<tuple>();
|
||||||
@ -653,8 +657,8 @@ private:
|
|||||||
if ((len(name) == 0u) && format.kind() == 'V') {
|
if ((len(name) == 0u) && format.kind() == 'V') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
field_descriptors.push_back(
|
field_descriptors.emplace_back(
|
||||||
{(pybind11::str) name, format.strip_padding(format.itemsize()), offset});
|
std::move(name), format.strip_padding(format.itemsize()), std::move(offset));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::sort(field_descriptors.begin(),
|
std::sort(field_descriptors.begin(),
|
||||||
|
Loading…
Reference in New Issue
Block a user