timely-util: avoid rebuilding well-formed bucket chains in restore#36897
Merged
Conversation
BucketChain::restore allocated a new BTreeMap and moved every bucket on each call, even when the chain was already well-formed. Callers invoke restore after every peel, so this cost ~19% of an MV sink correction buffer prototype's read path. Scan for well-formedness first and return early. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
petrosagg
approved these changes
Jun 4, 2026
maheshwarip
pushed a commit
that referenced
this pull request
Jun 5, 2026
…36897) ### Motivation `BucketChain::restore` allocates a fresh `BTreeMap` and moves every bucket over on each call, even when the chain is already well-formed — which is the common case, since callers invoke `restore` after every `peel`. In a correction buffer prototype this churn accounted for ~19% of the read-path samples; the production temporal bucketing operators pay the same cost on every frontier advance. ### Description Scan the buckets for well-formedness (each within two bits of its predecessor, starting from an imaginary -2) and return early without touching the map. The scan is O(buckets) without allocation; the slow path is unchanged. ### Verification Existing `temporal` unit tests pass. Measured ~20% improvement on bucketed correction-buffer benchmarks whose read path peels and restores per timestamp. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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.
Motivation
BucketChain::restoreallocates a freshBTreeMapand moves every bucket over on each call, even when the chain is already well-formed — which is the common case, since callers invokerestoreafter everypeel.In a correction buffer prototype this churn accounted for ~19% of the read-path samples; the production temporal bucketing operators pay the same cost on every frontier advance.
Description
Scan the buckets for well-formedness (each within two bits of its predecessor, starting from an imaginary -2) and return early without touching the map.
The scan is O(buckets) without allocation; the slow path is unchanged.
Verification
Existing
temporalunit tests pass.Measured ~20% improvement on bucketed correction-buffer benchmarks whose read path peels and restores per timestamp.
🤖 Generated with Claude Code