Skip to content

Preserve structural sharing in update, intersect and leaf combine - #35

Open
arthaud wants to merge 1 commit into
facebook:mainfrom
arthaud:preserve-structural-sharing
Open

Preserve structural sharing in update, intersect and leaf combine#35
arthaud wants to merge 1 commit into
facebook:mainfrom
arthaud:preserve-structural-sharing

Conversation

@arthaud

@arthaud arthaud commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

A Patricia tree is immutable and shared through Rc, so an operation whose result equals its input should return the input itself rather than rebuild it. merge_trees already did this; three other places did not, and because they allocated fresh nodes unconditionally they also defeated the Rc::ptr_eq fast paths that merge_trees, is_tree_subset_of and is_tree_leq rely on for sublinear behaviour on later operations. That matters here: fixpoint iteration merges near-identical environments over and over, and every lost identity makes the next round more expensive.

This mirrors what the C++ implementation does in PatriciaTreeCore.h.

  • update_node_by_key now returns the existing branch when the rebuilt child is pointer-equal to the old one, rather than rebuilding the whole root-to-leaf path. The comment asking for exactly this has been there since the file was written; C++ does it in update_leaf_by_key.

  • intersect_trees now returns s when both subtrees come back unchanged, matching intersect_trees in C++.

  • The leaf combiner keeps the existing leaf when the combined value equals the value already stored, instead of always allocating a new one. This is the C++ update_leaf_internal behaviour, and it is what makes the two checks above fire at all: for a set every value is (), so previously every shared leaf was reallocated during a union or intersection and no subtree ever looked unchanged.

Comparing a value requires V: Eq, which is now a bound on get_leaf_combine_with_value_op_semantics, union_with and intersect_with. All three are pub(crate) and every caller already satisfies it, so no public signature changes. C++ has the same requirement, via Value::equals in its Value contract.

Measured with a counting global allocator on a 10,000 element PatriciaTreeSet<u32>, for operations whose result equals their input:

remove(absent key)          14 -> 0 allocations
intersect_with(superset) 19999 -> 0 allocations
union_with(subset)         927 -> 0 allocations

insert of an already present key still costs one rebuilt path; it allocates its leaf before consulting the tree, and fixing it means adding V: Eq to PatriciaTreeMap::upsert, which is a public signature. Left for a separate change.

Test plan: cargo test — 55 tests pass, including two new ones covering that no-op operations keep the root pointer and that a combine which does change a value still updates it. cargo clippy --all-targets emits the same set of warnings as main; cargo fmt --check reports no diff for this file.

A Patricia tree is immutable and shared through `Rc`, so an operation
whose result equals its input should return the input itself rather than
rebuild it. `merge_trees` already did this; three other places did not,
and because they allocated fresh nodes unconditionally they also defeated
the `Rc::ptr_eq` fast paths that `merge_trees`, `is_tree_subset_of` and
`is_tree_leq` rely on for sublinear behaviour on later operations. That
matters here: fixpoint iteration merges near-identical environments over
and over, and every lost identity makes the next round more expensive.

This mirrors what the C++ implementation does in PatriciaTreeCore.h.

- `update_node_by_key` now returns the existing branch when the rebuilt
  child is pointer-equal to the old one, rather than rebuilding the whole
  root-to-leaf path. The comment asking for exactly this has been there
  since the file was written; C++ does it in `update_leaf_by_key`.

- `intersect_trees` now returns `s` when both subtrees come back
  unchanged, matching `intersect_trees` in C++.

- The leaf combiner keeps the existing leaf when the combined value
  equals the value already stored, instead of always allocating a new
  one. This is the C++ `update_leaf_internal` behaviour, and it is what
  makes the two checks above fire at all: for a set every value is `()`,
  so previously *every* shared leaf was reallocated during a union or
  intersection and no subtree ever looked unchanged.

Comparing a value requires `V: Eq`, which is now a bound on
`get_leaf_combine_with_value_op_semantics`, `union_with` and
`intersect_with`. All three are `pub(crate)` and every caller already
satisfies it, so no public signature changes. C++ has the same
requirement, via `Value::equals` in its `Value` contract.

Measured with a counting global allocator on a 10,000 element
`PatriciaTreeSet<u32>`, for operations whose result equals their input:

    remove(absent key)          14 -> 0 allocations
    intersect_with(superset) 19999 -> 0 allocations
    union_with(subset)         927 -> 0 allocations

`insert` of an already present key still costs one rebuilt path; it
allocates its leaf before consulting the tree, and fixing it means adding
`V: Eq` to `PatriciaTreeMap::upsert`, which is a public signature. Left
for a separate change.

Test plan: `cargo test` — 55 tests pass, including two new ones covering
that no-op operations keep the root pointer and that a combine which does
change a value still updates it. `cargo clippy --all-targets` emits the
same set of warnings as main; `cargo fmt --check` reports no diff for
this 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 6, 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