perf(region): memoize rebuildBlockLedger per snapshot to cut O(B^2*N) rebuilds (#109) - #123
Open
ranxianglei wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
我来协助看一下这个问题,请分析并回复处理结果。 |
This was referenced Sep 6, 2026
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.
What
Fixes #109 —
rebuildBlockLedgerwas being recomputed many times inside hot loops instead of once per tool call.Root cause
rebuildBlockLedgerscans the full durable log and callssummarySeqOfCompaction(itself O(N)) once per compaction block → O(B·N) per call. Several helpers wrap it (blockRegistry,expandShadowedSeqs,blockRefForSummarySeq,compactionIdsOfKernelBlocks,summarySeqOfKernelBlock) and are invoked per element in loops:search_context→buildSearchDocsloopsexpandShadowedSeqsover every block → O(B²·N)compress→handleCompresscallsblockRefForSummarySeq×2 +compactionIdsOfKernelBlocksper range → O(R·B·N)summarySeqOfKernelBlockper target blockSo search / compress / nudge latency grew super-linearly with the number of blocks.
Fix
Memoize
rebuildBlockLedgeron the input snapshot array via a module-levelWeakMap, guarded by array length. Safe because:sessionEventsOfreuses it until the next append);The function is pure and no caller mutates the returned ledger, so sharing the cached array is correct. Net effect: each tool call rebuilds the ledger once (O(B·N)) instead of per element — a ~B× reduction on the hot paths. Zero signature changes; all callers benefit automatically.
Verification
npm run typecheck✅npm test→ 209/209 pass (added an idempotency regression test asserting repeated calls on one snapshot return an identical ledger)npm run build✅ —dist/regenerated for CI's dist-drift check;dist/region.d.tsis byte-identical to main (no public type change)Files
src/region.ts(+7): theWeakMapmemo + length guardtests/region.test.ts(+21): idempotency regression testdist/index.js,dist/index.js.map: rebuilt bundle