Skip to content

Latest commit

 

History

History
166 lines (119 loc) · 24.7 KB

large-file-performance.md

File metadata and controls

166 lines (119 loc) · 24.7 KB

Large-file indexing performance

Read this before changing a loop in src/extraction/, src/resolution/ or codegraph-kernel/, before reviewing a pull request that does, and before answering an issue that says indexing hangs, is slow, or left a file with no symbols.

The rule

Indexing a file costs in proportion to its size. One megabyte of ordinary code takes about a second. Nothing done for a symbol, a reference, a route or a match may read the whole file, all of the file's nodes, all of its imports, or everything before it.

Until November 2026 that was false in every language. Time grew with the square of the file: a 0.9 MB Python file took 27 seconds and a Ruby one 54, a 1.2 MB Express file of inline handlers 58, 1.2 MB of Liquid 41. A file is given a time limit (the parse budget in src/extraction/parse-budget.ts, 10 to 20 seconds a parse, and the limits above it), and past it the file is given up on. So the cost was not only time: a large file ended with no symbols, and nothing a user asked about it (callers, impact, its own symbol map) had an answer. The size limit could not be raised from 1 MB to 4 MB until this was fixed (MAX_SOURCE_FILE_SIZE_BYTES in src/file-limits.ts).

About forty causes were removed, and every one was the same shape. No single one is obvious in review, because each looks like ordinary code that is fine on a 300-line file.

The shape, and what to write instead

Each row is something that was in the code. The left column is what to look for in a diff.

What it looks like Why it grows with the square Write this
text.slice(0, i).split('\n').length, (text.substring(0, i).match(/\n/g) || []).length, or a loop counting newlines up to an offset A pass over the text for each match. It was in more than fifty route and handler scans. lineAt(text, i) in src/resolution/synth-utils.ts. In an extractor class, a table of line starts built once (see LiquidExtractor.lineStarts).
content.split('\n') or split(/\r?\n/) inside something called for each reference The file is split again for each call. linesOf(text) in synth-utils.ts.
context.getNodesInFile(f).filter(n => n.startLine <= line && n.endLine >= line …), also with find, some or a for loop Every node of the file, for each reference, and a reference asks several such questions. Nearly forty places. nodesAroundLine(context, f, line) in src/resolution/nodes-around.ts, then the same test as before. With the nodes already in hand, nodesAroundLineIn(nodes, line).
getNodesInFile(f).find(n => n.name === x …) The same, by name. A routes file did it to its controller once per route. nodesNamed(context, f, x) then the same test. Also nodesNamedIn, nodesNamedLower, nodesUnder, and context.getNodesInFileNamed.
imports.find(m => m.localName === name) or imports.filter(m => m.source === s) for each reference Every import of the file, for each reference. importsBoundTo, importsFrom, importsReadBy, pythonMemberImports in src/resolution/import-lookup.ts.
this.nodes.find(n => n.id === id) in the extractor Every node extracted so far, for each member. The extractor's nodesById map.
for (let i = 0; i < node.childCount; i++) node.child(i) and the namedChild(i) form; in the kernel node.child(i) and named_child(i) Tree-sitter reaches child i by stepping from the first child when the children are a flat run, as a thousand comment lines or a thousand statements are. The loop is the square of the run. Reading every child: for (const c of node.namedChildren). Leaving early: childrenOf(node) or namedChildrenOf(node) in src/extraction/tree-sitter-helpers.ts. Kernel: for c in node.named_children(&mut node.walk()).
Finding where a declaration stands among its parent's children, to look at the siblings before it (decorators, a visibility marker) Counted from the first child for each declaration. Walk back from the node: previousNamedSibling, prev_named_sibling(). Where the answer depends on everything before (Ruby visibility, C++ access), keep the last answer per parent.
A column counted character by character from the start of its line (kernel) A script minified to one line pays the line's length for each symbol. textutil::LineStarts and col16 in codegraph-kernel/src/textutil.rs.
new RegExp(…name…) built and run over the file for each symbol The file, for each symbol. Read the names once per file into a set and look each up (laterExports, jsCodeWordSites, wordsWrittenBare).
source.indexOf(name, start) or source.includes(needle) for each item A scan for each item. A name declared late is found late. Many names in one text: firstIndexOfEach(text, names) in synth-utils.ts. One fact about a file: ask once per file and keep it (fileContains in src/resolution/index.ts, moduleDefaultStated in name-matcher.ts).
pattern.test(code.slice(functionStart, callOffset)) for each call in a function The function is read from its top for each call. Find where the first match in the function ends, once, and compare offsets (shadowed in tauri-synthesizer.ts).
"Does this function bind this name by this line?" answered by reading the function from its first line The same, for each reference. Read the function once and answer for any line (JsNameBinding, pythonStatementBinders, pythonScopesAround). A scan that keeps its place must answer an earlier line from what it kept, not start again.
Something done for each function around a line, for each match on that line: a for over the functions that hold it, enclosingFn for every site, the nodes around it mapped to a list A script minified to one line has every function of the file around every call on it, and "that function's source" is the whole file. A 0.4 MB build of echarts with 51 listeners took 37 seconds, and one of 0.7 MB six minutes (#3501). Keep the answer by line (enclosingFn, fnChain, referenceSources), and what a stretch of lines says of a name by its lines and that name (handlerDeclaration, localBindingIn): nodes on the same lines have the same source. Go through the functions on the same lines once (sameLines), and read what the nodes around a line resolve a name to once for the line (resolvedOnLine).
for (const a of emits) for (const b of ons) where the test inside cannot tell most of them apart Every pair of sites, though a thousand of them are one dispatcher on one object. The first of each group the test treats alike, in the same order (firstOfEach).
A group meant for a name that also reads a keyword: function\s+(\w+)|(\w+) Not a square by itself. on('e', function (x) {…}) gave the name function, which nothing declares, so every function around the call was read to its end looking for it. Refuse the words that are no name before looking (NOT_A_NAME).
scopes.filter(outer => holds(outer, start, end)) for each route Every route, for each route. Keep them by stretch of the source and by path (scanRoutes in frameworks/react.ts).
A pattern such as [\w.]+ given a megabyte of base64 or hex The pattern starts a name at every character of the run and reads to its end: 30 KB cost a second. The resolver reads files through withoutLongRuns (src/file-limits.ts). Read through context.readFile, never around it.
target.push(...items), Math.max(...values) on a list as long as a file's imports or symbols Not slow. It throws "Maximum call stack size exceeded" past about 125,000 items. pushAll, maxOf, minOf in src/utils.ts.

Two more things that are not a line of code:

  • The same code exists twice. The native kernel (codegraph-kernel/) and the wasm extractor (src/extraction/tree-sitter.ts, src/extraction/languages/) extract the same languages. A fault fixed in one is usually in the other. The kernel reads 17 languages; the wasm extractor reads the rest, and all of them when the kernel is absent or CODEGRAPH_KERNEL=0.
  • Frameworks and bridges are their own passes. src/resolution/frameworks/*.ts and the synthesizers read a routes file or a bridge with patterns, and link each route to its handler. They had the fault as much as the extractors, and a benchmark of plain functions never reaches them.

Rules for a lookup that keeps an index

The helpers above keep an index per file. A new one follows the same rules, and __tests__/large-file-lookups.test.ts is where it is pinned against the expression it replaces.

  1. Same answer, same order. The helper returns what the old expression returned, or a superset in the file's own order that the caller filters exactly as before. nodesAroundLine and nodesNamed are supersets on purpose: the caller's test is unchanged, so its answer is.
  2. Keyed by what the context hands out. A WeakMap on the array getNodesInFile or getImportMappings returned, or a map by context and file. Never a module-level map by file path alone: a resolver outlives one sync.
  3. Dropped with the caches. A memo in name-matcher.ts is deleted in clearNameMatcherMemos, and __tests__/name-matcher-memo-clears.test.ts fails if it is not. A memo in ReferenceResolver is cleared in clearCaches.
  4. Small inputs take the old path. Under a few dozen items the plain pass is as quick, and it keeps ordinary files on the code that has always run. Test both sides of the threshold.
  5. It changes nothing else. One of these fixes changed what is read (withoutLongRuns); every other is the same answer reached without reading again. Keep a speed fix and a behaviour change in separate pull requests.

A node for each of something

Time is not the only cost. A node is about a kilobyte of index (its row, the seven indexes on it, its line in the text index), a contains edge, one more name for every search that falls back to reading names, and one more candidate for every reference of that name.

Before an extraction gives a node to each of something a project can hold by the hundred thousand (the members of an object literal, the rows of a table, the keys of a locale file), measure it these ways. #3408 is the worked example: it set out to give every member of a long object literal a node, and ended with a node for the members somebody wrote a comment for, which is what could not be found before.

  1. Many small files, not only one large one. With a node for every member, one large file showed nothing: 0 to 4% at every size. A project of a thousand small settings files grew to seventeen times its index (5.5 to 96 MB) and took three times as long to build, and a query that matched nothing went from 3 to 28 ms.
  2. Count them on real code. Index a real project before and after and count the new kind (SELECT kind, count(*) FROM nodes GROUP BY kind). The form that was kept adds 98 nodes to this repository's own source.
  3. Every edge that was there is still there. scripts/dump-graph.mjs before and after, on real code, on both extractors. A node of a new kind is offered to the resolver as one more node of its name: a settings field named URL took new URL(…), and one named ParseError halved the confidence of every new ParseError(). A node that exists to be searched, not referred to, is kept out of the resolver's lookups, out of a sync's list of changed definitions, and out of the dead-code report (src/resolution/object-members.ts).
  4. What it costs where there is nothing to find. Most literals hold no comment. Visiting every member to learn that made a plain object of 100,000 values 16 to 35% slower to index, for no node. Ask the text first: the offsets where a comment could begin are read once for the file (commentMarks), and a literal or a member with none in its stretch of the source is left without a visit.
  5. What an answer does with it. codegraph_explore walks out from each match, and the walk read every edge and every neighbour of a node to keep a few. One field of an object of 10,000 cost 50 ms, all of it reading the other 9,999, and so did any question that landed on the object. The walk now fetches a step's neighbours a batch at a time and only while it can still keep one, and reads the edges of a wide node by the kinds it keeps: a node's id begins with its kind, so those edges are a few stretches of the edge index (getOutgoingEdgesToKinds, __tests__/node-id-kind.test.ts). 50 ms became 6, and a walk out of a class of 10,000 methods went from 59 ms to 21. A match that is itself a member is not walked at all; what holds it is added instead.
  6. What a search does with it. A word of a query visits every row of the text index that holds it, and a member's row holds its owner's name. An object of 10,000 documented fields adds about 5 ms to a question that names the object, and a class of 10,000 documented methods costs 12 to 27 ms for the same questions. That is the floor without a change to the text index, and it is why a node for every member was not affordable.

A lookup made as a last resort, when nothing else matched, reads the text index only (SearchOptions.indexedTextOnly). The substring scan and the nearest-name guess a search falls back to read every name, and a pasted log is fifty words that match nothing.

Measuring

The benchmarks are in scripts/benchmarks/large-files/. Each takes a built engine and a size in megabytes, writes one generated project a case, indexes it, and marks what is over 2 seconds a megabyte.

npm run build:kernel && npm run build

# The 17 languages the kernel reads, about 24 shapes each (317 cases)
node scripts/benchmarks/large-files/kernel-languages.mjs dist 1.2
node scripts/benchmarks/large-files/kernel-languages.mjs dist 3.9 python,ruby mixed,imports

# Everything else the README lists: the other languages, each framework's
# routes file, each bridge (96 cases)
node scripts/benchmarks/large-files/other-languages.mjs dist 1.2
node scripts/benchmarks/large-files/other-languages.mjs dist 3.9 express,rails

# The wasm extractor, which is what a machine without the kernel runs
CODEGRAPH_KERNEL=0 node scripts/benchmarks/large-files/other-languages.mjs dist 1.2

How to read them:

  • Run three sizes: 0.3, 1.2 and 3.9. A linear case takes four times as long for four times the size. One that takes ten to sixteen times as long has the fault, even when the small size looks fast. Under about half a megabyte the fixed cost of starting hides everything.
  • Read the counts, not only the seconds. A case that yields no symbols, routes or edges gave the engine nothing to link and measured nothing. A file over the size limit is left out by the engine and looks very fast; the harness marks it NOT MEASURED.
  • The shape decides. A benchmark of only functions, or only calls, hid most of the faults. The shapes that found them: functions that each declare a local and call a helper and the function before them (mixed), methods calling each other through the object (members), a table of rows, one statement that is the whole file (object, switch, enum), a script on one line (minified), one long string (blob, hex, dots), some thousand imports (imports), a long run of comment lines (commentrun), and for a framework its routes file together with the controller it names. And a script on one line that holds what a pass looks for (js/events-minified): the minified shape, functions that call each other on one line, never reached the pass that links an event to its listeners, which is where one-line bundles hung (#3501).
  • Start from the README's tables, not from the list of parsers. The first pass measured the kernel's 17 languages and called it every language. The other 18 language rows, the frameworks, the routers and the bridges had the same fault. A new language, framework or bridge gets a case in other-languages.mjs in the same change.

Where things stood when this was written (November 2026, one Mac): the 317 kernel cases at 3.9 MB average 2.1 seconds, the slowest 12 (a C++ file of 130,000 #include lines). The 96 other cases average 3.9 seconds with the largest file of each just under 4 MB. Cases around 2 seconds a megabyte that are linear, with 120,000 or more symbols in one file: Nix bindings, Elysia chains, NestJS controllers.

Finding the cause

node scripts/benchmarks/large-files/profile.mjs other-languages dist 1.2 express/inline
node scripts/benchmarks/large-files/profile.mjs kernel-languages dist 1.2 python mixed

It prints the lines with the most CPU samples over every thread, with each line's text from the build. Profile at a size where the case takes several seconds: the faulty line is then most of the samples, and it is nearly always one line. Fix it, run again, and the next one surfaces; a case often has three or four behind each other.

A line named tree-sitter.wasm is time inside the wasm parser, and the kernel's time shows on the one line that calls kernel.extractFile: neither is in the engine's TypeScript loops. For the kernel, build it with symbols and sample the process with the system's profiler to see which Rust function it is.

Proving nothing else changed

A speed fix must leave the graph as it was, and the check is mechanical. Build the engine before the change (a git worktree of main, built) and after it, index the same projects with both, and compare what scripts/dump-graph.mjs prints: every node, edge, unresolved reference and file.

node scripts/benchmarks/large-files/compare-graphs.mjs ../main/dist dist __tests__/fixtures src
CODEGRAPH_KERNEL=0 node scripts/benchmarks/large-files/compare-graphs.mjs ../main/dist dist __tests__/fixtures src

# The benchmark's own projects, small, one a case
BENCH_WRITE=/tmp/cases node scripts/benchmarks/large-files/other-languages.mjs dist 0.06
node scripts/benchmarks/large-files/compare-graphs.mjs ../main/dist dist --each /tmp/cases

Compare over, at least: __tests__/fixtures, this repository's src, real projects in the languages the change touches, the benchmark's projects written with BENCH_WRITE, and a small project written for each decision the faster path makes (a nested route, a name that is part of another name, Windows line endings, a file with no newline at its end). Run it with the kernel and with CODEGRAPH_KERNEL=0. A difference is a bug in the fix until shown otherwise.

Then the tests. Four files pin this, and a fix adds to the one that fits:

Test file What it pins
__tests__/large-file-indexing.test.ts The kernel's languages: shapes of real large files at 1 to 3.9 MB, each bounded in time and checked for what the faster path decides.
__tests__/large-file-frameworks.test.ts The other languages, routes files and bridges, the same way.
__tests__/kernel-large-file-parity.test.ts The kernel and the wasm extractor agree on files of some thousand declarations.
__tests__/large-file-lookups.test.ts Each lookup against the expression it replaced, on both sides of the size where it starts keeping an index.

A test that only bounds the time is half a test. Each case also asserts something the faster path could get wrong: the decorator on the right declaration, the route on its own handler and no other, the call a local binding hides left unlinked.

Traps that cost a run

  • Run the suite without CODEGRAPH_NO_DAEMON. The benchmark scripts set it for themselves. In the environment of npm test it fails exactly the daemon tests (38 tests in 7 files), which looks like a regression and is not.
  • The test setup rebuilds dist when src is newer. Do not run tests while a benchmark is reading dist, and do not edit src while the suite runs: the tests that start the built CLI fail.
  • The first case pays for loading the engine. The harnesses index a small project first; a timing script of your own must too.
  • A hand-written expectation is often wrong. Ask the unchanged engine what it yields for a small input before asserting it. Things that surprised: a bare Ruby private is not read as a visibility (only private :name), a Kotlin @Deprecated("x") yields no decorates reference, a Python decorator written as a call is a calls reference.

Known and left as it is

  • Tree-sitter's own error recovery. Where a grammar cannot parse a long run of lines, the parser's recovery takes the square of the run, inside the parser, and a profile shows it as tree-sitter.wasm (ts_parser__recover, ts_subtree_summarize_children), not as a line of the engine. No loop of ours can fix that. The cure is a preParse rewrite that hands the parser something it can read, at the same length (see blankLoneMacroLines), and that changes what the parser makes of every such file: it is a behaviour change, with its own before-and-after comparison on real code, not a speed fix. One case was found and cured that way: React Native's shim for a Swift module, @interface RCT_EXTERN_MODULE(…) over lines of RCT_EXTERN_METHOD(…), where 16,000 such lines took 7 seconds and 32,000 ran out the parse budget (src/extraction/languages/objc-rn-extern.ts). Expect others wherever a file is mostly macros the grammar does not know.
  • Memory. Memory per megabyte did not change, so a 4 MB file can take four times what a 1 MB file did while it is indexed. Fourteen real files of 1 to 3.8 MB peaked between 0.15 and 0.7 GB; a 3.8 MB bundle peaked at 1.8 GB.
  • Linear, and heavy. A file of 120,000 symbols takes 8 seconds at 3.9 MB. That is its size.
  • A handler's declaration is looked for with a pattern built from its name (handlerDeclaration, localBinding in event-emitter-synthesizer.ts), once for each stretch of lines and each name. A one-line script that registers thousands of handlers under thousands of different bare names would read the file once for each. Minified code passes a few short names again and again, and no bundle measured has that shape. If one turns up: read every name the patterns can match from a stretch of lines once, and look names up.
  • What a one-line script's event links mean. The pass reads scope by line, so on one line the function an emit is "in" is the first function of the file. The links it makes there were as arbitrary before; this work made them quick and left them as they were, because a speed fix changes no graph. Leaving minified files out of the pass, as constAliasEdges leaves generated ones out, is a change of behaviour for a pull request of its own.

When an issue says indexing is slow, hangs, or a file has no symbols

  1. Get the file's language, its size, and what it mostly holds (see the shapes above). Ask for codegraph status: it names files left out for their size, and an index run reports a parse that timed out.
  2. Check the report's date against the release that carries these fixes (see the house rule on anchoring reports in AGENTS.md). Before that release, every large file was slow.
  3. Run the file the report names, when it names one. #3501 gave a public file and its checksum, was answered from the release notes ("patched in 1.6.5"), and was not fixed: that release's benchmark had no case of its shape, and the file took 36 seconds where it had taken 39. CODEGRAPH_SYNTH_TIMINGS=1 names the pass.
  4. Reproduce with a generated file of that shape at that size. If no case in the harnesses has the shape, add one: that is the first half of the fix.
  5. Time it at three sizes. Four times the size for four times the time is not this fault: look at the parser (profile) or at the project (many files, not one large one).
  6. Profile, fix the line, compare the graphs, add the test. Then profile the generated case again: the report's file showed one pass, and the generated one showed a second with the same fault behind it.

When a pull request touches extraction or resolution

  • Look in the diff for the left column of the table. A new getNodesInFile(...).find, a new split('\n'), a new index loop over children, a new pattern built from a name: each is fine at 300 lines and seconds at a megabyte.
  • Ask what the new code does for each symbol, reference or match, and whether that depends on the file's size.
  • A new language, framework or bridge comes with a case in other-languages.mjs, timed at 1.2 and 3.9 MB.
  • A new kind of node, or a node for something that had none: see "A node for each of something". Count them on real code and on a project of many small files before reading any timing.
  • Run both harnesses on the branch and on main for the languages it touches, and compare-graphs.mjs if it claims to change nothing.
  • The regression gate (npm run gate, scripts/regression/README.md) runs both harnesses at 0.3 and 1.2 MB and fails a case that takes more than eight times as long for four times the size, and it indexes the real files that reports named against a time budget. Run it before opening a pull request; nothing runs it for you. It is the floor, not the measurement: it has the shapes in the harnesses and the files in its list, so a new shape still needs its case.