Repository navigation
Conversation
`Witness::bucket_used_slots` forwarded to `SaltWitness`, which looks up every slot of the bucket in the witness on each call. `EphemeralSaltState` asks for the count on every insertion into an empty slot, and a resize reinserts every key of the bucket, so replaying a block that resizes a bucket on its witness cost capacity x keys lookups: 5.2 s for a 16384-slot bucket and 106 s for a 65536-slot one (release build, Apple M4 Pro). The witnessed slots do not change once the witness is built, and the state adds its own insertions and deletions on top of the store's count, so every call returned the same answer. Count it once when the witness is built, in one pass over the witnessed keys, and serve it from a map: 20 ms and 99 ms for the same two blocks. The answers are those of the scan: a count only for a data bucket whose metadata and every slot below its capacity are witnessed, an error otherwise, and `Ok(0)` for metadata buckets and out-of-range ids. The pass runs on witnesses that are not verified yet, so it never panics on malformed metadata. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Mutation testing - PASSNothing to test: no viable mutants were generated. |
Codecov Report❌ Patch coverage is
🚀 New features to boost your workflow:
|
Performance Benchmark ComparisonCompared Detailed Comparison
|
Mutation testing - PASSNothing to test: no viable mutants were generated (3 unviable, 0 timed out). |
shanchen97
left a comment
There was a problem hiding this comment.
Two notes on the precompute. The cache should stay; the scan it replaces should not come back.
| /// Returns the occupied-slot count of a bucket whose slots are all witnessed. | ||
| /// | ||
| /// Same answers as [`SaltWitness::bucket_used_slots`] in constant time: resizing a bucket | ||
| /// reinserts every key, and each insertion asks for this count. | ||
| fn bucket_used_slots(&self, bucket_id: BucketId) -> Result<u64, Self::Error> { | ||
| self.salt_witness.bucket_used_slots(bucket_id) | ||
| if !is_valid_data_bucket(bucket_id) { | ||
| return Ok(0); | ||
| } | ||
| self.fully_witnessed | ||
| .get(&bucket_id) | ||
| .copied() | ||
| .ok_or(SaltError::NotInWitness { | ||
| what: "Bucket slots", | ||
| }) | ||
| } |
There was a problem hiding this comment.
This is not the scan's answer. SaltWitness::bucket_used_slots returns NotInWitness { what: "Metadata" } when metadata is missing, NotInWitness { what: "Key" } when a slot is missing, InvalidFormat when metadata does not decode, and panics when metadata is witnessed as None. Every failure here is NotInWitness { what: "Bucket slots" }, and the panic becomes Err. The PR body records that difference; this doc says the opposite.
bucket_used_slots_matches_a_scan_of_the_witness compares .ok(), so two different errors pass. It locks the Ok counts, not "same answers". test_witness_bucket_used_slots still says this method delegates to SaltWitness; it no longer does, and that comment is what someone will follow when they delete the cache.
Write the contract this function actually has. A fully witnessed data bucket returns the occupied-slot count. A metadata bucket or an out-of-range id returns Ok(0). Everything else is NotInWitness { what: "Bucket slots" }, including metadata the scan would panic on. Ok matches the scan and the error variant does not, on purpose: shi_upsert only tests Ok, and shi_rehash forwards the error. Leave SaltWitness::bucket_used_slots as the scanning implementation. Its tests lock the "Metadata" and "Key" variants, and this PR should not change them.
fully_witnessed reads as a set and holds a count, and both From and Witness::create fill it by hand. A private constructor and a name that says count (used_slots) puts that derived field in one place.
There was a problem hiding this comment.
Done in d535f9c.
- Contract. The doc on
Witness::bucket_used_slotsnow says what the function returns. A data bucket whose metadata and slots0..capacityare all witnessed gives the occupied-slot count. A metadata bucket or an out-of-range id givesOk(0). Anything else isNotInWitness { what: "Bucket slots" }, including metadata the scan panics on. It states that only theOkresults match the scan and that the errors differ on purpose, with theshi_upsert/shi_rehashreason. - Tests.
bucket_used_slots_matches_a_scan_of_the_witnessstill compares theOkcounts with the scan, and now asserts the exact error for every unknown count instead of.ok().building_a_witness_from_malformed_metadata_does_not_panicasserts the same error for its three malformed-metadata cases. - Stale comments.
test_witness_bucket_used_slotsno longer says the method delegates toSaltWitness. - Field. It is
used_slots, private, and filled only inWitness::new, whichFrom<SaltWitness>andWitness::createboth call.
SaltWitness::bucket_used_slots and its tests are untouched; the PR only changes proof/witness.rs. I also updated the PR body: it said the field was pub(crate) and that this method "gives the scan's answers".
… one place
Review follow-up for the precomputed used-slot count.
- Walk each bucket's run of witnessed keys with one cursor, and write down
why `capacity` keys below the capacity are exactly the slots `0..capacity`.
- Document what `Witness::bucket_used_slots` returns: only its `Ok` results
match the scan in `SaltWitness::bucket_used_slots`; every unknown count is
`NotInWitness { what: "Bucket slots" }`. Tests now pin that error.
- Rename the field to `used_slots`, make it private and fill it in
`Witness::new`, which both `From<SaltWitness>` and `Witness::create` use.
- Fix test comments that still said the method delegates to `SaltWitness`.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Resolve the conflict in salt/src/proof/witness.rs: both sides appended tests at the end of the test module; keep both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- fully_witnessed_usage: look up a bucket's metadata only when its first witnessed slot is slot 0 (a decoded capacity is at least 1, so no other bucket can be fully witnessed), and group each bucket's run with peek + next_if instead of a hand-managed cursor. - Trim the bucket_used_slots doc to the contract. - Tests: fold the malformed-metadata cases into bucket_used_slots_matches_a_scan_of_the_witness and cross-check every case against the scan; drop test_witness_bucket_used_slots, which that test covers; use apply_block, mock_salt_value and the module imports; build the plain-key test witness through Witness::from. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tate EphemeralSaltState asked the store for a bucket's used-slot count on every insertion into an empty slot. A witness answers by looking up every slot of the bucket, and a block that resizes a bucket reinserts all of its keys, so replaying such a block on its witness cost capacity x keys lookups. Keep the store's count of each bucket after the first successful read, in `base_usage_count`. The store does not change while a state borrows it, which `usage_count_delta` already relies on, so the count is fixed for the state's lifetime. A failed read is not kept: the store is asked again and its error reaches the caller unchanged. This replaces the count cache in `Witness`, whose code is main's again. Every `StateReader` now gets the fix: `Witness`, a bare `SaltWitness`, and readers outside this crate. The field is `pub` like the others, because code outside the crate rebuilds the state with struct-update syntax. `test_usage_count` and `test_metadata` wrote to the store under a live state and expected the count to follow. They now read each change to the store through a new state. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`SaltWitness::metadata` hit `unreachable!` when a witness mapped a metadata key to `None`. A witness builder never produces that, and the proof check rejects it, but a witness can be read before it is verified. Return `InvalidFormat` instead, so `metadata` and the used-slot scan that reads it report a malformed witness without panicking. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Summary
Replaying a block on its witness rescanned a whole bucket on every insertion when the block resized that bucket. A stateless validator does this replay to derive the block's state updates.
EphemeralSaltStateasked the store for a bucket's used-slot count on every insertion into an empty slot, and a resize reinserts every key of the bucket. A witness answers by looking up every slot of the bucket, so the replay cost capacity × keys lookups.Every one of those calls returned the same answer. The store does not change while a state borrows it, and the state adds its own insertions and deletions on top of the store's count (
usage_count_delta). This PR reads the store's count once per bucket per state and keeps it.An earlier version of this PR cached the counts in
Witness. The cache now lives inEphemeralSaltState, so everyStateReadergets the fix andWitnessis unchanged from main.What changes
EphemeralSaltStategains apub base_usage_countfield.usage_countasks the store on first use and reuses the answer afterwards.Witness,SaltWitness::bucket_used_slots, the witness bytes, proofs, state updates and state roots are unchanged.SaltWitness::metadatareturnsInvalidFormatfor a metadata slot witnessed as empty, where it hitunreachable!. The proof check rejects such a witness, so this only matters when a witness is read before it is verified.Behaviour change to review
EphemeralSaltStatenow relies on the store not changing while the state borrows it.usage_count_deltaalready needed that: a change applied to the store under a live state would be counted twice. But the count was re-read on every call, and two tests (test_usage_count,test_metadata) wrote to the store under a live state and expected the count to follow. They now read each change to the store through a new state.A caller that writes to the store under a live state and then inserts through that state would get the count from the first read.
Measurements
Release build, Apple M4 Pro. One bucket is filled to its resize threshold (the real 80% load factor,
test-bucket-resizewithNUM_DATA_BUCKETS=1), then a block of 8 inserts resizes it. Timed:EphemeralSaltState::update_finof that block on the block's witness. The PR columns are the best of 10 runs (3 for the two largest rows); themaincolumn is a single run. Every run returns state updates equal to the full store's.WitnessSaltWitnessThe first three
mainnumbers were measured with the same harness in the same session. The 65536 number is from the earlier version of this PR and was not rerun.What remains after the fix is the reinsertion itself, linear in the bucket's keys.
A block that resizes nothing costs what it does on main. No pass over the witnessed keys runs when a
Witnessis built, and an insertion into a partly witnessed bucket still scans up to the first slot that is not witnessed.Testing
All of these pass on this head:
cargo testcargo test --features test-bucket-resizewithNUM_DATA_BUCKETS=2andBUCKET_RESIZE_LOAD_FACTOR_PCT=1cargo test --no-default-features, with and withouttest-bucket-resizecargo check -p salt --target riscv64imac-unknown-none-elf --no-default-featurescargo clippy --all-targets -- -D warningsandcargo fmt --all -- --checkscripts/mutation_test.sh diff origin/main: 3 of 3 viable mutants killed, no timeouts, none unviableNot run locally: the i686 job and the spec-gate packs. The
canonpack targetsproof/salt_witness.rs, where it mutates sort and dedup calls and comparators; the lines changed there have none.New and changed tests:
test_usage_count_reads_the_store_once_per_bucketuses a store that counts itsbucket_used_slotscalls. Insertions that resize a bucket, then canonicalization, read the store's count once. Removing the line that keeps the count fails this test.test_usage_count_does_not_keep_a_failed_readchecks that a store that cannot serve the count is asked again by each insertion, and that a rehash returns the store's own error.resize_block_replayed_on_the_witness_matches_the_store(needstest-bucket-resize) replays a block that resizes a bucket on its witness and gets the full store's state updates and root.test_malformed_metadata_is_an_errorcovers a metadata slot witnessed as empty, undecodable metadata, and a capacity ofu64::MAX, formetadataand for the used-slot scan. It replacestest_metadata_none_entry_panics.test_empty_metadata_slot_fails_the_proof_checkshows that a real witness whose metadata slot is emptied failsverify_proof.test_witness_bucket_used_slotsis restored from main.Consumers
Nothing was compiled or run against either consumer.
EphemeralSaltStateover a verifiedsalt::Witness, so it needs the new salt revision and no code change. ItsLightWitnessis not affected: nothing in stateless-validator writes through it (it serves execution and tracing, which only read), and a read never asks for a bucket's used-slot count.pub, like the struct's other fields, so code outside the crate that builds the state from its fields keeps compiling.🤖 Generated with Claude Code