Skip to content

Point to insert_unique in code comment for IdOrdMap::from_iter_unique#300

Merged
sunshowers merged 2 commits into
oxidecomputer:mainfrom
izuzak:izuzak/fix-code-comment
Jul 17, 2026
Merged

Point to insert_unique in code comment for IdOrdMap::from_iter_unique#300
sunshowers merged 2 commits into
oxidecomputer:mainfrom
izuzak:izuzak/fix-code-comment

Conversation

@izuzak

@izuzak izuzak commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

👋 Hello! I was reading some code using IdOrdMap and then went on to read some of IdOrdMap'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 to insert_overwrite as the "would be nice if we could use this" idea. I think it should be referring to insert_unique instead. So, I'm doing my open-source duty and offering a tiny PR to update this.

Apologies for the noise if I misunderstood things!

@sunshowers sunshowers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@sunshowers
sunshowers merged commit 8410936 into oxidecomputer:main Jul 17, 2026
35 checks passed
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>
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.

2 participants