Repository navigation
[PR] perf: avoid repeated fold closure scans within a batch - #6747
Merged
ranxianglei merged 2 commits intoNov 15, 2026
Merged
ranxianglei merged 2 commits into
ranxianglei merged 2 commits into
Conversation
Louie0806
pushed a commit
to Louie0806/billion-context
that referenced
this pull request
Nov 14, 2026
…) into batch8-wave8
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.
Closes #6744
Problem
recordCacheFoldsFromBlocks()callspushFold()for each newly discovered block. Every call scans the complete retained ledger to freeze openkcounters. After the first new record, only the newly appended tail can remain open within that synchronous batch, so the repeated full scans do unnecessary work on long histories and larger block batches.Change
Keep a closure-scan starting index local to the current batch. The first eligible block still visits all old records, including multiple open records in a restored ledger. Each later eligible block begins at the previous new tail. Advance the index only after a successful append.
This preserves closed counters, fractional
requestsAfter, record order and identity, duplicate/high-water filtering, and an open final successful fold if a later block throws. It adds no persisted fields and retains the complete history. Prefix-token calculation and subsequent sample accounting are unchanged.Validation
a8bbb8d23108ba9148d09512927041364625d910: the 10 new tests report 2 bounded-work failures and 8 behavior controls passed.recordCacheSample().npm run typecheckandnpm run buildpass. The benchmark also passes strict TypeScript with the repository's NodeNext/ES2024 settings.git diff --checkpasses.Public-function benchmark
Node 24.22.1 / Linux x64; one warmup per fresh process and seven alternating base/patch pairs. Fixture construction and full ledger/report checksums are outside the timed registration call. The report clock is fixed, with no output fields removed; complete ledger and report hashes match for every paired run.
The small inputs do not establish a consistent speedup and have microsecond-scale measurement variation. This change bounds the closure scan within a batch; the existing prefix-token calculation can still depend on message history.
The committed
scripts/bench-fold-registration.tsreproduces one warmed process. Run it on each revision with the test preloads, alternating revisions for paired measurements:Existing work checked
Afloat16's historical production patches and non-PR issues were checked. The only earlier same-file patch, #6149, changes credential identity. #6256/#6687 concern the separate report minimum argument limit. The actual upstream patches in #1289, #1490, #2231 and #5651 preserve this repeated closure scan, and the open cache-ledger file-patch audit and function-specific searches contain no existing batch-closure optimization.
CI follow-up: deterministic report comparison
The Ubuntu and Windows Node 22 test shards in run 43602693147 exposed a fixture race in the new full-report comparison: the two calls to
buildSessionCacheReport()could cross a millisecond boundary, so onlygeneratedAtdiffered. The test now fixesDate.now()through its ownTestContext; the complete ledger/report deep-equality assertions and production implementation are unchanged.An independent advancing-clock preload deterministically reproduces the previous failure and passes after this fixture correction. The 54 focused tests, official typecheck and build pass on the tested tree. The follow-up is included in current head
3664e6ef06ac3d023e2962d6bfd19986f6f14ed2; its hosted CI result remains separate from these local checks.