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:
Aaron Gokaslan 2022-06-06 22:33:28 -04:00 committed by GitHub
parent 918892b97e
commit e2dcd95407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 3 deletions

View File

@ -641,10 +641,14 @@ private:
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>();
@ -653,8 +657,8 @@ private:
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(),