Skip to content

Reuse the existing leaf when inserting an equal binding - #36

Closed
arthaud wants to merge 1 commit into
meta:mainfrom
arthaud:insert-leaf-reuse
Closed

arthaud wants to merge 1 commit into
meta:mainfrom
arthaud:insert-leaf-reuse

Conversation

@arthaud

@arthaud arthaud commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

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.

`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
@meta-cla meta-cla Bot added the CLA Signed Do not delete this pull request or issue due to inactivity. label Sep 10, 2026
@meta-codesync

meta-codesync Bot commented Sep 10, 2026

Copy link
Copy Markdown

@arthaud has imported this pull request. If you are a Meta employee, you can view this in D119474283.

@arthaud arthaud closed this Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Do not delete this pull request or issue due to inactivity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant