Repository navigation
Conversation
`insert` built its replacement leaf before looking at the tree, so re-inserting a binding that is already present allocated a new leaf and, because that leaf was not pointer-equal to the old one, rebuilt every branch from the root down to it. The tree it produced was equal to the one it started from but shared nothing with it, which also cost the `Rc::ptr_eq` fast paths on every later operation against the original. Build the leaf only when it is actually needed: if the key is already bound to an equal value, hand the existing leaf back so that the checks added to `update_node_by_key` recognise the subtree as unchanged and share it. This is what the C++ implementation does in `update_leaf_internal` (PatriciaTreeCore.h). Comparing against the stored value requires `V: Eq`. Unlike the previous change this does reach the public API: `PatriciaTreeMap::upsert` and the `FromIterator` impl for `PatriciaTreeMap` now carry the bound. `PatriciaTreeMap` already required `V: Eq` for its own `PartialEq`, and `PatriciaTreeSet` is unaffected (its value type is `()`), so in practice nothing that compiled before stops compiling. C++ has the same requirement through `Value::equals` in its `Value` contract. One behavioural note: when the values compare equal, the previously stored value is now kept rather than replaced by the newly supplied one. For a correct `Eq` the two are interchangeable. The same is true of the C++ implementation. Measured with a counting global allocator, re-inserting an existing key into a 10,000 element `PatriciaTreeSet<u32>` goes from 14 allocations to 0. Together with the previous change, all of remove-absent-key, insert-existing-key, intersect-with-superset and union-with-subset now allocate nothing. Test plan: `cargo test` — 56 tests pass, including a new one covering both that re-inserting an equal binding keeps the root pointer and that inserting a different value still takes effect without disturbing trees sharing the old one. `cargo clippy --all-targets` emits the same warnings as the parent branch, and `cargo fmt --check` reports no diff for either file. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KRRfrza7QDBPhTT3yAS8NV
|
@arthaud has imported this pull request. If you are a Meta employee, you can view this in D119474283. |
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.
insertbuilt its replacement leaf before looking at the tree, so re-inserting a binding that is already present allocated a new leaf and, because that leaf was not pointer-equal to the old one, rebuilt every branch from the root down to it. The tree it produced was equal to the one it started from but shared nothing with it, which also cost theRc::ptr_eqfast paths on every later operation against the original.Build the leaf only when it is actually needed: if the key is already bound to an equal value, hand the existing leaf back so that the checks added to
update_node_by_keyrecognise the subtree as unchanged and share it. This is what the C++ implementation does inupdate_leaf_internal(PatriciaTreeCore.h).Comparing against the stored value requires
V: Eq. Unlike the previous change this does reach the public API:PatriciaTreeMap::upsertand theFromIteratorimpl forPatriciaTreeMapnow carry the bound.PatriciaTreeMapalready requiredV: Eqfor its ownPartialEq, andPatriciaTreeSetis unaffected (its value type is()), so in practice nothing that compiled before stops compiling. C++ has the same requirement throughValue::equalsin itsValuecontract.One behavioural note: when the values compare equal, the previously stored value is now kept rather than replaced by the newly supplied one. For a correct
Eqthe two are interchangeable. The same is true of the C++ implementation.Measured with a counting global allocator, re-inserting an existing key into a 10,000 element
PatriciaTreeSet<u32>goes from 14 allocations to 0. Together with the previous change, all of remove-absent-key, insert-existing-key, intersect-with-superset and union-with-subset now allocate nothing.Test plan:
cargo test— 56 tests pass, including a new one covering both that re-inserting an equal binding keeps the root pointer and that inserting a different value still takes effect without disturbing trees sharing the old one.cargo clippy --all-targetsemits the same warnings as the parent branch, andcargo fmt --checkreports no diff for either file.