Versions 154 to 318
new file, 90 lines
1+import type { ReadView, VisibleLine } from "../../src/engine/index.ts"; 2+import { StoredText } from "../../src/engine/stored-text.ts"; 3+import { eachCodeLine } from "./names.ts"; 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. 10+export const DEFINITION_CODE_IN_MEMORY = 4_000_000; 12+let inMemoryLimit = DEFINITION_CODE_IN_MEMORY; 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. 18+export function setDefinitionCodeInMemory(limit: number): number { 19+ const before = inMemoryLimit; 20+ inMemoryLimit = limit; 24+/** Parsed code is temporary derived data, discarded with the scratch transaction. */ 25+export function withDefinitionCode<T>( 27+ visible: readonly VisibleLine[], 28+ operation: (code: readonly string[]) => T, 29+ inMemory = inMemoryLimit, 32+ for (const line of visible) yield line.text; 34+ if (!view.graph.hasOrderedChildren) return operation([...eachCodeLine(source())]); 35+ const iterator = eachCodeLine(source()); 36+ let prefix: string[] = []; 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(); 44+ if (pending.done) return operation(prefix); 45+ return view.graph.withProjectionScratch((storage) => { 46+ const text = new StoredText(storage, "definition-code"); 48+ for (const line of prefix) text.write(String(count++), line); 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>(); 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; 65+ cache.set(index, value); 66+ characters += value.length; 71+ const code = new Proxy([] as string[], { 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); 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); 85+ return operation(code);