perf: Provide ThinMap, a map that uses ThinVec when the length is small - #23228
Open
ChayimFriedman2 wants to merge 1 commit into
Open
perf: Provide ThinMap, a map that uses ThinVec when the length is small#23228ChayimFriedman2 wants to merge 1 commit into
ThinMap, a map that uses ThinVec when the length is small#23228ChayimFriedman2 wants to merge 1 commit into
Conversation
ChayimFriedman2
force-pushed
the
shrink-def-map
branch
from
August 25, 2026 05:33
9fed540 to
2f73e51
Compare
This comment has been minimized.
This comment has been minimized.
It saves memory by being only one machine word inline and compact (no extra capacity) outline, while also being even faster if the length is commonly small because linear search is faster for small lengths. When measuring `analysis-stats`, this saves 40mb on self, 77mb on buck2 and 142mb on omicron. It's also *faster*: 3ginstructions saved on self and buck2, and 30ginstructions on omicron (walltime is too noisy). The memory effect is also likely to appear in IDE scenarios almost as well, because a major part is def maps, and I speculate that unnamed consts also play a role (because the amount of almost-empty expression stores seem too high for real bodies). Not all maps were changed to use this, only those that measurements show are commonly small. The threshold was at least ~95% of the maps of this kind fitting into the thin schema (that is, have at most 10 elements). I also switched some `IndexMap`s to `HashMap`s where I believe the order does not matter.
ChayimFriedman2
force-pushed
the
shrink-def-map
branch
from
August 26, 2026 08:17
2f73e51 to
4a439c7
Compare
Collaborator
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It saves memory by being only one machine word inline and compact (no extra capacity) outline, while also being even faster if the length is commonly small because linear search is faster for small lengths.
When measuring
analysis-stats, this saves 40mb on self, 77mb on buck2 and 142mb on omicron. It's also faster: 3ginstructions saved on self and buck2, and 30ginstructions on omicron (walltime is too noisy). The memory effect is also likely to appear in IDE scenarios almost as well, because a major part is def maps, and I speculate that unnamed consts also play a role (because the amount of almost-empty expression stores seem too high for real bodies).Not all maps were changed to use this, only those that measurements show are commonly small. The threshold was at least ~95% of the maps of this kind fitting into the thin schema (that is, have at most 10 elements).
I also switched some
IndexMaps toHashMaps where I believe the order does not matter.I made
ThinMapgeneric over the map type to support bothHashMapandIndexMap. However at the end the number ofIndexMaps was very small. I still can support both, but I wonder if I should or just make it non-generic and simplify the code. Currently I left it neither supportingIndexMaps nor removing the trait; will appreciate opinions.ThinMap(andThinSet) could very well be in their own crate, I just found no suitable crate on crates.io (also, the API isn't polished enough for an external crate).