Skip to content

fix(sourcemaps): reduce debug ID registration stack capture - #2542

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 8 commits into
masterfrom
hugo.silva/debug-id-stack-limit
Oct 6, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 8 commits into
masterfrom
hugo.silva/debug-id-stack-limit

Conversation

@jhssilva

@jhssilva jhssilva commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Reading the debug-ID registration stack can trigger expensive source-position collection in V8 for large bundles. Temporarily set Error.stackTraceLimit to 1 while creating the registration error, restoring the original value in finally before formatting its stack.

This is the default behavior; no CLI flag is required. Engines without support ignore the setting for stack capture. Capture failures are caught so application execution continues. Custom Error constructors can observe the temporary limit, and custom stack formatters receive fewer registration frames. Already-injected bundles are skipped; rebuild fresh artifacts to adopt the change.

Benchmark examples

Median cold bundle-evaluation time over 30 trials per variant, using synthetic ESM bundles in standalone headless Chrome 154 on an Apple M1 Max. No inspector or CPU sampling was enabled. The baseline removes the registration snippet; application bodies are identical across variants. These measurements used the earlier one-frame implementation with descriptor checks. The final snippet removes those checks; the table has not been rerun for that exact revision.

Synthetic bundle Without debug ID Previous capture One-frame capture
1 MB, one file 0.20 ms 3.80 ms 0.40 ms
5 MB, one file 0.90 ms 25.00 ms 1.60 ms
20 MB, one file 5.40 ms 107.45 ms 7.95 ms

These measure bundle evaluation, not download time or Web Vitals. Warm synthetic runs showed no meaningful benefit; warm application-bundle results remain inconclusive. Performance improvement depends on the workload. Benchmark scripts and raw results are excluded from this PR.

Context of the problem

V8 reduces startup work by postponing source-position collection. Error.stack triggers the collection of source positions, which can impact performance.

Described: lazy-source-positions

Solution:
Use stackTraceLimit
image

Tests

Adds runtime tests covering:

  • One-frame registration and restoration of limits 0, 1, 10, and Infinity before custom stack formatting; nested application errors retain their frame depth and named frames before and after registration.
  • Application execution continues when a strict-mode assignment to a read-only or getter-only setting fails; registration is skipped in that case. The original limit is restored when a custom Error constructor throws.

Existing injection tests verify original sourcemap positions, matching bundle/sourcemap IDs, default one-frame capture, dry-run behavior, and skipping already-injected bundles.

Validation: build, lint, README usage check, and all 123 sourcemaps tests pass. Separate local checks of the same capture snippet with RUM SDK 7.15.0 confirmed immediate, microtask, and cross-file debug-ID resolution in Chrome, Firefox, and WebKit; these browser checks are not added to CI.

Related to #2537.

@datadog-datadog-us1-prod

datadog-datadog-us1-prod Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Tests

✅ All CI checks and tests passed.

🎉 All green!

🧪 All tests passed
❄️ No new flaky tests detected

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 921e090 | Docs | View more details | Give us feedback!

@jhssilva jhssilva changed the title feat(sourcemaps): add experimental one-frame debug ID capture fix(sourcemaps): reduce debug ID registration stack capture Oct 6, 2026
@jhssilva
jhssilva marked this pull request as ready for review October 6, 2026 13:31
@jhssilva
jhssilva requested review from a team as code owners October 6, 2026 13:31
@jhssilva jhssilva added the rum Related to [dsyms, flutter-symbols, react-native, sourcemaps, unity-symbols] label Oct 6, 2026
Comment thread packages/base/src/commands/sourcemaps/debugId.ts
@jhssilva
jhssilva requested a review from amortemousque October 6, 2026 13:46
@buranmert
buranmert requested a balanced review from Copilot October 6, 2026 14:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The snippet leaves an observable stackTraceLimit property behind when the property was initially absent.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Reduces sourcemap debug-ID registration overhead by limiting V8 stack capture to one frame.

Changes:

  • Temporarily sets and restores Error.stackTraceLimit.
  • Adds runtime coverage for restoration and failure scenarios.
  • Verifies generated bundles contain the optimization.
File Description
debugId.ts Updates the injected registration snippet.
debugId.test.ts Tests stack limits and failure handling.
inject.test.ts Verifies optimized snippet injection.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

`(function(c,n){try{if(typeof window==='undefined')return;var w=window,m=w[n]=w[n]||{},s=new Error().stack;s&&(m[s]=c)}catch(e){}})({"ddDebugId":"${debugId}"},"${SOURCE_CODE_CONTEXT_MARKER}");`
const buildSnippet = (debugId: string): string => {
// Restore before reading .stack so prepareStackTrace sees the application setting.
return `(function(c,n){try{if(typeof window==='undefined')return;var w=window,m=w[n]=w[n]||{},l=Error.stackTraceLimit;try{Error.stackTraceLimit=1;var e=new Error()}finally{Error.stackTraceLimit=l}var s=e.stack;s&&(m[s]=c)}catch(e){}})({"ddDebugId":"${debugId}"},"${SOURCE_CODE_CONTEXT_MARKER}");`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was introduced by the simplification on d3280b4

Comment thread packages/base/src/commands/sourcemaps/debugId.ts Outdated
@jhssilva

jhssilva commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-10-06 15:41:44 UTC ℹ️ Start processing command /merge


2026-10-06 15:41:50 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 10m (p90).


2026-10-06 15:49:14 UTC ℹ️ MergeQueue: This merge request was merged

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

Labels

mergequeue-status: done rum Related to [dsyms, flutter-symbols, react-native, sourcemaps, unity-symbols]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants