Repository navigation
Subtract zipper - #94
Conversation
Split zipper_moving_tests to follow the new ValuesAt trait
|
Writing a fuzzer with materialized subtraction as an oracle reveals a few bugs: use pathmap::{PathMap, zipper::*};
fn check(label: &str, actual: impl std::fmt::Debug + PartialEq<bool>) {
println!("{label} {actual:?} (expected false)");
assert_eq!(actual, false, "{label}");
}
fn main() {
// The trace format records the boolean; the path is intentionally unchanged.
let lhs = PathMap::from_iter([([2], 7u64)]);
let rhs = PathMap::<u64>::new();
let mut zipper = SubtractZipper::new(lhs.read_zipper(), rhs.read_zipper());
zipper.descend_to([2]);
check("zero-k result", zipper.descend_first_k_path(0));
assert_eq!(zipper.path(), [2], "zero-k must not move");
// This exercises seek_k_path exhaustion and refresh-on-failure.
let lhs = PathMap::from_iter([([10, 20], 1u64), ([10, 21], 2)]);
let rhs = PathMap::from_iter([([10, 20], 1u64), ([10, 21], 2)]);
let mut zipper = SubtractZipper::new(lhs.read_zipper(), rhs.read_zipper());
zipper.descend_to([10]);
check("k-path result", zipper.descend_first_k_path(2));
check("k-path cache valid", zipper.path_exists());
check("k-path result", zipper.descend_first_k_path(2));
check("k-path state valid", zipper.path_exists());
// to_next_k_path must refresh after advance_to_next_subtree fails.
let lhs = PathMap::from_iter([([10, 20], 1u64), ([10, 21], 2)]);
let rhs = lhs.clone();
let mut zipper = SubtractZipper::new(lhs.read_zipper(), rhs.read_zipper());
zipper.descend_to([10, 20]);
check("next-k result", zipper.to_next_k_path(2));
check("next-k state valid", zipper.path_exists());
// empty result, so result.read_zipper().path_exists() reported true.
let empty_lhs = PathMap::<u64>::new();
let empty_rhs = PathMap::<u64>::new();
let result = empty_lhs.subtract(&empty_rhs);
let result_zipper = result.read_zipper();
check("empty-map subtraction", result_zipper.path_exists());
} |
|
Thanks @imlvts I will turn these into tests and fix'em |
|
@imlvts The first failure is somewhat interesting: The docs say that it must |
|
@imlvts This passes: |
|
@imlvts This also passes: |
|
That sounds like a doc bug, in this particular case. The spirit of the
api is that true means you're k bytes below where you started and false
means you're where you started. So either result is correct and leads to
the same thing assuming it gates a to_next loop.
But we gotta pick one.
I suppose I'd pick k=0 should be false. But I don't care much so could go
with either.
…On Sat, Sep 12, 2026, 11:25 AM Marcin Rzeźnicki ***@***.***> wrote:
*marcin-rzeznicki* left a comment (Adam-Vandervorst/PathMap#94)
<#94 (comment)>
@imlvts <https://github.com/imlvts> The first failure is somewhat
interesting:
check("zero-k result", zipper.descend_first_k_path(0));
The docs say that it must return true if the zipper successfully
descended k steps (you can argue that descending 0 steps is always a
success - I return true in this case), and then it contradicts itself
saying that returning false means that zipper did not move (it obviously
did not have to move when k=0). @luketpeterson
<https://github.com/luketpeterson> ?
—
Reply to this email directly, view it on GitHub
<#94?email_source=notifications&email_token=AIY2CNNOUXZKNOWCCSE3UKT5OSX3DA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKNRUGI4DIOJTGE32M4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLDGN5XXIZLSL5RWY2LDNM#issuecomment-5642849317>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIY2CNITSJ6R6HI446G35ID5OSX3DAVCNFSNUABFKJSXA33TNF2G64TZHM3TQNZVGUZTKNJTHNEXG43VMU5TKMZXGM4DEMZSGIYKC5QC>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/AIY2CNJ5PDQ74OCKKBIJVLT5OSX3DA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKNRUGI4DIOJTGE32M4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJKTGN5XXIZLSL5UW64Y>
and Android
<https://github.com/notifications/mobile/android/AIY2CNP6F2NJXRGLP3PYGWL5OSX3DA5CNFSNUABFM5UWIORPF5TWS5BNNB2WEL2JONZXKZKDN5WW2ZLOOQXTKNRUGI4DIOJTGE32M4TFMFZW63VHNVSW45DJN5XKKZLWMVXHJLTGN5XXIZLSL5QW4ZDSN5UWI>.
Download it today!
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
|
@imlvts @luketpeterson can we get this decided on and merged? |
|
I'm good with this. seems reasonable. but will need merges. |
|
I am still working on one improvement (survival witness - makes some benchmarks faster) and benchmarks |
Conflicts: - src/utils/mod.rs: keep the branch's u64_4_raw_clear_bit helper in clear_bit, and add master's new toggle_bit. - src/zipper.rs: take master's doc example for val_at. - src/prefix_zipper.rs: take master's side (the ZipperValues test import is unused after master's changes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the child being ascended out of is not part of the subtraction (an off-trie focus, or a path the RHS removed), has_surviving_sibling() counted the parent's one remaining child as a sibling, so the ascent stopped at a non-branch. Such a parent is a branch only with two surviving children; one is enough to prove it survives for the next step. Fixes the six *ascend_until_from_off_trie* shared-suite tests that master added, and adds a test for the subtracted-child case against the materialized subtraction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Bench A/B vs base: donejob log · 2026-10-23 21:55:05 UTC base 70d3dd3 → head 280c63d, 3 round(s), median of each run averaged; negative is faster
15 case(s) moved more than 5%
Full tables per bench are in the job log and the bench-out artifact. |
Summary
This PR adds
SubtractZipper<V, A, B>, a virtual zipper exposing the materialized algebraic subtraction:without eagerly constructing the resulting trie.
The zipper exposes the same observable
Zipperstate as a zipper over a materialized subtraction result:psubtract;path_exists,is_val,child_mask,child_count, andvalreflect the resulting virtual trie.Traversal
SubtractZipperimplementsZipperMovingandZipperIteration.Several movement operations are specialized because the default implementations would repeatedly recompute the virtual subtraction state after every intermediate byte movement.
The optimized implementations move the two backing zippers in sync and refresh the cached virtual state only at externally observable stopping points.
This includes optimized implementations of:
Overlapping subtrees are inspected lazily and short-circuit as soon as the difference is known to be non-empty. Once RHS no longer contains the current path, traversal can treat the remaining subtree as plain LHS.
val_count()is computed lazily and cached.ZipperValuessplitThe PR also separates current-focus value access from arbitrary-path value access.
Previously
ZipperValues<V>required both:This is problematic for computed zippers: a value at the current focus can be cached and returned by reference, while
val_at()may need to synthesize a new value with no stable backing storage.The traits are therefore split into:
This lets computed zippers such as
SubtractZipperremain compatible with generic algorithms that only requireval(), while concrete zippers can continue supporting arbitrary-path borrowed access.