GitNexus/gitnexus/bench/http-route-symbols/README.md
Abhinav Pandey d6917308d5
perf(group): load route handler symbols for many files in one query (#3929)
* perf(group): load route handler symbols for many files in one query

What

- Add CONTAINING_FILES_QUERY: CONTAINING_QUERY's three-label UNION ALL
  with `sym.filePath IN $filePaths`, at most 500 files per query.
- The HTTP route extractor's per-file symbol cache now takes a list of
  files it should expect to be asked for. The graph provider pass names
  every file whose HANDLES_ROUTE row carries a handlerSymbolId; after the
  project scans, the files with project-scan detections are named too.
  The first cache miss loads all of them, plus the file asked for, in one
  query and groups the rows by file. A file with no rows caches [], which
  is what its own query returns. A batch whose query fails caches nothing,
  so each of its files falls back to its own CONTAINING_QUERY. A file that
  was never named still gets its own CONTAINING_QUERY, as before.
- CONTAINING_QUERY and CONTAINING_FILES_QUERY are exported so an
  integration test can run both against a real LadybugDB.

The batch has no ORDER BY, the same as CONTAINING_QUERY. Rows are grouped
by file in arrival order, so each file keeps the row order its own query
gives (Function, then Method, then CodeElement rows, each in scan order).
resolveSymbolByName and resolveContainingSymbol depend on that order to
break ties, so it is left as it was.

Why

`filePath` is not indexed, so each CONTAINING_QUERY scans the Function,
Method and CodeElement tables to find the rows of one file, and the
extractor issued one of these for every file it resolved a symbol in.

Results

18 route fixtures from gitnexus/test/fixtures, each analyzed once, with
the extractor of origin/main and of this branch run alternately (one
warm-up, then 3 runs each). Both builds were given the same HANDLES_ROUTE
and FETCHES rows, so they saw the same input order.
- Output: byte-identical in all 6 runs for every repository (87 HTTP
  contracts).
- Symbol queries: 41 per-file -> 11 batched + 7 per-file.
- Median extract time summed over the 18 repositories: 217.8 ms ->
  169.4 ms, of which time in symbol queries 85.4 ms -> 39.5 ms.
- `gitnexus group sync` over a group of the 18 repositories: 108
  contracts and 8 cross-links in every run, and the sorted contracts.json
  is identical across 3 runs per build. The unsorted file differs between
  two runs of origin/main as well, because HANDLES_ROUTE_QUERY and
  FETCHES_QUERY have no ORDER BY.

This repository's own graph at 5c7da84f (3,565 files, 124,815 nodes, 118 HTTP
contracts):
- Symbol queries: 22 per-file -> 2 batched (8 files) + 14 per-file,
  covering the same 22 files and returning the same 681 rows.
- Replaying each build's recorded symbol queries, 7 alternating runs:
  median 197.3 ms -> 145.7 ms.
- The remaining 14 per-file queries are files reached through FETCHES
  rows, which are not named in advance.

Testing

- Unit (http-route-extractor.test.ts): one handler-file batch and no
  per-file queries; a file never named gets its own query; a failing
  batch falls back to one query per file with identical contracts; a file
  the batch has no rows for caches [] with no further query; batched and
  per-file runs resolve the same symbols, including an equal-span tie
  that only row order decides.
- Express stubs that matched on $filePath also answer $filePaths, and the
  failed-lookup test in http-route-graph-method.test.ts now expects the
  failing batch followed by one per-file query, still with no retry per
  route.
- Integration (http-route-symbol-batch.test.ts): real pipeline and
  LadybugDB; for every file, the batch returns exactly that file's
  CONTAINING_QUERY rows in the same order, and the extractor gives the
  same contracts with and without the batch, in fewer queries.
- With the source change reverted, 8 of these tests fail.
- tsc --noEmit; vitest on test/unit/group, test/integration/group and
  the route integration tests: 83 files, 1462 tests passed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* perf(group): enforce linear symbol loading

Bound selective lookups, reuse one symbol snapshot for larger workloads,
and index handler IDs per file. Add a native scaling gate with exact
contract checks and before/after evidence.

Refs #3929

* perf(group): load named symbol files with one hash-join query

Above 500 files the loader switched to a scan that returned every
symbol row of the repository. Row transfer costs far more per row than
the scan, so on a 120,000-symbol database 501 handler files took 9.3 s
against 0.42 s for the 500-file IN batches before it and 6.3 s for one
query per file, and files discovered late paid 500 per-file queries and
then the scan.

Replace both the capped IN batches and the complete scan with one
UNWIND join per group of named files. The engine plans it as a hash
join: one scan per table whatever the length of the list, and only the
named files' rows come back. Every pass now names its files first (all
route files, then every scanned file with a detection), so a healthy
extraction issues two symbol queries. Same shapes after the change:
501 handler files 0.24 s, 501 late files 0.48 s (was 16 s), identical
contracts.

Sort each file's rows on node table and scan offset when they enter the
cache. On a multi-thread connection the engine returns a file's rows in
an order that changes between calls, so the first-match resolvers could
pick a different symbol from run to run; the three load paths now give
one order by construction.

Bench: every fixture holds as many unrequested symbols again and each
run asserts the queries returned exactly the requested files' rows;
returned rows join the gated metrics; median averages the two middle
values for an even sample count. The integration test compares row sets
for every file, adds a mixed-label file with an equal-span tie, asserts
exact query counts, and is registered in the lbug-db project and the
cross-platform list.

Refs #3929

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

* docs(bench): re-measure the route symbol gate at the join loader

Regenerate the committed evidence with the harness of e6fa28fcb from
clean checkouts: baseline 5340562f5 (500-file IN batches) fails the
gate on scanned tuples (9.00) and handler-ID reads (15.94); candidate
e6fa28fcb passes every comparison, with scanned tuples, returned rows
and ID reads at exactly 1.00. RESULTS.md names both commits, records
that the complete-scan revision 2ebbdd79d fails the returned-rows check
at N=1,024, and drops the wording that described 5340562f5 as the PR
head.

Refs #3929

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
2026-11-14 22:06:10 +00:00

5.7 KiB
Raw Permalink Blame History

HTTP route symbol scaling

The initial before/after measurements and their limits are in RESULTS.md.

From gitnexus/, with the package dependencies installed:

node --expose-gc --import tsx bench/http-route-symbols/measure.mjs \
  --check --output /tmp/http-route-symbols.json

The existing serial benchmark CI job runs this command and uploads the JSON, including failed checks. Output paths must not already exist. --root selects another checkout's gitnexus/ package for a comparison; it needs the same dependencies. Each report identifies the revision, working-tree changes, extractor, lockfile, harness and scaling-guard hashes.

The harness calls the production HttpRouteExtractor.extract() with real LadybugDB Function, Method and CodeElement tables. Its executor replays fixed HANDLES_ROUTE rows and an empty FETCHES result, so symbol loading is isolated from route-discovery query order. The repository directory is empty: this is an extractor/symbol-loading benchmark, not an indexing or complete group-sync benchmark. Database creation, imports and correctness assertions are outside the timed operation. Query preparation, native execution, row transfer, cache construction, handler lookup and contract assembly are inside it.

Arm Growing input Expected output
spread N handler files, six symbols per file N exact provider contracts
dense One file, 6N symbols and N handlers N exact provider contracts
sparse N files, six symbols per file, one requested handler One exact provider contract

N is 256, 1,024 and 4,096. All three node labels are populated. Each handler selects the second Function in its unit, so dense lookups cannot pass by always choosing the first row. Every invocation compares the full contract array, including identity, name, file, method and metadata, to an independently generated expectation. Merely preserving the number of contracts is insufficient.

Every fixture also holds as many symbols again in files that no route names (lib/o<n>.opaque). Every invocation checks that the symbol queries returned exactly the rows of the requested files: 6N for spread and dense, 6 for sparse. A loader that reads more of the repository than it was asked for fails the run, whatever its timings.

Each arm/size runs in a fresh child process with one database execution thread and a 128 MiB buffer pool. Two warmups precede seven samples. GC runs before each measured invocation, outside its timing. Reports retain every sample and use medians; there are no retries or removed outliers. Heap and RSS are measured at the end of extraction. Process maximum RSS also includes fixture construction and startup. These absolute memory measures include fixed runtime overhead, which can mask small allocation changes.

An untimed instrumented invocation also records:

  • Actual scanned tuples from each symbol query's native PROFILE plan. The complete plans are saved. Missing or unrecognized scan counters fail closed.
  • Symbol-ID property reads through proxies around the real returned rows. This detects repeated array searches within one file even when native row transfer dominates elapsed time. Timed invocations do not use these proxies or PROFILE.

The shared callable-declaration scaling guard enforces a 1.5 ceiling on (large cost / small cost) / (large N / small N) for every adjacent pair and the whole 16× span. It gates wall time, CPU, heap, RSS, maximum RSS, scanned tuples, returned rows and ID reads. Invalid measurements fail. --check requires all arms, the reviewed sizes and at least seven samples. Overrides without --check are diagnostics. These bounded checks detect regressions; they are not a proof of linear complexity for every source-scan, name-resolution or database-failure path in the extractor.

Implementation bound

Each pass of the extractor names the files it is about to read: the route files before the graph provider pass, then the scanned files with detections. The first lookup after that loads every named file with one query, which joins the three symbol tables to the list of paths. The engine plans that as a hash join: one scan per table whatever the length of the list, and only the named files' rows are returned and kept. A healthy extraction therefore issues two symbol queries, and its work grows with the size of the tables plus the rows it needs.

Two alternatives were measured and rejected:

  • filePath IN $filePaths is a LIST_CONTAINS filter, which walks the list for every scanned row. Capping the list at 500 files bounds that work per query and repeats the table scans once per 500 files.
  • One scan of every symbol row has no list, and returns the rows of every other file in the repository as well. Row transfer costs far more per row than the scan does, so that was slower than one query per file as soon as the requested files were a minority of the symbols.

A file's rows are sorted on their node table and scan offset before the first-match resolvers read them, so the result does not depend on which query loaded the file or on the order the engine delivered the rows. ID maps are built once per requested file, keeping the first row for a duplicate ID.

If the batch query fails, each of its files falls back to its own per-file query and the healthy-path bound no longer applies. One query returns all the named files' rows, so its duration grows with them; the per-file fallback also covers a batch that exceeds the pool's query timeout. The focused regression tests cover the fallback, files named by each pass, missing files, row order and first-row behavior.