Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: call reserve method in set and map casters #4194

Merged
merged 4 commits into from
Sep 25, 2022

Conversation

Skylion007
Copy link
Collaborator

@Skylion007 Skylion007 commented Sep 21, 2022

Description

  • Our STL caster was smart enough to reserve space in the list_caster. However, std::unordered_map and std::unordered_set also have the ability to reserve space ahead of time. This PR updates our other casters to make use of this. Reserving the size of the container is even more important for these types than list_caster since resizes will also call all elements to be rehashed. I am rather surprised it took so long for someone to notice this issue.
  • I wanted to separate out the messy enable_if_t condition into a has_reserve_method alias, but kept getting syntax errors.
  • Tested by adding static_assert false to make sure the proper reserve_method overload is called when appropriate.

Suggested changelog entry:

* Reserve space in set and STL map casters if possible. This will prevent unnecessary rehashing / resizing by knowing the number of keys ahead of time for Python to C++ casting. This improvement will greatly speed up the casting of large unordered maps and sets.

@Skylion007 Skylion007 changed the title perf: call reserve method in set and map casters. perf: call reserve method in set and map casters Sep 21, 2022
include/pybind11/stl.h Outdated Show resolved Hide resolved
Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, I really like this version!

@Skylion007 Skylion007 merged commit 5aa0fad into pybind:master Sep 25, 2022
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Sep 25, 2022
@henryiii henryiii removed the needs changelog Possibly needs a changelog entry label Oct 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants