Point to insert_unique in code comment for IdOrdMap::from_iter_unique#300
Merged
Merged
Conversation
sunshowers
added a commit
that referenced
this pull request
Jul 19, 2026
Hello, again! 👋 While I was reading about `IdOrdMap::from_iter_unique` and writing up #300, it occurred to me that the method might be doing extra work. Specifically, it _seems_ like we're checking for duplicates twice: - the first check happens in `map.entry(value.key())` and that tells us whether this is a duplicate. - the second check happens in the `Entry::Vacant` branch (i.e. after we already determined that this is not a duplicate) when `entry.insert_ref(value)` is called. `insert_ref` [calls `insert_unique_impl`](https://github.com/oxidecomputer/iddqd/blob/82bbad02fc3a60283fd12495334a6ee16d81f248/crates/iddqd/src/id_ord_map/entry.rs#L162-L170) which [also checks for duplicates](https://github.com/oxidecomputer/iddqd/blob/82bbad02fc3a60283fd12495334a6ee16d81f248/crates/iddqd/src/id_ord_map/imp.rs#L1470-L1492). ```rust for value in iter { match map.entry(value.key()) { // <---- first check Entry::Occupied(entry) => { // return error for duplicate } Entry::Vacant(entry) => { entry.insert_ref(value); // <---- second check nested inside } } } ``` So, if I understood things correctly, we're checking for duplicates twice for a single unique-key insert, and once should be enough. This PR is an attempt at a minimal change to avoid that extra work: extract the insertion logic out of `insert_unique_impl` so that it can be reused. This new method, `insert_known_unique_impl` (naming suggestions always welcome 🙈), is private and assumes that it doesn't need to check for duplicates itself because that has been done by the callers. This method is what we then call from both `insert_unique_impl` and `from_iter_unique` after those methods determined that the value's key is unique. And that then removes the extra check for duplicates in `from_iter_unique`. Again, I tried to start with a minimal diff, and if the general idea makes sense and feels valuable -- happy to iterate on the implementation based on feedback! --------- Co-authored-by: Rain <rain@oxide.computer>
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.
👋 Hello! I was reading some code using
IdOrdMapand then went on to read some ofIdOrdMap's implementation bits.As I was reading a comment in
from_iter_unique(side-note: many thanks for leaving useful comments like that -- I was wondering about exactly the thing the comment talks about), I was a bit confused by it referring toinsert_overwriteas the "would be nice if we could use this" idea. I think it should be referring toinsert_uniqueinstead. So, I'm doing my open-source duty and offering a tiny PR to update this.Apologies for the noise if I misunderstood things!