Repository navigation
perf(worker): load heavy dependencies on first use to cut startup time - #466
Open
IWANABETHATGUY wants to merge 1 commit into
Open
IWANABETHATGUY wants to merge 1 commit into
IWANABETHATGUY wants to merge 1 commit into
Conversation
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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
just-bashis 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 formimetextandsafe-buffer. The shim forwards syncfscalls afterexec, which is how those callers use it.import { type Git }from@cloudflare/shell/gitkept a side-effect import underverbatimModuleSyntax, which pulled isomorphic-git into startup; it is nowimport 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 thevite buildoutput, one dry-run bundle per state and five profiles each, same machine, base =stagingat 17ecb5f: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 lintbun run typecheckbun run --cwd space typecheckbun run --cwd space buildbun run buildbun run test(39 files, 467 tests passed, 1 skipped; run in batches, see note)wrangler check startuponstagingand on this branch (numbers above)Note for reviewers: on this macOS machine the full
bun run testin one process dies withEADDRNOTAVAILfrom the workers pool after about 36 files, onstagingas well; the suite was run in batches of 4 to 12 files instead, all green.