Skip to content

Commit

Permalink
chore: optimize dictionary access in strip_padding numpy (#3994)
Browse files Browse the repository at this point in the history
* emplace field descriptors

* reserve sufficient capacity

* remove std::move

* properly iterate through dict

* make handle casting more explicit

* Revert to old dict api
  • Loading branch information
Skylion007 authored Jun 7, 2022
1 parent 918892b commit e2dcd95
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,10 +641,14 @@ class dtype : public object {
pybind11::str name;
object format;
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;
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 name = spec[0].cast<pybind11::str>();
auto spec_fo = spec[1].cast<tuple>();
Expand All @@ -653,8 +657,8 @@ class dtype : public object {
if ((len(name) == 0u) && format.kind() == 'V') {
continue;
}
field_descriptors.push_back(
{(pybind11::str) name, format.strip_padding(format.itemsize()), offset});
field_descriptors.emplace_back(
std::move(name), format.strip_padding(format.itemsize()), std::move(offset));
}

std::sort(field_descriptors.begin(),
Expand Down

0 comments on commit e2dcd95

Please sign in to comment.