Skip to content

perf: cut cold-index RSS 4.3GB→1GB and wall-time ~6.5×#519

Merged
justrach merged 1 commit into
release/0.2.5824from
feat/lesser-memory
Jun 1, 2026
Merged

perf: cut cold-index RSS 4.3GB→1GB and wall-time ~6.5×#519
justrach merged 1 commit into
release/0.2.5824from
feat/lesser-memory

Conversation

@justrach

@justrach justrach commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Summary

Two independent fixes to the initial scan/index path. Measured on a ~16k-file repo with a cold codedb index:

RSS peak wall-clock
before 4.34 GB ~24 s
after 1.01 GB 3.7 s

−77% memory, ~6.5× faster — no functional change.

1. Memory — per-file scratch arena (src/watcher.zig)

Each parse worker held one long-lived page_allocator arena for its entire chunk. parseContentForIndexing throws off large transient parse state (AST/token buffers) that isn't part of the returned content/outline; an arena never reclaims freed memory, so each worker's arena ballooned to the high-water-mark of every file's transients across its whole chunk (~3.5 GB total).

Now each file is parsed in a per-file scratch arena that resets between files; only keep-data (content dupe + Explorer.cloneOutline, made pub) is copied into the persistent results arena. The threading model is unchanged — parse parallel, commit serial on the main thread — so the per-worker copy is race-free.

2. Speed — symbol-index O(n²) (src/explore.zig)

commit called removeSymbolIndexFor per file, which scans the entire global symbol index to evict the path's old entries. On a cold scan every file is brand-new (nothing to evict), so the whole pass was O(files × total_symbols) — billions of comparisons. Skip the scan when the path has no prior entry (had_prior=false).

Commit phase: 23,713 ms → 1,517 ms (14×).

Phase breakdown (after)

walk:                70 ms
parse (parallel x8): 495 ms
commit (serial):  1,517 ms   (was 23,713 ms)
trigram-build:      628 ms
snapshot:           261 ms

The parse was already fast (0.5s), so the win came from the commit, not from adding parallel workers.

Verification

  • zig build test670/670 pass
  • Rebuilt the index and confirmed search (50 hits) and symbol lookup (find) return correct results.

Also bumps build.zig.zon to 0.2.5824.

Two independent fixes to the initial scan/index path, measured on a
~16k-file repo (cold `codedb index`): 4.3GB/~24s -> 1.0GB/3.7s.

1. Memory (src/watcher.zig, initialScanWorker): each parse worker held
   one long-lived page_allocator arena for its whole chunk, so it
   retained the high-water-mark of every file's transient parse state
   (AST/token buffers from parseContentForIndexing) chunk-wide and never
   reclaimed it (~3.5GB). Now each file is parsed in a per-file scratch
   arena that is reset between files; only keep-data (content dupe +
   Explorer.cloneOutline, now pub) is copied into the persistent results
   arena. Threading model unchanged (parse parallel, commit serial on the
   main thread), so the copy is race-free. -> 4.3GB to 1.0GB.

2. Speed (src/explore.zig, rebuildSymbolIndexFor): commit called
   removeSymbolIndexFor per file, which scans the entire global symbol
   index. On a cold scan every file is new (nothing to evict), making the
   whole pass O(files * total_symbols). Skip the scan when the path has no
   prior entry (had_prior=false). Commit phase 23.7s -> 1.5s.

Both verified: 670/670 tests pass; search + symbol lookup (`find`)
confirmed correct on the rebuilt index.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 1, 2026

Copy link
Copy Markdown

Benchmark Regression Report

Thresholds: 10.00% and 50,000 ns absolute delta

NOISE means the percentage threshold was exceeded, but the absolute delta was too small to fail CI.

Tool Base (ns) Head (ns) Delta Abs Delta (ns) Status
codedb_bundle 86617 85571 -1.21% -1046 OK
codedb_changes 10481 10768 +2.74% +287 OK
codedb_context 801568 805536 +0.50% +3968 OK
codedb_deps 304 287 -5.59% -17 OK
codedb_edit 41660 42995 +3.20% +1335 OK
codedb_find 10649 9370 -12.01% -1279 OK
codedb_hot 24314 25180 +3.56% +866 OK
codedb_outline 28156 28965 +2.87% +809 OK
codedb_read 14343 14982 +4.46% +639 OK
codedb_search 22394 21824 -2.55% -570 OK
codedb_snapshot 56420 65644 +16.35% +9224 NOISE
codedb_status 9498 9245 -2.66% -253 OK
codedb_symbol 17950 18019 +0.38% +69 OK
codedb_tree 19641 19565 -0.39% -76 OK
codedb_word 12884 10911 -15.31% -1973 OK

@justrach justrach merged commit 17c4e0e into release/0.2.5824 Jun 1, 2026
1 check passed
@justrach justrach deleted the feat/lesser-memory branch June 1, 2026 13:12
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.

1 participant