Skip to content

perf(ts): do per-directory analysis work once per directory - #417

Merged
aronstrandberg merged 25 commits into
mainfrom
aronstrandberg/ts-test-analysis
Dec 2, 2026
Merged

aronstrandberg merged 25 commits into
mainfrom
aronstrandberg/ts-test-analysis

Conversation

@aronstrandberg

@aronstrandberg aronstrandberg commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Make ts_test and ts_compile do their per-directory work once per directory during analysis, where they did it once per file. One analysis of the Lovable monorepo's 1,045 TypeScript programs allocates about 40% less and spends about a third less Starlark CPU, and every action, argument, written file and runfiles entry stays the same.

Claude Fable 5.2, max

Why. A Starlark CPU profile of that analysis puts 79% of the time in _ts_test_impl. For each test it walks every module of the closure and works out where the module sits at run time, which package.json scope it falls under and which npm packages it may import. Those answers depend on the module's directory, not on the module. The largest test has 22,185 modules in 3,041 directories, and the 2.15 million module pairs that reach the scope projection lie in 242,048 pairs of source and runtime directory. In places the same runfiles path was also built three or four times.

What changes. Thirteen passes, one commit each, each keeping its outputs and their order:

  • compile_program: the layout check skips pairs whose source and output are one File. The scope projection is computed once per pair of directories, and its two passes share what the first derived. An index that holds one scope directory answers "is it above this directory" with a prefix comparison.
  • ts_test: the scope requirement, the comparison of staged scopes and the placement of ancestor scopes run for the first module of each directory, or pair of directories. _runtime_demands builds the runfiles path of an ordinary File once.
  • Launcher and vitest config: the check for an opaque entry above a path remembers the directories it has cleared. Hidden runfiles paths are found per directory, without sorting every path. A module reference whose three Files are one builds one path.
  • npm lookups: the modules of one directory share one lookup, and a directory at which the walk does nothing takes the bindings of the directory above it.

Where a check now runs for the first module of a directory, a failure still names the first module that fails. The commits after the passes are the review's: one shape for the three directory keys, and the tests below.

Benchmarks. One analysis, --nobuild, every target configured again, no package loaded, Bazel 9.3.3. CPU-seconds are from --starlark_cpu_profile, in two pairs of runs taken minutes apart on a shared machine; they move with its load. The allocation does not.

1583e19a this branch change
Allocation, the two pairs 121.5 / 121.3 GiB 70.1 / 72.4 GiB -42% / -40%
Starlark CPU, first pair 359.5 s 215.6 s -40%
Starlark CPU, second pair 351.1 s 238.3 s -32%
_ts_test_impl, the two pairs 293.0 / 283.1 s 155.6 / 176.6 s -47% / -38%
_ts_compile_impl, the two pairs 39.1 / 38.8 s 25.4 / 29.4 s -35% / -24%

Per function, first pair, cumulative seconds: runtime_scope_destinations 44.4 to 6.8, runtime_npm_contexts 48.1 to 17.8, rlocation_path 26.1 to 5.3, require_runtime_scopes 23.8 to 6.3, _nearest_scope 15.6 to 2.6, _runtime_scope_data 44.7 to 17.6, validate_runfiles_modules 12.7 to 4.7. Over 13 and 20 runs of the first push, which had one more pass, the median allocation went from 118.7 to 69.8 GiB.

Why I say no action changes.

  • bazel aquery over every rules_typescript target reachable from the monorepo's 2,316 TypeScript targets, at 1583e19a (the commit the monorepo pins) against the same commit plus this branch's commits, canonicalised to one line per action: 41,675 actions equal in every field, file write contents and param files included, and 614 runfiles manifests equal by action key, inputs and outputs. aquery cannot print a manifest whose runfiles hold a declared symlink that is not built. main differs from the pin in none of the changed files.
  • The same comparison over this repository's //tests/...: none of the baseline's 17,862 actions and 885 manifests changes, and the 94 actions and 11 manifests that are new belong to the new test targets.
  • A real build and test run of the monorepo: the validation build of the 1,045 programs and its 246 ts_test targets at the baseline, then with the branch. With the branch the build found 25,384 actions in Bazel's action cache and ran nothing, and the test run found 28,440 and ran only the two tests that fail on that checkout at the baseline too, which Bazel does not cache.
  • //tests/... with --config=fast on NixOS: 581 pass, the nine new tests among them. The 31 that fail to build there fail without the change too (Cargo build scripts and a protoc mismatch on that machine), //tests/dev_server among them. CI covers those and macOS.

Three times during these checks a print differed in one action and was equal when repeated: aquery --output=proto prints targets in parallel and can leave one param file out of one action. The last of the three is identical once that field is left out, and printing the baseline alone six times shows the same.

Review. Three reviewer agents tried to break the claim on the first push, each on one group of passes. They ran the old code and the new side by side on generated inputs, about 1.4 million comparisons, with every fail turned into a value so that the message and the module it names compare too, and they ran real ts_compile and ts_test subjects under both. They found one difference, in a pass that is no longer here: building held_mappings only for a moved layout stopped the accidental rejection of an owner record outside TsInfo's documented shape (a Label as label, or None as a runtime File). I dropped that pass; #416 lists it as a candidate that needs a real check of the record.

They also found that the tests did not hold the passes in place. I made each directory key too coarse, one at a time, and ran //tests/...: of 13 such changes, 10 passed every test. With the tests of this PR, 20 of 21 fail a test: those 13, the five conditions of npm_directory_lookup, the two of the single-scope answer and a lookup that gives up after three directories. The one that still passes is the source directory in the key of the ancestor scope links.

Tests.

  • //tests/starlark:runfiles_paths_tests: the new directory resolution against the sort it replaces, over every subset of twelve files that cover the ancestor relations up to five directories, with and without symlink entries. Two subjects call validate_runfiles_modules with an opaque entry at the top of a module's path, and beside a directory found clear earlier.
  • //tests/starlark:runtime_scope_tests: every single scope and one directory with two scopes against the linear scan. runtime_scope_projection and runtime_scope_targets against one run per module, over every subset of nine module pairs in both orders. Two subjects call require_runtime_scopes with a module that passes and one that fails and shares a directory with it.
  • //tests/starlark:npm_lookup_tests: npm_directory_lookup against the walk of npm_directory_bindings from every directory at, above, beside and beneath 14 links, for each link alone, each pair and all of them. Three of its five conditions never decide anything for the links a lockfile produces, so no real repository can show they are right. And runtime_npm_contexts for one source directory in two runtime directories.
  • //tests/vitest/ancestor_scope/roots/pairs:pairs_test moves one source directory under two runtime directories, and //tests/vitest/config_srcs:config_scope_change_reaches_each_directory_test stages a config scope over modules of three directories.

Not covered. The action comparison ran on Linux x86-64 with Bazel 9.3.3 only. The source directory in the key of the ancestor scope links has no test: it needs two packages that emit into one runtime directory, and I found no such fixture. The reviewers' side-by-side runs were on the first push; the changes since rest on the action comparison and the tests above.

What is left. Most of the remaining analysis cost is output with one entry per module, written once per test. Shrinking it changes actions, so it is a design question: #416.

_program_layout asks of every dependency pair whether the module path of its
source differs from the logical path of its output, and builds three path
strings per pair to answer. When source and output are one File, both are that
File's logical path and the answer is no. In the lovable monorepo that is
2,147,256 of 2,152,067 pairs.

Leave those pairs out of the any(). The result is the same bool.
_runtime_scope_data asked runtime_scope_destinations for every module pair of
the closure, and _runtime_scope_targets walked the scope ancestors and joined a
destination path for every pair again. Both answers depend only on the source's
directory and workspace root and on the runtime File's directory: every module
of one source directory placed in one runtime directory reaches the same
destinations and has the same prefix lengths.

Compute each answer once per such pair of directories and reuse it. What stays
per module is the {source, runtime} entry it adds to a scope's targets, in the
same order as before. In the lovable monorepo 2,152,067 module pairs lie in
242,048 directory pairs.
require_runtime_scopes walked every module's runtime directory up to the root,
joining two paths at each level, and asked runtime_scope_destinations for
every module. Each of its checks reads only the source's directory and
workspace root and the runtime path's directory, never the file's own name, so
every module of one source directory at one runtime directory passes or fails
alike.

Check the first module of each such pair of directories and skip the rest. A
failure still names the first module that fails. The largest test of the
lovable monorepo has 18,134 scoped modules in 3,041 directories.
After config staging, ts_test looked up the nearest package.json of every
scoped module twice, before and after, joining one path per ancestor directory
each time. The nearest scope of a module is the nearest scope of its
directory.

Compare the first module of each directory and skip the others. A failure
still names the first module whose scope changes.
_ancestor_scope_links asked runtime_scope_destinations for every scoped
module and walked the module's runtime directory against the present scopes.
Where a scope has to go depends on the source's directory and workspace root
and on the module's runtime directory, not on the file, and placing a scope a
second time at the same coordinate writes the entry it already holds.

Place from the first module of each such pair of directories and skip the
rest. The placements, their order and the failures are the same.
_runtime_demands built the runfiles path of every module four times and of
every ordinary runfiles File three times, a fourth time in its caller: about
170,000 rlocation_path calls for the largest test of the lovable monorepo,
which has 22,185 modules and 28,304 runfiles Files.

Keep the path beside each ordinary File, fill the module paths in the loop that
already visits each module, and read both wherever a path was rebuilt. The
link path of a module is now built only when the module is no ordinary File,
the one case that reads it. Every list and dict holds the same entries in the
same order.
validate_runfiles_modules split every demanded path and joined one prefix per
ancestor directory to look for an opaque entry above the path. The demanded
paths of a test share their directories: 22,964 paths in 3,492 directories for
the largest test of the lovable monorepo.

Walk up with rpartition and remember each directory that has no entry at or
above it, so a later path in or beneath it stops there. The first path with an
entry above it still fails, naming the same entry.
_beneath_another sorted every runfiles path by its list of components to find
the paths with an ancestor among the paths: one split and a list comparison per
path and sort step, 30,133 paths for the largest test of the lovable monorepo.

Resolve each directory once instead: it is covered when it or a directory
above it is itself a path, and a path is beneath another exactly when its
directory is covered. Callers only test membership in the result, so its order
does not matter.
For every module that can import, _npm_lookup_demands built the module's
runfiles path, asked runtime_scope_destinations for its nearest package scopes,
built two dicts and formatted one key per anchor; runtime_npm_contexts then
asked runtime_scope_destinations again and looked the scope's manifest up for
every module. The largest test of the lovable monorepo has 17,666 such modules
in about 3,000 directories.

A module's lookup directory and nearest scopes are those of its source
directory, so the modules of one directory of one owner now share one lookup
tuple, key strings included. The nearest scopes come straight from
runtime_scope_ancestors, which is all the old call's keys were. The package
scope of a context depends on that tuple and on the module's runtime
directory, so it is resolved once per such pair and the contexts share the
dict. The contexts, the binding sets, their order and the inputs are the same.

A directory source, and a file at the runfiles root, is its own lookup
directory and keeps a tuple of its own.
runfiles_visible_paths drops every runfiles path beneath another entry. It
found those by sorting all paths by their components; it now resolves each
directory once. The test runs both over every subset of ten files that cover
the ancestor relations (a directory entry over files and over another entry,
a sibling sharing a name prefix, another repository, a path with no
directory), with and without symlink entries over them, and compares the
visible paths, order included. It passes on the sorting implementation too.
Every target builds fresh scope indexes, and runtime_scope_ancestors filled
each one's cache with a walk per source directory: about a million walks over
the lovable monorepo's 1,045 programs. Most of those indexes hold the scopes of
one directory, the importer's package.json, and many hold none.

With one scope directory, a directory has it above or not, which a prefix
comparison answers; with none, nothing is above. Both return what the walk
returned. The test now also runs every single scope and one directory with two
scopes against the linear scan.
_runtime_scope_data and _runtime_scope_targets each derived, for every module
pair, the directories that decide its scopes: two attribute reads, two
rpartitions and a tuple, twice over 2.1 million pairs in the lovable monorepo.
The second pass also looked up each scope's target list anew for every pair.

The first pass now keeps each pair's projection with its two paths, and the
second reads them. A projection holds its scopes' target lists themselves, so a
module appends to them without another lookup. Each list gets the same entries
in the same order.
vitest_config_action built three runfiles paths for every module reference:
the source's, the runtime File's and the selected File's. For a source that is
published as itself and not replaced by the runner they are one path, and that
is 99.8% of the 1.16 million references of the lovable monorepo's vitest
tests.

Build the source's path, and build the other two only for a File that differs.
MODULE_REFERENCES holds the same strings.
Each owner resolved the npm bindings of every lookup directory with its own
walk up the importer chain, built one binding dict per directory, and
runtime_npm_contexts turned each into a sorted tuple to find the directories
with equal bindings: three passes over about a hundred names for each of some
3,000 directories of a large test.

_npm_directory_bindings does nothing at a directory that holds no link, hides
no name, is no declared path and lies in no node_modules directory, and then
continues exactly as from the directory above; it skips a node_modules
directory outright. Such directories now take the bindings and the key of the
directory above, so only the others run the walk. A key names a lookup for
the binding sets and for the dev server's verdicts, where one key for equal
lookups of one owner gives the same sets and the same verdicts.
The lone-scope branch made the function long enough for buildifier's
function-docstring warning. Say what the index is.
…ath function

The three passes that run once per placement each built their key their
own way: with the source's separator, without it, and with the runtime
directory as a pair. A placement is now (source workspace root, source
directory, runtime directory, runtime separator) in all three, and
_runtime_scope_targets unpacks the projection by name instead of reading
it by position. A real short_path never starts with a slash, so the
source separator distinguished nothing.

_runtime_demands took the paths of ordinary Files from rlocation_path and
of the other modules from its runtime_path argument, which its one caller
sets to rlocation_path. It now calls rlocation_path for both and takes no
such argument, so the two cannot differ.

The comments say what each key holds, that a failure names the first
module of a placement, and that only an ordinary File's path is built
once. runtime_scope_ancestors says its result is shared.
A directory takes the bindings of the directory above it when the walk
of npm_directory_bindings would do nothing at it. Five conditions decide
that, and three of them never decide anything for the links an ordinary
lockfile produces: a link named node_modules in a node_modules
directory, a declared node_modules directory, and a link on a scope path
under another name. So neither the fixtures nor a comparison of actions
over a real repository can show that those conditions are right.

The test resolves every directory at, above, beside and beneath 14 links
of those kinds, for each link alone, each pair and all of them, in two
visiting orders, and compares each lookup with the walk from that
directory, names in order. Dropping any one of the five conditions fails
it.

The three functions lose their underscore so the test can load them, and
the docstring of npm_directory_lookup names all five conditions and says
why a directory in a node_modules directory is one of them.
The deepest file of the test sat two directories beneath the nearest
entry that could hide it, so a lookup that gave up after three
directories passed. A file now sits three, four and five directories
beneath an entry, a second file shares its directory's answer, a sibling
differs from a directory entry by a character that sorts before the
slash, and one link takes a file's own path.
require_runtime_scopes checks the first module of each placement. Two
subjects call it with a module that passes followed by one that fails
and shares a directory with it: the same source directory in another
runtime directory, and another source directory in the same runtime
directory. Each must fail and name the second module, so a key that
leaves out the runtime directory or the source directory lets one of
them pass.
ts_test places an ancestor scope once per pair of source and runtime
directory. Every fixture had one such pair per source directory, so a
placement keyed by the source directory alone passed them all. Here two
modules of kept/ are emitted by two packages, and each runtime directory
needs its own link to their scope: with one link missing the test fails
analysis.
The staged-scope comparison runs for the first module of each runtime
directory. Every subject had its modules in one directory, so a
comparison that skipped a directory passed them all. This subject has a
directory that keeps its own scope, a directory with two modules whose
scope the staged manifest changes, and the package's own directory
after them: the failure has to name the first module of the second.
The destinations pass of _runtime_scope_data runs once per projection:
source directory, runtime directory and whether the module is retained.
No fixture fails when any of the three is left out of the key. The pass
has no need of the rule context, so it becomes runtime_scope_projection,
beside runtime_scope_targets, and a unit test compares both with one run
per module. The modules hold a retained and an emitted module in one
pair of directories, one source directory in two runtime directories
and two source directories in one, in every subset and both orders.
…ifest

The modules of one source directory share their npm lookups, and their
package scope is found once per runtime directory. No fixture places one
source directory in two runtime directories that hold a manifest each,
so one scope for the whole source directory passed them all.
validate_runfiles_modules walks up from each module to the first
component of its path and remembers the directories it found clear. No
test failed when the walk stopped one directory short. Two subjects call
it: one with the entry as the first component of the path, and one where
the directory above the entry is already known clear from an earlier
module. Each must fail and name its entry.
The fifteen commits before this one change how much work ts_test and
ts_compile do at analysis and nothing a consumer can observe in a build:
every action, argument, written file and runfiles entry is the same. A
consumer still sees the difference in analysis time and in the memory the
server allocates, so the pending changelog says what changed and by how much.

The numbers are from the lovable monorepo, one analysis of its 1,045
programs with the rules at 1583e19 and with this branch, in runs taken
minutes apart on one Bazel server. CPU-seconds there move with the machine's
load and the server's heap by more than the change itself; the allocation
does not.
@aronstrandberg
aronstrandberg force-pushed the aronstrandberg/ts-test-analysis branch from 8a8dbdb to 692ec27 Compare December 1, 2026 20:05
@aronstrandberg

Copy link
Copy Markdown
Member Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR and didn't find any bugs. It changes the core analysis logic of ts_test, ts_compile, the launcher and npm lookups in 13 passes, and every path is covered by the default CODEOWNERS entry, so a maintainer should still look at it.

What was reviewed:

  • The placement keys in require_runtime_scopes and _ancestor_scope_links: (source workspace root, source dir, runtime dir, separator) covers every input that runtime_scope_destinations, the opaque-parent check and the nearest-manifest check read. So skipping later modules in the same placement doesn't change the outcome.
  • The single-scope only fast path and the shared _NO_ANCESTORS list in runtime_scope_index/runtime_scope_ancestors. I found no caller that mutates the returned lists.
  • In _runtime_demands, rlocation paths are now computed once for each ordinary File and reused, and scoped_modules is now keyed by that path instead of by runtime_path. I also read the per-directory dedup in the staged-scope comparison.
  • I did not run the action-equivalence comparison or the new tests. The PR says the source directory in the ancestor-scope-links key still has no test.
Extended reasoning...

The change touches about 880 lines across ts/private/providers.bzl, ts_test.bzl, ts_compile.bzl, node_modules.bzl, tools/launcher/launcher.bzl and vitest.bzl. It also adds Starlark unit tests and vitest fixtures. It touches no security-sensitive surface such as injection, auth or data exposure. It is a perf refactor that claims to leave actions and runfiles unchanged, and that claim depends on each dedup key covering every input the skipped checks read, which needs careful review rather than a quick look. The .github/CODEOWNERS file assigns all paths to a default owner, and that owner should sign off on a change this broad to core rule logic.

@aronstrandberg
aronstrandberg merged commit 093757c into main Dec 2, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants