Skip to content

Load-time stack probes format the importer's top-level frame, so V8 re-parses the whole bundle at startup #9082

Closed

Description

@chiefcll

Follow-up to #8936 / #8937 and #8114.

Two probes run when effect is evaluated, and both format a full-depth stack:

  • pickInternalCall in packages/effect/src/Utils.ts (new Error().stack inside standard[InternalTypeId])
  • continuationMarksStack in packages/effect/src/internal/effect.ts (stackProbe)

#8937 skips the second one when Error.stackTraceLimit is 0. With the default limit, both still format every frame on the stack. In a bundled browser app, the bottom frame is the top-level function of the chunk that contains Effect, which is usually the app's entry chunk.

V8 compiles lazily without source positions. Formatting a stack therefore makes it collect positions for every frame (V8.CollectSourcePositions in a trace). For a module's top-level function, that means re-parsing the entire chunk. Neither probe reads more than its own first one or two frames.

Measurements. Production smart-TV app (SolidJS + Effect 4.0.0), a single ES-module entry chunk built with Vite 7 / Rollup, Chromium:

  • Entry chunk of 2.2 MB: a Chrome performance trace at 6× CPU throttle showed one 132 ms V8.CollectSourcePositions early in the entry's module evaluation, out of 269 ms total. Traces run with the inspector attached (see the caveat below), so treat that as an order of magnitude. The table is inspector-free.

  • After shrinking the entry to 795 KB:

    Effect build Time from entry download to first renderer work
    Unpatched 35 ms
    Probes bounded as below 25 ms

    Desktop Chrome, no throttle, median of 8. TV hardware is roughly 6–20× slower.

Measuring caveat. This can't be measured with DevTools or Playwright attached. With the inspector's Runtime domain enabled, V8 captures a detailed stack trace for every new Error(), whatever Error.stackTraceLimit is set to. We measured with a plain headless Chromium and in-page performance.now() marks.

Proposed change. Cap the stack depth to the frames each probe inspects. This keeps the existing detection result and the stackTraceLimit === 0 behaviour from #8937, so it's a no-op for engines without Error.stackTraceLimit:

// Utils.ts — pickInternalCall
const limit = getStackTraceLimit()
const bounded = typeof limit === "number"
if (bounded) setStackTraceLimit(Math.min(limit, 2))
let isNotOptimizedAway: boolean
try {
  isNotOptimizedAway = limit !== 0 &&
    standard[InternalTypeId](() => new Error().stack)?.includes(InternalTypeId) === true
} finally {
  if (bounded) setStackTraceLimit(limit)
}
// internal/effect.ts — continuationMarksStack
const limit = getStackTraceLimit()
if (limit === 0) return false
const bounded = typeof limit === "number"
if (bounded) setStackTraceLimit(Math.min(limit, 1))
try {
  return probe[marker]()?.includes("[as " + marker + "]") === true
} finally {
  if (bounded) setStackTraceLimit(limit)
}

In V8 (Node 24), both bounded probes return the same result as the originals at Error.stackTraceLimit 10, 2, 1 and 0. We measured it as a local pnpm patch before switching to the workaround below.

Current workaround. We put effect in its own chunk with manualChunks, so the bottom frame is Effect's own ~110 KB chunk instead of the app entry. That recovers most of the time, but not all, and it only works for builds that can code-split.

Happy to open a PR with a regression test along the lines of StackCapture.test.ts.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions