Commit ade7f34
committed
perf(metrics): Cache gpt-tokenizer BPE ranks as JSON to cut tokenizer init
The metrics worker's TokenCounter initialization is the single largest cost on
a warm-cache CLI run. gpt-tokenizer ships each BPE merge-rank table as a ~2 MB
CommonJS module of inline array literals; `resolveEncodingAsync` `require`s it,
forcing V8 to lex/parse/execute the file and allocate a ~200k-element array on
every cold worker thread (~120 ms), before `GptEncoding.getEncodingApi` then
builds the rank Map (~90 ms). A verbose trace showed this landing at ~211 ms on
the metrics critical path that gates output generation.
The value returned by `resolveEncodingAsync` is a plain JSON-serializable array
(strings, plus byte arrays of 1–19 bytes for ranks whose token bytes are not
valid UTF-8). New `bpeRanksCache.ts` persists it once as JSON under
`$TMPDIR/repomix/cache/bpe-ranks/` and, on later runs, reloads it via
`readFileSync` + `JSON.parse` (~40 ms) — a restricted-grammar parse V8 handles
in native C++, ~3x faster than re-executing the JS module. `getEncodingApi`
receives a byte-identical ranks array, so token counts are unchanged.
intent(metrics): cut the ~211ms tokenizer init that dominates warm-cache runs
decision(bpe-cache): runtime JSON disk cache via gpt-tokenizer's public resolveEncodingAsync output — no build step, no bundled data, no internal imports
rejected(bpe-cache): bundling a pre-built ranks JSON in lib/ — adds ~2MB to the published package and reaches into gpt-tokenizer's internal cjs/bpeRanks path, fragile across upgrades
decision(module-split): extract the cache to bpeRanksCache.ts mirroring tokenCountCache.ts — keeps TokenCounter focused and makes the cache unit-testable
decision(cache-key): key the file by gpt-tokenizer version + format version so an upgrade auto-invalidates stale tables (miss → rebuild)
constraint(bpe-cache): runs inside worker threads — sync fs + crypto-random tmp name + atomic rename so a concurrent worker (which shares this pid) never reads a partial file; all read/write errors fall back to resolveEncodingAsync (pure optimization, never a correctness signal)
constraint(shape-guard): readBpeRanksCache rejects any non-array/empty JSON as a clean miss, so a structurally-valid-but-wrong file is rebuilt rather than silently producing zero counts
constraint(opt-out): shares the REPOMIX_TOKEN_CACHE=0 switch with the token-count cache; REPOMIX_BPE_RANKS_CACHE_PATH redirects the dir for tests
Correctness:
- Library-level equivalence verified: JSON round-trip yields identical encode()
output and countTokens() across source code, unicode, emoji, special tokens
(<|endoftext|>) and invalid UTF-8; all 1571 byte-array rank entries (lengths
1–19) preserved for o200k_base and cl100k_base.
- Full CLI token totals identical with the cache enabled vs REPOMIX_TOKEN_CACHE=0,
and the generated output file is byte-identical.
- 1332 tests pass (12 new for the cache: hit/miss/disabled/corrupt/malformed-shape
fallback); `tsgo --noEmit`, biome and oxlint clean.
Benchmark — `node bin/repomix.cjs --include src -o <tmp> --quiet`, warm cache,
25 runs/round, baseline (no cache) and patched rebuilt and run interleaved
(two rounds each, to control for environment drift):
base: 773.1 / 742.6 ms (mean 757.9)
patched: 722.0 / 688.1 ms (mean 705.1)
delta: -52.8 ms (7.0%) — minimums ~703 → ~665 ms move in lockstep
Exceeds the 2% target and sits outside the run-to-run noise band (sd ~25 ms on
the patched runs) as shown by the consistent direction across both rounds.1 parent 6106f30 commit ade7f34
4 files changed
Lines changed: 259 additions & 3 deletions
File tree
- src/core/metrics
- tests
- core/metrics
- testing
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
10 | 30 | | |
11 | 31 | | |
12 | 32 | | |
| |||
30 | 50 | | |
31 | 51 | | |
32 | 52 | | |
33 | | - | |
34 | | - | |
35 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
36 | 57 | | |
37 | 58 | | |
38 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
1 | 4 | | |
2 | 5 | | |
3 | 6 | | |
| |||
6 | 9 | | |
7 | 10 | | |
8 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
0 commit comments