perf: skip the MCP server, prompt library, and Playwright until a command needs them

Merged
KD
18d ago
+303-130
  • e2e --version, --help, run, and list no longer load the MCP server, @clack/prompts, or zod at startup. mcp, init, login, logout, models, explore, upgrade, doctor, and --watch import their module when they run. --version reads 260 files, 2.2 MB, instead of 438 files, 4.6 MB. list on a web project reads 336 files, 3.1 MB, instead of 517 files, 11.9 MB.
  • The bounds and choice lists a flag needs moved into modules that import nothing heavy, so defining a flag loads nothing. These are SESSION_BOUNDS, STEP_BOUNDS, TIMEOUT_BOUNDS, PROVIDER_IDS, and the upgrade target parser.
  • @e2e-dev/web imports playwright-core at the first browser lookup, launch, or attach. e2e list never loads it, and neither does the main process of a run on a CDP-attached or provider-hosted browser. browserCheck and ensureBrowsersInstalled are async now.
  • BrowserConnection.acquire sets pending before it awaits, because the first call now loads Playwright. A dispose that arrived during that load would miss the browser the launch then produced, and two callers could each launch one.
  • e2e/runner loads the explorer when explore is called. @e2e-dev/acp loads the ACP SDK and the MCP server at its first session, and @e2e-dev/limrun loads @limrun/api at its first lease. A missing @limrun/api now fails at that lease instead of at config import.
  • A STARTUP table in package-imports.test.ts fails with the import chain when the CLI, runner, worker, acp, limrun, or web entry reaches one of these packages through static imports again. The scan also counts import { type X } from 'x' as a runtime import, because verbatimModuleSyntax leaves import {} from 'x' in the output.

Measured

main at 76a5280b against this change, on one Apple M1 Max with Node 26.12.1, measured before the last rebase onto 4dce5a76, a tap-position fix that does not touch loading. The runs alternate between the two builds in random order. Each row is the median of the paired runs with the 95% bootstrap interval of the paired change, computed by scripts/bench-ab/stats.ts. All 14 rows are faster against a threshold of 3% and 10 ms.

Scenariomainthis PRchange (95% CI)pairs
e2e --version272 ms198 ms-27.4% (-27.8% to -27.0%)30
e2e --help272 ms198 ms-27.2% (-27.6% to -26.7%)30
e2e list, web project, 4 tests452 ms238 ms-47.4% (-47.6% to -47.0%)30
e2e list, testbed, 51 files567 ms379 ms-33.3% (-33.9% to -32.9%)20
e2e init --yes, new directory295 ms245 ms-16.7% (-17.3% to -16.1%)15
e2e doctor --target web450 ms415 ms-8.1% (-8.4% to -7.5%)20
e2e mcp, until tools/list answers386 ms363 ms-5.8% (-6.2% to -4.7%)20
e2e run, local browser, 1 worker, 4 tests1.75 s1.69 s-3.7% (-4.1% to -3.3%)20
e2e run, CDP attach, 1 worker, 4 tests1.83 s1.63 s-10.8% (-11.4% to -9.9%)20
e2e run, CDP attach, 3 workers, 4 tests1.56 s1.36 s-13.2% (-13.4% to -12.8%)20
import @e2e-dev/web316 ms166 ms-47.5% (-47.8% to -47.1%)20
import @e2e-dev/limrun203 ms141 ms-30.6% (-31.4% to -30.2%)20
import @e2e-dev/acp223 ms194 ms-13.3% (-13.7% to -12.9%)20
import e2e/runner207 ms183 ms-12.2% (-12.5% to -11.6%)20

The imports include about 100 ms of Node starting. Peak memory of the largest process drops from 104 MB to 85 MB for --version, from 181 MB to 91 MB for list on the web project, and from 228 MB to 142 MB for list on the testbed. run stays at about 200 MB.

A local launch still loads Playwright in the main process, in prepare, and in every worker, so run saves only the CLI's startup, 40 to 70 ms. A run whose browser is attached over CDP skips Playwright in the main process too.

pnpm bench:ab --suite testbed --threshold 3 --min-pairs 30 --max-pairs 30 -- tests/forms.e2e.ts ran every test through the same steps with the same outcome on both builds, and every counter matched.

Metricmainthis PRchange (95% CI)verdict
wall (process)3.21 s3.17 s-1.3% (-1.6% to -1.0%)same
suite (sum of attempts)1.97 s2.11 s+7.2% (+6.7% to +7.6%)slower

The slower row comes from one test, the first one a worker runs. fills, clears, and submits a profile takes 1.51 s on main and 1.66 s here. A worker now loads Playwright for its first browser inside that attempt. It used to load it with the config, before any attempt started. When each worker loads Playwright before it starts, using NODE_OPTIONS=--import, the first attempt is 14 ms over main and the process takes as long as before, 3150 ms with the preload and 3155 ms without, over 8 runs each. The work moved and did not grow. The changeset says so.

Verified

Ran it locally: yes

  • Built CLI against main on 27 commands, covering --help of every changed command, flag-bound errors, login, logout, models, upgrade argument errors, explore, doctor, and list. Stdout, stderr, and exit code are identical.
  • run --watch in a pty shows the same screen as main. init --yes writes the same 16 files and links and prints the same text. upgrade --dry-run in the testbed prints the same line and exits 0.
  • e2e mcp answers initialize and tools/list with the same 5 tools.
  • The emitted .d.ts files of e2e, @e2e-dev/web, @e2e-dev/acp, and @e2e-dev/limrun are unchanged at every entry point. The files that differ are internal modules.
  • The new tests fail on the old code. With pending set after the registration, dispose sees 0 close calls on the browser and two callers launch twice. Without its await, browserCheck reports an uninstalled browser as installed.
  • Not run: browsers hosted by Kernel, smol, lightpanda, BrowserStack, or TestMu, which need credentials or binaries that are not available here. The CDP rows stand in for them, since prepare returns early for a lease the same way it does for connect.

šŸ¤– Generated with Claude Code