Skip to content

prompt cache: open Imprint in linear time - #2316

Merged
ashhart merged 3 commits into
mainfrom
lead/imprint-linear
Nov 8, 2026
Merged

ashhart merged 3 commits into
mainfrom
lead/imprint-linear

Conversation

@ashhart

@ashhart ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Imprint.open (the --learn prompt cache) had two O(N²) loops at startup. Together they took about 5.1 s of the 5.7 s open at 100,000 kept states. This PR makes the open linear and keeps the result exactly the same.

What changed

  • Duplicate check (readIndex): m.has(x.key) scanned every record kept so far. A local AutoHashMapUnmanaged(u64, void) of the kept keys now does that check. The first record of a key is kept and later ones are freed, as before.
  • Unindexed-file sweep (learned_dirs.removeUnindexed): it scanned every record for each file in the directory. It now takes the allocator and builds one set of the records' u64 keys. The whole set is reserved before the directory is opened, so a failed allocation removes nothing. The set holds keys rather than file names, so an indexed key's .bin, .r0.bin and .r1.bin all stay.
  • test-startup-states-timing: a new host-only ReleaseFast step, outside the default test run. It writes 1K, 10K and 100K synthetic kept states in each store's own format, opens each store in five fresh processes, and reports the time and peak memory.

Timing (Apple M3 Ultra, ReleaseFast, five processes a count)

States Open before Open after Heap peak before → after
1,000 7.5 ms 7.4 ms 34.9 → 34.9 MiB
10,000 119.6 ms 72.5 ms 349.8 → 350.0 MiB
100,000 5,812.7 ms 716.0 ms (8.0×) 3,506.1 → 3,507.3 MiB

Each 10× more states now costs about 10× more time. Before, it cost 16× and 43–48×.

Exactness: the same records are kept in the same order, the same duplicates are dropped and the same files are removed. The five new tests in prompt_imprint_open_test.zig all go through Imprint.open, and they also passed on the old code before the fix:

  • duplicate records;
  • an indexed key and an unindexed key under every suffix, beside a file that isn't a state;
  • records whose files are missing;
  • an open that runs out of memory at each allocation;
  • a random index of 305 records and a directory of 364 files, checked against the old linear scans.

Checks

  • zig build test-learn-recovery: 526/526 pass. Main has 521, so the 5 new tests run there.
  • The prompt_imprint.zig tests: 15/15 pass.
  • test-startup-states-timing -Dstartup-states-counts=1000: exits 0.
  • zig fmt --check: passes.

find, has, touch and remove are still linear scans. They run on reads and writes, not at open.

@ashhart

ashhart commented Nov 6, 2026

Copy link
Copy Markdown
Owner Author

/atc exchange d2e62f72c645591669dedd517edc25b542af25422c89ff99c52294a80dbe8480

Add test-startup-states-timing, a host-only ReleaseFast step outside the
default test run. It seeds 1,000, 10,000 and 100,000 synthetic kept
states in each store's own on-disk format, with realistic metadata: chat
conversations of 2K to 64K tokens with shared system prompts, pasted
documents, forks and decoded spans, laid out as the 27B's image with its
drafter's taps ring, so the rows store also gets each state's block
files; and --learn's Imprint index of 1K to 32K-token prefixes with
their payload files. It then opens each store in five fresh processes,
reporting time, requested heap peak and the process footprint peak, the
open's parts measured alone, and the median.
Imprint.open compared every index record with every record before it, and
the unindexed-file sweep compared every file in the directory with every
record: two O(N^2) loops, about 5.1 s of the 5.7 s open at 100,000 records.
readIndex now keeps a hash set of the keys it has kept. removeUnindexed
takes the allocator and builds one set of the records' u64 keys, reserved
whole before the directory is read, so a failed allocation removes nothing.
The set holds keys rather than file names, so an indexed key's .bin, .r0.bin
and .r1.bin files all stay.

The same records are kept in the same order, the same duplicates dropped
and the same files removed. New host tests cover duplicate records, an
indexed and an unindexed key under every suffix beside a file that isn't a
state, records whose files are missing, an open that runs out of memory,
and a random index and directory checked against the old linear scans.
tf-startup-states' Imprint phases now time the key set instead of
estimating the old scans.
@ashhart
ashhart force-pushed the lead/imprint-linear branch from 237e524 to 534772c Compare November 8, 2026 02:26
@ashhart
ashhart force-pushed the lead/imprint-linear branch from 534772c to eac1697 Compare November 8, 2026 02:49
@ashhart
ashhart merged commit 8176f08 into main Nov 8, 2026
6 of 11 checks passed
@ashhart
ashhart deleted the lead/imprint-linear branch November 10, 2026 16:34
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