Repository navigation
prompt cache: open Imprint in linear time - #2316
Merged
Merged
Conversation
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
force-pushed
the
lead/imprint-linear
branch
from
November 8, 2026 02:26
237e524 to
534772c
Compare
ashhart
force-pushed
the
lead/imprint-linear
branch
from
November 8, 2026 02:49
534772c to
eac1697
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Imprint.open(the--learnprompt 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
readIndex):m.has(x.key)scanned every record kept so far. A localAutoHashMapUnmanaged(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.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.binand.r1.binall 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)
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.zigall go throughImprint.open, and they also passed on the old code before the fix:Checks
zig build test-learn-recovery: 526/526 pass. Main has 521, so the 5 new tests run there.prompt_imprint.zigtests: 15/15 pass.test-startup-states-timing -Dstartup-states-counts=1000: exits 0.zig fmt --check: passes.find,has,touchandremoveare still linear scans. They run on reads and writes, not at open.