code-index eval refresh-cost.md
6.8 kB · 126 lines
Markdown
at main

What an incremental refresh spends its time on #

Recorded 2026-09-14 with eval/refresh-cost.py, against a synthetic index of the live one's shape: 2,448 files, 36,720 chunks of ~974 characters, 1024-wide vectors — a 150 MB matrix beside a ~45 MB SQLite database. The embedder is stubbed and the data directory is a temporary one, so the shared index and the server on 8177 were untouched.

Rows inserted and bytes written, not wall-clock. A full build had all 16 cores for the duration, which makes any timing a measurement of the other process. Bytes come from /proc/self/io write_bytes, so they cover SQLite's WAL traffic and the matrix file alike; the matrix share is the size of the generation file each operation leaves behind, counted only when the file was written rather than hardlinked.

One file changed #

operation                      rows      matrix   write_bytes
replace_from(shadow)          36720 150 MB link         78 MB
rebuild(shadow)               36718      150 MB        229 MB
rebuild(shadow)               36718      150 MB        229 MB
replace_from(live)            36718 150 MB link         78 MB
total                        146874      301 MB        614 MB

13 chunks were re-embedded. Publishing them cost 146,874 row inserts and 614 MB written — four full passes over the index for one edited file.

The two rebuild calls are checkpoints. Only two fire on a quiet refresh, both at the end: indexer.py _build() checkpoints when a repository finishes only if CHECKPOINT_EVERY chunks have been embedded since the last one, so on a run with almost nothing to embed the only triggers left are the last repository and the final checkpoint("complete"). They write the same index twice, back to back.

Neither half dominates #

Matrix writing is 301 MB of the 614 MB; rewriting rows is the other 313 MB. The split holds at the other size measured too. So there is no single cheap fix: the np.save in store.py rebuild() and the delete-and-reinsert in _replace_rows() cost the same order, and cutting one alone halves the run at best.

Per pass the shapes differ. A replace_from hardlinks the matrix — 0 bytes for 150 MB of vectors, which is _link_or_copy() doing its job — and spends its whole 78 MB on rows. A rebuild pays both: 150 MB of matrix plus the same ~78 MB of rows. The row cost is ~2,100 bytes per chunk against ~1,250 bytes of stored text, the difference being WAL.

Checkpoints multiply with real work #

With 800 files changed (10,400 chunks re-embedded, the scale of a busy day rather than a single edit) checkpoints fire every 2,000 chunks and each one rewrites everything accumulated so far:

operation                      rows      matrix   write_bytes
replace_from(shadow)          36720 150 MB link         78 MB
rebuild(shadow)                6069       25 MB        103 MB
rebuild(shadow)               13199       54 MB         85 MB
rebuild(shadow)               19341       79 MB        142 MB
rebuild(shadow)               26518      109 MB        171 MB
rebuild(shadow)               33695      138 MB        217 MB
rebuild(shadow)               35120      144 MB        226 MB
rebuild(shadow)               35120      144 MB        226 MB
replace_from(live)            35120 144 MB link         82 MB
total                        240902      692 MB       1330 MB

Checkpoint n rewrites the whole prefix, so the total grows with the square of the number of checkpoints. Ten thousand new chunks cost 240,902 row inserts.

What this said to fix #

  1. The two trailing checkpoints are pure waste. They write an identical index twice, the second immediately before replace_from(live) copies it a third time. Skipping a checkpoint whose contents are unchanged removes ~229 MB and ~37k row inserts from every refresh, with no change to the guarantees.
  2. Append instead of replace. A checkpoint that appends new vector rows to the matrix and inserts only new chunk rows turns both curves above from quadratic into linear, and is the only change that helps a refresh with real work in it.
  3. The shadow seed and the promotion are already cheap on the matrix side and expensive only in rows. They are worth attacking after the checkpoints, not before.

After #

store.py append() publishes a checkpoint's own rows and nothing else, and indexer.py _build() tracks how far the shadow has been published so a checkpoint with nothing new to say writes nothing at all. Same measurement, same shape of index:

operation                      rows          matrix   write_bytes
replace_from(shadow)          36720   150.4 MB link       78.3 MB
rebuild(shadow)               36718        150.4 MB      228.7 MB
replace_from(live)            36718   150.4 MB link       78.3 MB
total                        110156        150.4 MB      385.4 MB

One edited file: 614 MB down to 385 MB, 146,874 row inserts down to 110,156. And with 800 files changed, where the checkpoints used to square:

operation                      rows          matrix   write_bytes
replace_from(shadow)          36720   150.4 MB link       78.3 MB
rebuild(shadow)                6069         24.9 MB      103.1 MB
append(shadow)                 7130         29.2 MB       46.0 MB
append(shadow)                 6142         25.2 MB       39.8 MB
append(shadow)                 7177         29.4 MB       46.3 MB
append(shadow)                 7177         29.4 MB       46.3 MB
append(shadow)                 1425          5.8 MB        7.6 MB
replace_from(live)            35120   143.9 MB link       82.1 MB
total                        106960        143.9 MB      449.5 MB

1,330 MB down to 450 MB, 240,902 row inserts down to 106,960, and the total is now flat in the number of checkpoints instead of growing with its square.

What is left #

Three passes over every row remain, and they are all the same shape: the shadow seed, the start-of-run rebuild and the promotion each copy the whole index row by row through Python. They are what stops a one-file refresh from costing what one file costs.

The rebuild is there because the shadow is seeded with a copy of the live index and the run's own rows have to replace it rather than follow it. The seed is there so that unchanged files find their old vectors. Removing either means the shadow starting as a reference to the live generation rather than a copy of it, which is the same change in both cases and the one place where the pairing invariant is easiest to break. Nothing here measures it as urgent: the three remaining passes are linear in the index, where the checkpoints were not.