-
Notifications
You must be signed in to change notification settings - Fork 292
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
insert_unique_unchecked operation #293
Conversation
Sometimes a map is constructed when it is known that all keys are unique (e. e. if keys are coming from another map or from a sorted/deduplicated iterator). In this case we can make insertion faster by skipping a check that a key already exists in the map. `insert_unique_unchecked` is guaranteed to be memory-safe, but does not guarantee anything beyond that: if inserted key is not unique, `HashMap` can panic, loop forever, return incorrect entry etc. Added simple benchmark. `insert_unique_unchecked` is about 30% faster than `insert`. Your mileage may vary of course. Similar PR was [added to `indexmap` crate](indexmap-rs/indexmap#200) and they asked to discuss the name of the operation with `hashbrown` crate owners to come to the same naming convention (if `hashbrown` is willing to have the same operation).
b8e7d40
to
8d6c278
Compare
Could you change this to return an |
Regular What would be the use case for that reference? If a user need to modify an entry, they can do that before the insertion. I can do it of course, just want to understand it. |
... we can't return |
Added a commit on top which changes to return |
b3bdc62
to
7002f9f
Compare
@bors r+ |
📌 Commit 7002f9f has been approved by |
☀️ Test successful - checks-actions |
Sometimes a map is constructed when it is known that all keys are
unique (e. e. if keys are coming from another map or from a
sorted/deduplicated iterator). In this case we can make insertion
faster by skipping a check that a key already exists in the map.
insert_unique_unchecked
is guaranteed to be memory-safe, but doesnot guarantee anything beyond that: if inserted key is not unique,
HashMap
can panic, loop forever, return incorrect entry etc.Added simple benchmark.
insert_unique_unchecked
is about 30%faster than
insert
. Your mileage may vary of course.Similar PR was
added to
indexmap
crateand they asked to discuss the name of the operation with
hashbrown
crate owners to come to the same naming convention (if
hashbrown
is willing to have the same operation).