Skip to content

perf(worker): load heavy dependencies on first use to cut startup time - #466

Open
IWANABETHATGUY wants to merge 1 commit into
cloudflare:stagingfrom
IWANABETHATGUY:perf/worker-startup
Open

IWANABETHATGUY wants to merge 1 commit into
cloudflare:stagingfrom
IWANABETHATGUY:perf/worker-startup

Conversation

@IWANABETHATGUY

Copy link
Copy Markdown

Summary

  • The Worker's global scope loaded the code fixer (babel), the static analyzer, the browser-capture client, the pre-deploy safety gate, cf-git, the worker bundler, just-bash, Octokit and the OpenAI client, although each is only used by specific routes or agent operations. They now load on first use.
  • just-bash is imported by two dependencies (@cloudflare/think/tools/workspace, agents/skills), so it loads through a lazy shim behind a Vite alias, the same pattern the config already uses for mimetext and safe-buffer. The shim forwards sync fs calls after exec, which is how those callers use it.
  • The Think skill catalog parsed its SKILL.md frontmatter at import; it is built on first use.
  • import { type Git } from @cloudflare/shell/git kept a side-effect import under verbatimModuleSyntax, which pulled isomorphic-git into startup; it is now import type.

Each deferred module still loads once per isolate: the first request that needs it pays that load, later requests hit the module cache. Isolates that never reach those routes no longer pay at all.

Measurement

wrangler check startup (Wrangler 4.149.0, workerd 1.20261006.1) on the vite build output, one dry-run bundle per state and five profiles each, same machine, base = staging at 17ecb5f:

staging this PR
active startup, median (min, max) 102.5 ms (100.3, 108.8) 63.3 ms (58.9, 74.0)
garbage collection during startup, median 27.4 ms 16.3 ms
JavaScript evaluated at startup 12.1 MB 4.8 MB
total upload 25207.40 KiB (gzip 6237.56 KiB) 25360.13 KiB (gzip 6271.45 KiB)

Remaining startup cost is dominated by zod schema construction in @modelcontextprotocol/sdk (loaded through the agents SDK), by isomorphic-git (constructed in the space backends) and by the app's own zod and drizzle schemas; none of those move with an import change.

Test plan

  • bun run lint
  • bun run typecheck
  • bun run --cwd space typecheck
  • bun run --cwd space build
  • bun run build
  • bun run test (39 files, 467 tests passed, 1 skipped; run in batches, see note)
  • pre-commit related suites
  • wrangler check startup on staging and on this branch (numbers above)

Note for reviewers: on this macOS machine the full bun run test in one process dies with EADDRNOTAVAIL from the workers pool after about 36 files, on staging as well; the suite was run in batches of 4 to 12 files instead, all green.

The Worker's global scope loaded the code fixer (babel), the static analyzer,
the browser-capture client, the pre-deploy safety gate, cf-git, the worker
bundler, just-bash, Octokit and the OpenAI client, although each is only used
by specific routes or agent operations. Load them on first use instead, build
the Think skill catalog on first use, and import the shell git types as types
only, which had kept isomorphic-git in the startup graph.

Measured with `wrangler check startup` on the `vite build` output, median of
5 runs on one machine, against staging at 17ecb5f: active startup
102.5 ms -> 63.3 ms (GC 27.4 -> 16.3 ms). JavaScript evaluated at startup:
12.1 MB -> 4.8 MB. Total upload +153 KiB.
@IWANABETHATGUY
IWANABETHATGUY marked this pull request as ready for review October 11, 2026 18:09
IWANABETHATGUY added a commit to rolldown/rolldown that referenced this pull request Nov 4, 2026
Measure a Vite-built Cloudflare Worker's startup under the app's own
workerd and attribute it to modules, with the loop the browser and node
modes have: `worker-config` writes a Vite config that puts the rolldown
metrics report on every environment; `worker-scan` times the main
module's import on fresh workerd instances minus an empty-import floor,
hits the pinned requests on the real entry, profiles the import, and
takes precise coverage from inside the isolate (wrangler >= 4.108.0, or
`--tools <dir>` for that run); `worker-attrib` charges startup CPU to
the evaluating module from a preserveModules build; `why <module>` lists
the importers and the chain; `cut --root a,b` keeps Durable Object class
modules eager so the cut lands below them.

cloudflare/agents-starter after one scan:

```
[consolidated] 5 cold runs (fresh workerd each)
startup 23ms (p75 24ms) | floor 4ms | app 19ms | instance boot 87ms (not counted)
guard: PASS
first requests on the real entry (startup already done; a deferred module loads on its first hit):
  /                            200      6ms  785 bytes
  /agents/chat-agent/default   404      9ms  15 bytes
startup CPU (profile): active 24ms = app modules 10ms (57.1%) + runtime 3ms + engine 4ms + parse/compile "(program)" 4ms; GC 4ms; 168 samples
  top self-time at startup:
          5ms  ../../node_modules/zod/v4/core/core.js
          4ms  (engine: gc)
          3ms  ../../node_modules/zod/v4/classic/schemas.js
          3ms  (unmapped)
          1ms  ../../node_modules/zod/v4/core/util.js
          1ms  ../../node_modules/zod/v4/core/schemas.js
  3ms sits in chunk top-level code V8 gave no line ticks for - `node harness.mjs worker-attrib` on an attribution build charges it to the evaluating module
evaluated at startup: 547.3KB of 1940.2KB in the static closure (183 modules, 28% executed); 4 runtime module(s) loaded (node-internal:internal_inspector, cloudflare:workers, node:async_hooks, node:diagnostics_channel)
cold at startup - in the closure, never executed to start (2):
      24.5KB  ../../node_modules/@cfworker/json-schema/dist/esm/validate.js
       4.3KB  ../../node_modules/zod/v3/locales/en.js
parsed but mostly not executed at startup (libraries run a slice of themselves at import; the rest is load cost):
     202.3KB idle of   269.3KB   25% executed  ../../node_modules/agents/dist/index.js
     172.5KB idle of   184.0KB    6% executed  ../../node_modules/ai/dist/index.mjs
     102.0KB idle of   152.7KB   33% executed  ../../node_modules/@cloudflare/ai-chat/dist/index.js
      75.5KB idle of    84.7KB   11% executed  ../../node_modules/zod/v3/types.js
      64.9KB idle of    77.0KB   16% executed  ../../node_modules/workers-ai-provider/dist/index.mjs
      62.9KB idle of   120.8KB   48% executed  ../../node_modules/agents/dist/chat/index.js
      52.3KB idle of    58.0KB   10% executed  ../../node_modules/@ai-sdk/provider-utils/dist/index.mjs
```

Shared changes, used by every mode: profile attribution splits a frame's
self-time by its line ticks and maps every chunk that has a sourcemap
(checklist A4); the ownership split counts multi-chunk source rows as
app code; `cut --root`.

Shaped on cloudflare/agents-starter and cloudflare/vibesdk; the vibesdk
pass is cloudflare/vibesdk#466 (`wrangler check startup`: 102.5 ms ->
63.3 ms). Design notes: "Worker startup mode (Cloudflare Workers)" in
`packages/perf-kit/README.md`; the agent contract in `AGENTS.md`.

The first commit, the `serve.mjs` socket fix, is a parity matter and can
move down into #11140.

requires #11140
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant