Skip to content

Fix internal frames leaking into user stack traces, and stop allocating a closure per Effect step - #8386

Merged
tim-smart merged 9 commits into
Effect-TS:mainfrom
front-depiction:effect-core-allocations
Sep 22, 2026
Merged

tim-smart merged 9 commits into
Effect-TS:mainfrom
front-depiction:effect-core-allocations

Conversation

@front-depiction

@front-depiction front-depiction commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the internal stack marker in rendered traces and removes per-step closures on V8 for core combinators.

The bug

cleanErrorStack cuts a rendered stack at the first frame matching ~effect/Effect. The internalCall marker was renamed to ~effect/Utils/internal in #1695, so the cut stopped matching and Cause.pretty of a defect thrown from a map callback or a try catcher printed the marker frame plus the internal closure above it. The cut now also recognises ~effect/Utils.

Removing the wrappers

On V8, most internalCall(() => f(x)) wrappers are redundant. The function calling them is a continuation or a suspend thunk stored under a ~effect/Effect/... property, so V8 names its frame ContImpl.mapCont [as ~effect/Effect/successCont] or PrimitiveImpl.~effect/Effect/args, and that frame already marks the cut. Calling the callback directly drops one closure per step in map, andThen, tap, zipWith, try, failSync, the catch* and tapCause* family, matchEffect and the Metric.track and Metric.trackDuration helpers. trackErrors and trackDefects also drop the wrapper, saving a closure per tracked error or defect. When their metric mapping callback throws, its rendered stack gains one internal frame compared with the old wrapper after the regex fix.

useSpan now calls its evaluator directly. Relative to main, this removes one thunk closure per span invocation. For a synchronous evaluator throw, its rendered stack has fewer internal frames than main on Node (0 instead of 2) and Bun (4 instead of 5); Bun can still show older internal frames.

tryPromise's catcher and the callback register keep the marker because they run outside the run loop.

On V8, match and matchCause return the handler result as an exit instead of wrapping each outcome in a sync effect. That saves the thunk closure per outcome; the run-loop step count is unchanged. Handlers are still called as methods of the options object, and a throwing handler is still a defect.

flatMap now builds the same frame as andThen (shared continuation plus payload) instead of its own frame with an arity check and a per-call wrapper for non-unary callbacks. Callbacks receive only the value; the fiber, exit and internal receiver were never part of the typed API. The callback's own frame also stops being swallowed by the cut, since it is no longer the continuation itself.

On engines without V8-style continuation frame labels (including JavaScriptCore), map retains an internalCall marker around the user callback, while match and matchCause retain the sync thunk. These branches keep the new rendered-stack regressions clean on Bun without allocating a per-step wrapper on V8. Other callbacks, including older adapter closures, can still expose internal frames on JavaScriptCore; SpiderMonkey has not been verified.

Behaviour differences

  • On V8, rendered stacks of defects thrown from callbacks lose the ~effect/Utils/internal frame and the wrapper closure frame. A unary flatMap callback's own frame is now rendered.
  • Effect.flatMap callbacks no longer receive the fiber and exit as extra arguments, or the continuation frame as this.

Local validation: Cause/Effect/Tracer tests (424 tests) pass on Node, Deno and Bun. Whole-repo lint (oxlint and dprint) and tsc -b for packages/effect pass. Bun 1.3.13 direct-source probes for both match/matchCause branches and map render only the user frame.

Closes EFF-1475
Closes EFF-1480

@changeset-bot

changeset-bot Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 21642cf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 31 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/ai-typesafe Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-janitor effect-janitor Bot added bug Something isn't working 4.0 labels Sep 22, 2026
@front-depiction front-depiction changed the title Fix the rendered-stack cut and reduce allocations in core Effect combinators Fix internal frames leaking into user stack traces, and stop allocating a closure per Effect step Sep 22, 2026
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
arbitrary-combinators.ts 38.18 KB 38.13 KB +0.05 KB (+0.14%)
basic.ts 6.89 KB 6.88 KB +0.02 KB (+0.26%)
batching.ts 10.08 KB 10.08 KB +0.00 KB (+0.03%)
brand.ts 6.59 KB 6.59 KB 0.00 KB (0.00%)
cache.ts 10.81 KB 10.81 KB +0.00 KB (+0.01%)
config.ts 21.99 KB 21.95 KB +0.04 KB (+0.20%)
differ.ts 20.85 KB 20.81 KB +0.04 KB (+0.21%)
http-client.ts 22.11 KB 22.08 KB +0.04 KB (+0.16%)
http-router.ts 33.47 KB 33.45 KB +0.03 KB (+0.08%)
logger.ts 10.95 KB 10.93 KB +0.03 KB (+0.23%)
metric.ts 8.93 KB 8.93 KB +0.00 KB (+0.01%)
optic.ts 6.82 KB 6.82 KB 0.00 KB (0.00%)
pubsub.ts 15.17 KB 15.13 KB +0.05 KB (+0.30%)
queue.ts 11.85 KB 11.87 KB -0.02 KB (-0.14%)
schedule.ts 11.03 KB 10.98 KB +0.05 KB (+0.43%)
schema-bigdecimal.ts 13.44 KB 13.40 KB +0.05 KB (+0.36%)
schema-binary.ts 39.42 KB 39.38 KB +0.04 KB (+0.10%)
schema-class.ts 20.56 KB 20.52 KB +0.04 KB (+0.21%)
schema-fromJsonSchemaDocument.ts 31.28 KB 31.23 KB +0.05 KB (+0.15%)
schema-representation-roundtrip.ts 26.75 KB 26.71 KB +0.04 KB (+0.15%)
schema-string-transformation.ts 14.16 KB 14.11 KB +0.05 KB (+0.33%)
schema-string.ts 11.72 KB 11.67 KB +0.05 KB (+0.41%)
schema-template-literal.ts 15.87 KB 15.83 KB +0.04 KB (+0.28%)
schema-toArbitrary.ts 37.73 KB 37.68 KB +0.05 KB (+0.13%)
schema-toCodeDocument.ts 24.99 KB 24.95 KB +0.04 KB (+0.16%)
schema-toCodecJson.ts 19.79 KB 19.75 KB +0.04 KB (+0.22%)
schema-toEquivalence.ts 19.98 KB 19.94 KB +0.04 KB (+0.22%)
schema-toFormatter.ts 20.02 KB 19.97 KB +0.04 KB (+0.22%)
schema-toJsonSchemaDocument.ts 24.34 KB 24.29 KB +0.04 KB (+0.18%)
schema-toRepresentation.ts 20.06 KB 20.02 KB +0.04 KB (+0.22%)
schema.ts 19.77 KB 19.73 KB +0.04 KB (+0.21%)
stm.ts 12.83 KB 12.85 KB -0.02 KB (-0.12%)
stream.ts 9.85 KB 9.83 KB +0.02 KB (+0.21%)

@tim-smart
tim-smart force-pushed the effect-core-allocations branch from e7849cd to 7634bc9 Compare September 22, 2026 22:07
tim-smart and others added 5 commits September 22, 2026 22:21
`cleanErrorStack` cut rendered stacks at `~effect/Effect`, but the
`internalCall` marker frame was renamed to `~effect/Utils/internal`, so
defects thrown from user callbacks rendered the marker and the internal
closure above it. The cut now also recognises `~effect/Utils`.

Most `internalCall(() => f(x))` wrappers are redundant: the caller is a
continuation or suspend thunk invoked through a `~effect/Effect/...`
property, whose frame already marks the cut. Calling the callback
directly drops a closure per step. `match` / `matchCause` return the
handler result as an exit instead of wrapping each outcome in `sync`,
and `flatMap` reuses the shared `andThen` continuation, so a callback
frame is no longer swallowed by the cut and the arity check goes away.

Co-authored-by: front_depiction <[email protected]>
…test

The map and try cases no longer emit the ~effect/Utils/internal marker,
so they passed with the old regex. tryPromise's catcher runs outside the
run loop and still relies on the marker, so it fails on main's regex.
The partition rerun test pinned behaviour this PR no longer changes.
@tim-smart
tim-smart merged commit f9179cb into Effect-TS:main Sep 22, 2026
8 of 10 checks passed
arrudaricardo pushed a commit to arrudaricardo/effect that referenced this pull request Sep 23, 2026

This branch had an error being deployed

1 failed deployment
fork — 21642cfb Deployed Sep 22, 2026 by tim-smart via approval-gate #15856
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants