Commit e2dcd954 by Aaron Gokaslan Committed by GitHub

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
parent 918892b9
...@@ -641,10 +641,14 @@ private: ...@@ -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: ...@@ -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(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment