Skip to content

perf(telemetry): cache approxIndexSizeBytes - codedb_status 9.4x faster#474

Merged
justrach merged 1 commit into
mainfrom
perf-status-cache
May 20, 2026
Merged

perf(telemetry): cache approxIndexSizeBytes - codedb_status 9.4x faster#474
justrach merged 1 commit into
mainfrom
perf-status-cache

Conversation

@justrach
Copy link
Copy Markdown
Owner

Fixes the codedb_status +96% regression flagged after PR #471 (trigram cap-lift). Caches the index-size computation for 5 seconds - appropriate for a "this is approximate" memory counter.

Measured (React corpus, 100 iter warm, MCP)

metric before after
p50 423 us 45 us
p95 - 115 us
p99 - 150 us

9.4x faster, also faster than the pre-cap-lift baseline (216 us). Cost: 32 bytes of atomic state.

…9.4x faster

The approxIndexSizeBytes function iterates 3 indexes (word_index,
trigram_index, sparse_ngram_index) to sum approximate memory usage. It's
called by codedb_status — the only caller — once per status call.

After PR #471 lifted the trigram cap from 64KB to 1MB (a correctness fix
that pulled large code files into the index), this iteration got ~2x
slower because trigram_index grew. The CI bench flagged codedb_status
+96% (216us -> 423us) which was noisy enough to gate the parity merge.

Fix: cache the computed size with a 5-second TTL. Status is a
human-readable summary that says "index_memory: NN KB"; 5-second staleness
is fine for an approximate counter. The first call after each 5s window
still does the full iteration to refresh.

Measured on React corpus (100 iter warm, codedb_status MCP):

  before (no cache):  ~423us p50
  after  (cached):    45us p50  (9.4x speedup)
                      150us p99 (the periodic refresh)

Also faster than the pre-cap-lift baseline (216us p50), because the
cached fast-path skips even the original iteration cost.

Cost: ~32 bytes of atomic state (two u64 atomics).
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f056a3568a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/telemetry.zig
Comment on lines +340 to +341
if (cached_at != 0 and now - cached_at < SIZE_CACHE_TTL_MS) {
return size_cache_value.load(.monotonic);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scope index-size cache per explorer

This cache is process-global, so codedb_status can return the wrong index_memory when two different projects are queried within 5 seconds: MCP resolves project to different Explorer instances via ProjectCache.get(...) and then calls handleStatus(...), but approxIndexSizeBytes reuses one shared cached value for all explorers. In that scenario, project B will report project A’s index size until the TTL expires, which is a functional regression (not just staleness) for multi-project MCP sessions.

Useful? React with 👍 / 👎.

@github-actions
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 488757 505230 +3.37% +16473 OK
codedb_changes 55241 57802 +4.64% +2561 OK
codedb_deps 9155 9281 +1.38% +126 OK
codedb_edit 7076 6405 -9.48% -671 OK
codedb_find 60712 59768 -1.55% -944 OK
codedb_hot 97478 94682 -2.87% -2796 OK
codedb_outline 295472 298262 +0.94% +2790 OK
codedb_read 98485 94228 -4.32% -4257 OK
codedb_search 143508 144163 +0.46% +655 OK
codedb_snapshot 286449 295067 +3.01% +8618 OK
codedb_status 421198 13348 -96.83% -407850 OK
codedb_symbol 60805 57321 -5.73% -3484 OK
codedb_tree 60688 64983 +7.08% +4295 OK
codedb_word 85684 87006 +1.54% +1322 OK

@justrach justrach merged commit fc4caa8 into main May 20, 2026
1 check passed
@justrach justrach deleted the perf-status-cache branch May 20, 2026 19:56
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