Skip to content

[PR] perf: index message lookups for fold image notes - #6612

Merged
ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-13_index-fold-image-notes
Nov 14, 2026
Merged

ranxianglei merged 1 commit into
ranxianglei:masterfrom
Afloat16:2026-11-13_index-fold-image-notes

Conversation

@Afloat16

Copy link
Copy Markdown
Contributor

Fixes #6610

Problem

After creating a block, applyRanges looks up each direct message with view.find(...) before producing image recovery notes. Plain-text messages take this path too. Folding the first 8,000 messages of a 12,000-message history performs 32,004,000 ID comparisons in this step, synchronously on the proxy event loop.

Change

Build one local ID-to-message index when the first new block with direct messages needs it. Keep the first occurrence of each ID to preserve Array.find behavior, and visit direct IDs in their original order. The selected view remains compressMessages ?? messages; existing blocks and new blocks without direct IDs do not build an index.

The lookup phase becomes O(N + K) for N source messages and K direct IDs. The index adds O(N) temporary references for that call. Image descriptions, recovery refs, summary bytes, eligibility, net-benefit accounting and the other compression steps retain their existing behavior.

Measured effect

scripts/bench-fold-image-notes.ts measures the complete real applyRanges call, using the real kernel and a successful 8,000-message fold over 12,000 valid user/assistant text messages. The serialized input is 5,035,781 bytes.

Three fresh-process pairs, alternating execution order, one warm-up and three measured calls per process:

Pair Master median Indexed median
1 2,676.51 ms 2,350.03 ms
2 2,739.58 ms 2,403.49 ms
3 2,531.24 ms 2,373.06 ms

Across the nine measured calls per variant, the pooled median is 2,676.51 → 2,373.06 ms, an 11.3% reduction in this local workload. Every measured large-workload receipt, summary, covered-ID list and compressed-token total has the same SHA-256: 8fa459ff698fcd2a496fdd1bd814fcab1abe4575014371719c72f369c88f7592.

The small control (16 messages, six folded, 53,285 serialized bytes; 20 measurements per process) stays in the low-millisecond range: pooled 1.976 → 1.744 ms. CPU and peak RSS ranges overlap; this change targets repeated lookup work and makes no memory-reduction claim. Results depend on machine and workload.

Reproduction after building dependencies:

node --expose-gc --import ./tests/preload-test-env.mjs --import ./tests/preload-storage-baseline.mjs --import tsx scripts/bench-fold-image-notes.ts 12000 8000 3 12
node --expose-gc --import ./tests/preload-test-env.mjs --import ./tests/preload-storage-baseline.mjs --import tsx scripts/bench-fold-image-notes.ts 16 6 20 120

Copy the unchanged benchmark into a worktree at the base revision to compare.

Validation

  • Six new real-fold semantic cases pass on both unchanged master and this change: four wire sidecars, image/recovery-ref order and repeated images, unpruned compression view, first-match duplicate IDs, multiple blocks, already-covered no-op and plain-text summaries.
  • 37 related tests pass, covering the new suite, image recovery, Google tool images, refolding, and the net-shrink/regurgitation gates.
  • Typecheck, production build, unused-export gate, test-import graph and git diff --check pass.
  • The Codex prerequisite probe finds codex-cli 0.167.0-alpha.4, but the local upstream at 127.0.0.1:8199 is unreachable; the live four-phase Codex suite was not run. The full-suite verdict remains with hosted CI on this head.

Baseline: e1f31ad9d5b47500362699a9867819b861c7de96 (0.1.213), Node v24.22.1, repository test/storage preloads.

Prior work

All-state searches and contributor history found no existing fix at this step. #2043 indexes summary-carrier evidence in stripKernelSummaries; #6242 removes storage media-array copies. #2022 introduced image recovery, and #3726 concerns visual summary quality. Their mechanisms are separate.

Suggested triage: p2, scope/kernel; Topic C — compression behavior. Source changes are host-only; no kernel/, configuration or persistence changes.

@ranxianglei ranxianglei changed the title perf: index message lookups for fold image notes [PR] perf: index message lookups for fold image notes Nov 13, 2026
@ranxianglei
ranxianglei merged commit a27c87d into ranxianglei:master Nov 14, 2026
45 of 50 checks passed
@ranxianglei

Copy link
Copy Markdown
Owner

Superseded by #6677 (merged as 3858b6956, 2026-11-14).

Landed into master via the real merge commit a27c87d636e (Merge Afloat16/2026-11-13_index-fold-image-notes (PR #6612)), so your original commits are preserved verbatim in master history — only hashes of the batch wrapper changed, your authorship and commit messages are intact.

The fix for #6610 is now in master. Closing this PR as superseded — thanks for the careful, well-tested fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Repeated full-history scans while attaching image notes slow large text-only folds

2 participants