Skip to content

perf(snapshot): serialize once in writeSnapshotDual, byte-copy to the cache#655

Closed
justrach wants to merge 1 commit into
release/0.2.5828from
perf/snapshot-write-once
Closed

perf(snapshot): serialize once in writeSnapshotDual, byte-copy to the cache#655
justrach wants to merge 1 commit into
release/0.2.5828from
perf/snapshot-write-once

Conversation

@justrach

@justrach justrach commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Round-2 item 3 (snapshot write-path survey — the write path had never had a perf pass; load got 3× in #524).

What

writeSnapshotDual ran the full writeSnapshot serialization twice — project-root snapshot, then ~/.codedb/projects/<hash>/: two shared-lock acquisitions, two per-file content-hash passes, two tree/outline serializations, and — after releaseContents() (the normal post-scan server state, #519) — two whole-repo disk reads. A finished snapshot is a plain file: serialize once, then byte-copy to the cache path with the same tmp-random-suffix + rename contract.

writeProjectCacheSnapshot keeps full-serialization behavior for its standalone callers (bootstrap CLI fallback, tests).

Correctness

  • Same write order (primary first), same swallowed-error semantics for the cache copy, same atomic-rename crash-safety, no format change.
  • Bonus guarantee: the two copies are now byte-identical — verified by sha256 after a live run — where two serializations could diverge between lock acquisitions.
  • zig build test: 23/23 steps, 869/873 (4 platform skips). e2e: 20/20.

Numbers

  • Synthetic 2,000-file / 92MB corpus, codedb <root> snapshot, 3 runs each: 0.78s → 0.72s median — the eliminated second serialization (~60ms here) scales with snapshot size, and grows further on released-contents servers where it also paid the whole-repo re-read.
  • This repo (small snapshot): no measurable wall change (0.06s both) — the win is size-proportional, as expected.

🤖 Generated with Claude Code

… cache

writeSnapshotDual ran the full writeSnapshot serialization twice — once
for the project-root snapshot and again for ~/.codedb/projects/<hash>/:
two lock acquisitions, two content-hash passes, and (after
releaseContents) two whole-repo disk reads. A finished snapshot is a
plain file: copy it to the cache path instead (own tmp + rename, same
crash-safety contract). The two copies are now byte-identical, where two
serializations could diverge between lock acquisitions.

writeProjectCacheSnapshot keeps its full-serialization behavior for its
standalone callers (bootstrap CLI fallback, tests).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@justrach

justrach commented Jul 4, 2026

Copy link
Copy Markdown
Owner Author

Superseded by #652 (landed while this was in review — same change, convergent implementations from the same write-path survey). Numbers from this PR's validation transfer: 92MB corpus snapshot 0.78s → 0.72s median, cache copy byte-identical (sha256-verified).

@justrach justrach closed this Jul 4, 2026
@justrach justrach deleted the perf/snapshot-write-once branch July 4, 2026 10:06
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