- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/worker/project/definition-code.ts?format=json
90 linescodex-capacity-chair 77% · capacity 23%Last change capacity in version 275, 3 versions changed it
1import type { ReadView, VisibleLine } from "../../src/engine/index.ts";
2import { StoredText } from "../../src/engine/stored-text.ts";
3import { eachCodeLine } from "./names.ts";
5/**
6 * Characters of parsed code kept in memory before the rest goes to scratch (abso Capacity). The visible lines it is
7 * parsed from are already in memory, so this at most doubles a file's text for the call; at 64 KB, every large file's
8 * code went row by row into scratch and back, 25,000 rows each for abso's index.ts, a third of a cold collisions call.
9 */
10export const DEFINITION_CODE_IN_MEMORY = 4_000_000;
12let inMemoryLimit = DEFINITION_CODE_IN_MEMORY;
14/**
15 * Tests only: the in-memory limit for every later call, so a small file takes the scratch path (abso's kept-reader
16 * test checks scratch is let go). Answers the limit it replaces, to put back.
17 */
18export function setDefinitionCodeInMemory(limit: number): number {
19 const before = inMemoryLimit;
20 inMemoryLimit = limit;
21 return before;
22}
24/** Parsed code is temporary derived data, discarded with the scratch transaction. */
25export function withDefinitionCode<T>(
26 view: ReadView,
27 visible: readonly VisibleLine[],
28 operation: (code: readonly string[]) => T,
29 inMemory = inMemoryLimit,
30): T {
31 function* source() {
32 for (const line of visible) yield line.text;
33 }
34 if (!view.graph.hasOrderedChildren) return operation([...eachCodeLine(source())]);
35 const iterator = eachCodeLine(source());
36 let prefix: string[] = [];
37 let size = 0;
38 let pending = iterator.next();
39 while (!pending.done && size + pending.value.length + 1 <= inMemory) {
40 prefix.push(pending.value);
41 size += pending.value.length + 1;
42 pending = iterator.next();
43 }
44 if (pending.done) return operation(prefix);
45 return view.graph.withProjectionScratch((storage) => {
46 const text = new StoredText(storage, "definition-code");
47 let count = 0;
48 for (const line of prefix) text.write(String(count++), line);
49 prefix = [];
50 text.write(String(count++), pending.value!);
51 for (const line of iterator) text.write(String(count++), line);
52 const cache = new Map<number, string>();
53 let characters = 0;
54 const read = (index: number) => {
55 if (index < 0 || index >= count) return undefined;
56 const cached = cache.get(index);
57 if (cached !== undefined) return cached;
58 const value = text.read(String(index));
59 if (value.length <= 65536) {
60 while (cache.size >= 64 || characters + value.length > 65536) {
61 const first = cache.keys().next().value!;
62 characters -= cache.get(first)!.length;
63 cache.delete(first);
64 }
65 cache.set(index, value);
66 characters += value.length;
67 }
68 return value;
69 };
70 let active = true;
71 const code = new Proxy([] as string[], {
72 has(target, key) {
73 if (!active) throw Error("definition code scope has ended");
74 if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) return Number(key) < count;
75 return Reflect.has(target, key);
76 },
77 get(target, key, receiver) {
78 if (!active) throw Error("definition code scope has ended");
79 if (key === "length") return count;
80 if (typeof key === "string" && /^(0|[1-9][0-9]*)$/.test(key)) return read(Number(key));
81 return Reflect.get(target, key, receiver);
82 },
83 });
84 try {
85 return operation(code);
86 } finally {
87 active = false;
88 }
89 });
90}