Skip to content

diagnostics_channel: lazily create tracing context - #65513

Open
RomainLanz wants to merge 1 commit into
nodejs:mainfrom
RomainForks:perf/diagnostics-channel-lazy-context
Open

diagnostics_channel: lazily create tracing context#65513
RomainLanz wants to merge 1 commit into
nodejs:mainfrom
RomainForks:perf/diagnostics-channel-lazy-context

Conversation

@RomainLanz

Copy link
Copy Markdown
Contributor

Note

AI assistance was used to investigate this issue and draft parts of the
implementation, tests, benchmarks, and PR description. I reviewed and
verified the final changes and benchmark results.

Hey! 👋🏻

While benchmarking our HTTP router, I noticed that manually checking whether a diagnostics channel had subscribers before tracing an operation improved performance.

That felt unexpected, so I dug deeper. It turns out that TracingChannel.traceSync() and tracePromise() create their default context before checking whether any tracing channel has subscribers.

As a result, calls without an explicit context allocate an object even when they immediately take the no-subscriber fast path.

This PR moves the default-context creation after the subscriber check. It also adds focused tests and benchmarks to verify the behavior and measure the performance impact.

Benchmarks

The comparison used two release builds, 30 alternating runs per configuration,
n=3,000,000, and benchmark/compare.js --analyze.

Method Context Improvement Confidence interval
traceSync omitted +59.94% ±2.11%
traceSync undefined +59.48% ±3.54%
tracePromise omitted +42.03% ±4.85%
tracePromise undefined +43.62% ±3.38%

All four improvements have *** confidence (p < 0.001). No statistically
significant regressions were observed for explicit-context or subscribed
control cases.

The comparison can be reproduced with:

node benchmark/compare.js \
  --old ./node-before \
  --new ./node-after \
  --runs 30 \
  --filter tracing-channel- \
  --analyze \
  diagnostics_channel

Default parameters are evaluated before the function body, so the
tracing context is allocated before the no-subscriber fast path.

Initialize it only for subscribed traces. Preserve method arity with an
undefined default and preserve explicit null behavior.

Add focused regression coverage and reusable TracingChannel benchmarks.

Signed-off-by: Romain Lanz <romain.lanz@pm.me>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added diagnostics_channel Issues and PRs related to diagnostics channel needs-ci PRs that need a full CI run. labels Aug 24, 2026
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.16%. Comparing base (18fb35a) to head (b86b81a).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65513      +/-   ##
==========================================
+ Coverage   90.15%   90.16%   +0.01%     
==========================================
  Files         751      751              
  Lines      253514   253557      +43     
  Branches    47746    47774      +28     
==========================================
+ Hits       228563   228630      +67     
+ Misses      16203    16195       -8     
+ Partials     8748     8732      -16     
Files with missing lines Coverage Δ
lib/diagnostics_channel.js 97.34% <100.00%> (+0.03%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

diagnostics_channel Issues and PRs related to diagnostics channel needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants