perf(usage): cut warm usage scans from seconds to milliseconds on large histories (#15149)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
39efcd8558
commit
71dbaf1f94
9 files changed
+579
-131
No files matched your search
@@ -103,26 +103,32 @@ interface SerializedCache {
|
||||
readonly files: Readonly<Record<string, SerializedFile>>;
|
||||
}
|
||||
|
||||
/** Serialises the cache, interning the repeated model and session strings. */
|
||||
export function encodeScanCache(cache: ScanCache): SerializedCache {
|
||||
const models: string[] = [];
|
||||
const sessions: string[] = [];
|
||||
const modelIndex = new Map<string, number>();
|
||||
const sessionIndex = new Map<string, number>();
|
||||
/** Model and session strings, each stored once and referenced by index. */
|
||||
interface InternTables {
|
||||
readonly models: string[];
|
||||
readonly sessions: string[];
|
||||
readonly modelIndex: Map<string, number>;
|
||||
readonly sessionIndex: Map<string, number>;
|
||||
}
|
||||
|
||||
const intern = (table: string[], index: Map<string, number>, value: string): number => {
|
||||
const existing = index.get(value);
|
||||
if (existing !== undefined) return existing;
|
||||
const next = table.length;
|
||||
table.push(value);
|
||||
index.set(value, next);
|
||||
return next;
|
||||
};
|
||||
function makeInternTables(): InternTables {
|
||||
return { models: [], sessions: [], modelIndex: new Map(), sessionIndex: new Map() };
|
||||
}
|
||||
|
||||
function intern(table: string[], index: Map<string, number>, value: string): number {
|
||||
const existing = index.get(value);
|
||||
if (existing !== undefined) return existing;
|
||||
const next = table.length;
|
||||
table.push(value);
|
||||
index.set(value, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
function serializeFile(entry: CachedFile, tables: InternTables): SerializedFile {
|
||||
const serializeRecord = (record: UsageRecord): SerializedRecord => [
|
||||
record.timestampMs,
|
||||
intern(models, modelIndex, record.model),
|
||||
intern(sessions, sessionIndex, record.sessionId),
|
||||
intern(tables.models, tables.modelIndex, record.model),
|
||||
intern(tables.sessions, tables.sessionIndex, record.sessionId),
|
||||
record.totals.uncachedInputTokens,
|
||||
record.totals.cachedInputTokens,
|
||||
record.totals.cacheCreationTokens,
|
||||
@@ -132,23 +138,68 @@ export function encodeScanCache(cache: ScanCache): SerializedCache {
|
||||
record.reportedCostUsd,
|
||||
SPEEDS.indexOf(record.speed),
|
||||
];
|
||||
return {
|
||||
s: entry.size,
|
||||
m: entry.mtimeMs,
|
||||
p: entry.provider,
|
||||
r: entry.records.map(serializeRecord),
|
||||
t: entry.tailRecords.map(serializeRecord),
|
||||
o: entry.position.resumeOffset,
|
||||
gl: entry.position.guardLength,
|
||||
gh: entry.position.guardHash,
|
||||
cs: entry.position.codexState,
|
||||
};
|
||||
}
|
||||
|
||||
/** Serialises the cache, interning the repeated model and session strings. */
|
||||
export function encodeScanCache(cache: ScanCache): SerializedCache {
|
||||
const tables = makeInternTables();
|
||||
const files: Record<string, SerializedFile> = {};
|
||||
for (const [path, entry] of cache) {
|
||||
files[path] = {
|
||||
s: entry.size,
|
||||
m: entry.mtimeMs,
|
||||
p: entry.provider,
|
||||
r: entry.records.map(serializeRecord),
|
||||
t: entry.tailRecords.map(serializeRecord),
|
||||
o: entry.position.resumeOffset,
|
||||
gl: entry.position.guardLength,
|
||||
gh: entry.position.guardHash,
|
||||
cs: entry.position.codexState,
|
||||
};
|
||||
}
|
||||
for (const [path, entry] of cache) files[path] = serializeFile(entry, tables);
|
||||
return {
|
||||
version: USAGE_SCAN_CACHE_VERSION,
|
||||
models: tables.models,
|
||||
sessions: tables.sessions,
|
||||
files,
|
||||
};
|
||||
}
|
||||
|
||||
return { version: USAGE_SCAN_CACHE_VERSION, models, sessions, files };
|
||||
/**
|
||||
* Returns a function that serialises the cache to JSON text, re-encoding only
|
||||
* the entries that changed since its last call. Call it once per persist.
|
||||
*
|
||||
* Writes the same document as `encodeScanCache`. Most entries never change
|
||||
* between scans, and encoding all of them made each persist cost close to a
|
||||
* second on a large cache. Entries are replaced, never mutated, when their file
|
||||
* changes, so an entry's JSON is memoised by identity. The intern tables only
|
||||
* grow, so a memoised entry's indexes stay valid; a pruned entry can leave an
|
||||
* unused string behind until the next process start.
|
||||
*/
|
||||
export function makeScanCacheWriter(): (
|
||||
cache: ScanCache,
|
||||
extra: Readonly<Record<string, unknown>>,
|
||||
) => string {
|
||||
const tables = makeInternTables();
|
||||
const fragments = new WeakMap<CachedFile, string>();
|
||||
return (cache, extra) => {
|
||||
const files: string[] = [];
|
||||
for (const [path, entry] of cache) {
|
||||
let fragment = fragments.get(entry);
|
||||
if (fragment === undefined) {
|
||||
fragment = JSON.stringify(serializeFile(entry, tables));
|
||||
fragments.set(entry, fragment);
|
||||
}
|
||||
files.push(`${JSON.stringify(path)}:${fragment}`);
|
||||
}
|
||||
// Encoded after the files, which may have added to the intern tables.
|
||||
const head = JSON.stringify({
|
||||
...extra,
|
||||
version: USAGE_SCAN_CACHE_VERSION,
|
||||
models: tables.models,
|
||||
sessions: tables.sessions,
|
||||
});
|
||||
return `${head.slice(0, -1)},"files":{${files.join(",")}}}`;
|
||||
};
|
||||
}
|
||||
|
||||
function isRecordArray(value: unknown): value is readonly unknown[] {
|
||||
|
||||
Reference in new issue
Block a user