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: use flat_set, flat_map for small, trivially-moved containers #40817

Merged
merged 19 commits into from Jan 5, 2024

Conversation

ckerr
Copy link
Member

@ckerr ckerr commented Dec 22, 2023

Description of Change

base::flat_map and base::flat_set are STL-like map & set containers that are implemented using std::vector instead of an red-black tree, so they have good memory locality and lower overhead, especially for containers with smaller sizes. The upstream usage advice recommends:

Most maps and sets in Chrome are small and contain objects that can be moved efficiently. In this case, consider base::flat_map and base::flat_set.

You need to be aware of the maximum expected size of the container since individual inserts and deletes are O(n), giving O(n^2) construction time for the entire map. But because it avoids mallocs in most cases, inserts are better or comparable to other containers even for several dozen items, and efficiently-moved types are unlikely to have performance problems for most cases until you have hundreds of items. If your container can be constructed in one shot, the constructor from vector gives O(n log n) construction times and it should be strictly better than a std::map.

This PR replaces std::map and std::set with their flat counterparts iff they match those recommendations: they are all smaller containers with trivially-moveable types (e.g. ints and pointers).

This is a general cleanup PR so no specific stateholders, but all reviews welcomed

Checklist

Release Notes

Notes: none

@ckerr ckerr added the semver/patch backwards-compatible bug fixes label Dec 22, 2023
@electron-cation electron-cation bot added the new-pr 🌱 PR opened in the last 24 hours label Dec 22, 2023
@ckerr ckerr changed the title perf: use flat_set, flat_map for small, trivially-moved keys/vals perf: use flat_set, flat_map for small, trivially-moved containers Dec 22, 2023
@ckerr ckerr force-pushed the refactor/use-base-small-map branch from d154538 to 24340d4 Compare December 22, 2023 23:07
lukins-cz

This comment was marked as off-topic.

@ckerr ckerr marked this pull request as draft December 23, 2023 01:27
@ckerr ckerr force-pushed the refactor/use-base-small-map branch 2 times, most recently from af26c52 to c29afe5 Compare December 23, 2023 15:21
@electron-cation electron-cation bot removed the new-pr 🌱 PR opened in the last 24 hours label Dec 23, 2023
@ckerr ckerr marked this pull request as ready for review January 4, 2024 15:50
Copy link
Contributor

@jkleinsc jkleinsc left a comment

Choose a reason for hiding this comment

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

LGTM

@codebytere codebytere merged commit 22970f5 into main Jan 5, 2024
17 checks passed
@codebytere codebytere deleted the refactor/use-base-small-map branch January 5, 2024 11:18
Copy link

release-clerk bot commented Jan 5, 2024

No Release Notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-backport semver/patch backwards-compatible bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants