Skip to content

perf(sqlite): keep a short fingerprint of saved rows instead of full copies in memory - #48207

Merged
brennanb2025 merged 1 commit into
mainfrom
perf/keyed-json-row-cache-hash-baseline
Nov 25, 2026
Merged

brennanb2025 merged 1 commit into
mainfrom
perf/keyed-json-row-cache-hash-baseline

Conversation

@nwparker

@nwparker nwparker commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

Several of Orca's small databases kept a full copy of every saved row in memory, just to tell later whether a row changed. Now they keep a short fingerprint of each row instead, which is enough to spot a change.

Files Added Deleted Net
Test 0 0 0 0
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​76 $\color{#cf222e}{\Huge{\mathbf{−}}}$​56 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​20

What Changed

  • KeyedJsonRowCache keeps a SHA-256 digest (44 bytes) of each stored row's JSON, plus its sequence number, in place of the full JSON string.
  • Planned writes still carry the JSON to SQLite; only the in-memory baseline shrinks.
  • On close the cache also drops its baseline, so a closed store holds no rows.
  • The shared types move into keyed-json-row-plan.ts (re-exported from the cache module) to avoid an import cycle.
  • Nothing users see changes.

Why

The baseline exists only to skip writing rows that didn't change. Holding every row's JSON to do that kept the whole GitHub cache, last-status and catalog contents in RAM twice. A digest answers the same question at a fixed size. An accidental SHA-256 collision is not a practical risk.

Linked Issue

N/A (maintainer PR; part of a five-PR memory series measured on a 1,266-worktree profile)

Visual Proof

N/A: memory-only change; nothing on screen changes.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below
  • Every added test fails without this change and avoids the test anti-patterns

No new tests: the existing row-cache tests already cover skip-unchanged, reinsert, restart and legacy import through the public API, and they pass. Memory before/after on a seeded ~1,900-workspace Windows profile is in the review comment.

Performance

Lowers memory: −9.2 MiB of main heap on a 1,266-worktree profile. CPU: each compared row is hashed. Patch writes, including the per-status agent updates, hash only the changed rows. Full writes hash bytes the caller has just serialized; about +1 ms per MB written (4,096 rows / ~1 MB: 1.67 ms against 0.64 ms). There are no new writes, timers or queries.

Risk

Level: Low. If the digest planning were wrong, a row could be rewritten needlessly or, in theory, skipped. Equal content still produces equal digests, and the on-disk format is unchanged, so older and newer builds read the same files. Revert is a plain revert.

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Ensure no issues in: Security, Cross-platform support (Linux, Windows, Mac), Remote SSH, Mobile, general backwards compatibility, performance

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • I stated the risk: what could break, for whom, and how to back it out
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@brennanb2025

Copy link
Copy Markdown
Contributor

Perf scan (head e3498acd733)

Findings: none.

  • Trigger and work: each KeyedJsonRowCache write now SHA-256 hashes the JSON of every row it compares, where it used to compare strings. Patch writes (agent last-status, the busiest caller at 1–2 status updates/s) hash only the changed rows: one small row per update, microseconds. Full writes (the GitHub cache, dismissals, catalog) hash every row; those callers already JSON.stringify every row on each write, so hashing adds a constant factor on the same bytes and doesn't change how the work scales. A baseline reread (another connection wrote, or the database reopened) hashes all stored rows once.
  • Price: the blinded audit measured 4,096 unchanged rows (~1 MB) at 1.67 ms, against 0.64 ms for the old string compare. That is about +1 ms per MB per full write. Full writes are debounced or happen once per change, not per status update. There are no new timers, wakeups, queries or disk writes. Retained memory drops from full JSON to 44 bytes per row (−9.2 MiB on a 1,266-worktree profile, author-measured).
  • Classes touched: whole-file rewrite per change (unchanged: only rows whose digest differs are written; equal content still writes nothing); sync SQLite on main (unchanged statements and count).
  • Six questions: 1: equal rows are still skipped before any SQLite write. 2: one cache per store, unchanged. 3: main thread, linear in bytes already being serialized. 4: retention shrinks. 5: no host-specific code. 6: on close the cache now drops its baseline (memory is released; the next open rereads, as before).

@brennanb2025 brennanb2025 changed the title perf(main): store SHA-256 digest instead of raw JSON in KeyedJsonRowCache baseline perf(sqlite): keep a short fingerprint of saved rows instead of full copies in memory Nov 24, 2026
@brennanb2025

Copy link
Copy Markdown
Contributor

Review status — head e3498acd733: ready from review.

  • Code review (Claude): correct; no changes needed.
  • Blinded regression audit (Codex, diff + purpose only): VERDICT: SAFE on this head.
  • Perf scan: clean (see the perf-scan comment).
  • Memory, Windows high spec, one seeded profile snapshot reused for every arm (1,921 worktrees in 20 repos, 1,800 chats, 20k packed refs and 6k branch.* keys per repo), post-GC median of 3 samples after a 4-minute settle, compared with the build just below this PR in the stack (14e75caebed):
    • main-process heap 93.1 → 94.4 MiB (+1.3)
    • renderer heap 88.4 → 88.9 MiB (+0.5)
    • app process tree private bytes 905.5 → 922.0 MiB (+16.5)
    • no measurable change on this profile: its stored rows are small (no GitHub cache content, few catalog rows), and +1.3 MiB is within sample spread (93.4–94.7). The saving grows with stored-row bytes (Neil's real profile: −9.2 MiB).
  • CI: green.

Benchmark: name=fleet-memory-win-high head=e3498acd7331c364c85b739cb0e011a2cc59e21c gate=PASS (no memory regression against 14e75caebed; main heap +1.3 MiB)

@brennanb2025
brennanb2025 force-pushed the perf/keyed-json-row-cache-hash-baseline branch from e3498ac to 3c1d18d Compare November 25, 2026 01:16
@brennanb2025
brennanb2025 changed the base branch from perf/detach-git-cache-sliced-strings to main November 25, 2026 01:16
@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Row planning now hashes JSON rows with SHA-256 and base64 encoding. Full-row and patch planning compare sequence numbers and hashes when deciding whether to emit writes. The cache stores sequence and hash values in its baseline and uses ordered stored-row iteration for snapshots and baseline reads. Closing the database clears the baseline and resets the maximum sequence.

Merge Risk: Moderate · up to 3c1d1

This change trades memory for extra CPU work by hashing saved rows instead of keeping them in memory. It cuts memory on large profiles, but it makes unchanged-row comparisons slower and slightly raises memory on small profiles. It is not a correctness risk. Accept the small performance regression explicitly or show it is negligible before merging.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the row cache now keeps a short SHA-256 fingerprint per saved row instead of full JSON copies in memory. The perf(sqlite) prefix fits the memory-focused intent. The ch…
Description check Passed The description follows the repository template. It fills in ELI5, What Changed, Why, Testing, Performance, Risk, and the checklist with concrete detail. The Linked Issue section says N/A and gives a …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the row cache now keeps a short SHA-256 fingerprint per saved row instead of full JSON copies in memory. The perf(sqlite) prefix fits the memory-focused intent. The changeset matches this description.

Full details: Description check

Explanation

The description follows the repository template. It fills in ELI5, What Changed, Why, Testing, Performance, Risk, and the checklist with concrete detail. The Linked Issue section says N/A and gives a reason, which is a maintainer exception. The Visual Proof section uses N/A with a reason. The AI Disclosure and Review sections are empty, but the template says to skip AI Disclosure for internal team members. The Review section is empty and has no content. These gaps are minor and do not block the check.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cb93b67b-1b74-49e4-8360-a7138cde3b26
Commits

Reviewing files that changed from the base of the PR and between 436feee and 3c1d18d.

Files selected for processing: 2

  • src/main/sqlite/row-cache/keyed-json-row-cache.ts
  • src/main/sqlite/row-cache/keyed-json-row-plan.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@brennanb2025

Copy link
Copy Markdown
Contributor

Perf scan (head 3c1d18d0f96): this head carries the same diff as e3498acd733, moved for unstacking; the scan above stands unchanged. Findings: none.

@brennanb2025

Copy link
Copy Markdown
Contributor

Review status — head 3c1d18d0f96: ready from review.

  • Code review (Claude): correct; no changes needed.
  • Blinded regression audit (Codex, diff + purpose only): VERDICT: SAFE on this head.
  • Perf scan: clean (see the perf-scan comment).
  • Memory, Windows high spec, one seeded profile snapshot reused for every arm (1,921 worktrees in 20 repos, 1,800 chats, 20k packed refs and 6k branch.* keys per repo), post-GC median of 3 samples after a 4-minute settle, compared with the build just below this PR in the stack (14e75caebed):
    • main-process heap 93.1 → 94.4 MiB (+1.3)
    • renderer heap 88.4 → 88.9 MiB (+0.5)
    • app process tree private bytes 905.5 → 922.0 MiB (+16.5)
    • no measurable change on this profile: its stored rows are small (no GitHub cache content, few catalog rows), and +1.3 MiB is within sample spread (93.4–94.7). The saving grows with stored-row bytes (Neil's real profile: −9.2 MiB).
  • Unstacked: this head carries the same diff as the measured e3498acd733, rebased for the base change; memory numbers were taken on that code.
  • CI: green.

Benchmark: name=fleet-memory-win-high head=3c1d18d0f96623b18aa088e046eb0160f3d889a5 gate=PASS (no memory regression against 14e75caebed; main heap +1.3 MiB)

@brennanb2025
brennanb2025 merged commit 04e51d5 into main Nov 25, 2026
63 checks passed
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.

2 participants