{"draft":null,"from":116,"to":318,"files":[{"path":"worker/project/definition-code.ts","added":90,"removed":0,"diff":"--- /dev/null\n+++ b/worker/project/definition-code.ts\n@@ -0,0 +1,90 @@\n+import type { ReadView, VisibleLine } from \"../../src/engine/index.ts\";\n+import { StoredText } from \"../../src/engine/stored-text.ts\";\n+import { eachCodeLine } from \"./names.ts\";\n+\n+/**\n+ * Characters of parsed code kept in memory before the rest goes to scratch (abso Capacity). The visible lines it is\n+ * 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\n+ * 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.\n+ */\n+export const DEFINITION_CODE_IN_MEMORY = 4_000_000;\n+\n+let inMemoryLimit = DEFINITION_CODE_IN_MEMORY;\n+\n+/**\n+ * Tests only: the in-memory limit for every later call, so a small file takes the scratch path (abso's kept-reader\n+ * test checks scratch is let go). Answers the limit it replaces, to put back.\n+ */\n+export function setDefinitionCodeInMemory(limit: number): number {\n+\tconst before = inMemoryLimit;\n+\tinMemoryLimit = limit;\n+\treturn before;\n+}\n+\n+/** Parsed code is temporary derived data, discarded with the scratch transaction. */\n+export function withDefinitionCode<T>(\n+\tview: ReadView,\n+\tvisible: readonly VisibleLine[],\n+\toperation: (code: readonly string[]) => T,\n+\tinMemory = inMemoryLimit,\n+): T {\n+\tfunction* source() {\n+\t\tfor (const line of visible) yield line.text;\n+\t}\n+\tif (!view.graph.hasOrderedChildren) return operation([...eachCodeLine(source())]);\n+\tconst iterator = eachCodeLine(source());\n+\tlet prefix: string[] = [];\n+\tlet size = 0;\n+\tlet pending = iterator.next();\n+\twhile (!pending.done && size + pending.value.length + 1 <= inMemory) {\n+\t\tprefix.push(pending.value);\n+\t\tsize += pending.value.length + 1;\n+\t\tpending = iterator.next();\n+\t}\n+\tif (pending.done) return operation(prefix);\n+\treturn view.graph.withProjectionScratch((storage) => {\n+\t\tconst text = new StoredText(storage, \"definition-code\");\n+\t\tlet count = 0;\n+\t\tfor (const line of prefix) text.write(String(count++), line);\n+\t\tprefix = [];\n+\t\ttext.write(String(count++), pending.value!);\n+\t\tfor (const line of iterator) text.write(String(count++), line);\n+\t\tconst cache = new Map<number, string>();\n+\t\tlet characters = 0;\n+\t\tconst read = (index: number) => {\n+\t\t\tif (index < 0 || index >= count) return undefined;\n+\t\t\tconst cached = cache.get(index);\n+\t\t\tif (cached !== undefined) return cached;\n+\t\t\tconst value = text.read(String(index));\n+\t\t\tif (value.length <= 65536) {\n+\t\t\t\twhile (cache.size >= 64 || characters + value.length > 65536) {\n+\t\t\t\t\tconst first = cache.keys().next().value!;\n+\t\t\t\t\tcharacters -= cache.get(first)!.length;\n+\t\t\t\t\tcache.delete(first);\n+\t\t\t\t}\n+\t\t\t\tcache.set(index, value);\n+\t\t\t\tcharacters += value.length;\n+\t\t\t}\n+\t\t\treturn value;\n+\t\t};\n+\t\tlet active = true;\n+\t\tconst code = new Proxy([] as string[], {\n+\t\t\thas(target, key) {\n+\t\t\t\tif (!active) throw Error(\"definition code scope has ended\");\n+\t\t\t\tif (typeof key === \"string\" && /^(0|[1-9][0-9]*)$/.test(key)) return Number(key) < count;\n+\t\t\t\treturn Reflect.has(target, key);\n+\t\t\t},\n+\t\t\tget(target, key, receiver) {\n+\t\t\t\tif (!active) throw Error(\"definition code scope has ended\");\n+\t\t\t\tif (key === \"length\") return count;\n+\t\t\t\tif (typeof key === \"string\" && /^(0|[1-9][0-9]*)$/.test(key)) return read(Number(key));\n+\t\t\t\treturn Reflect.get(target, key, receiver);\n+\t\t\t},\n+\t\t});\n+\t\ttry {\n+\t\t\treturn operation(code);\n+\t\t} finally {\n+\t\t\tactive = false;\n+\t\t}\n+\t});\n+}"}],"unmatched":[]}