Skip to content

diagnostics_channel: lazily create tracing context - #65513

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
RomainForks:perf/diagnostics-channel-lazy-context
Sep 2, 2026
Merged

diagnostics_channel: lazily create tracing context#65513
nodejs-github-bot merged 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 the diagnostics_channel module. 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 162 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.

@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 25, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 25, 2026
@nodejs-github-bot

This comment was marked as resolved.

@RomainLanz

Copy link
Copy Markdown
Contributor Author

I cannot see the CI, what's the issue there?

@legendecas legendecas added the author ready PRs with CI started, the required approvals, and no outstanding review comments. label Aug 26, 2026
@nodejs-github-bot

This comment was marked as resolved.

@legendecas

Copy link
Copy Markdown
Member

Non-relevant failures. Restarted.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Benchmark GHA (diagnostics_channel / tracing-channel-): https://github.com/nodejs/node/actions/runs/33629584049

Results

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

Benchmark results:

                                                                                            confidence improvement accuracy (*)   (**)   (***)
diagnostics_channel/tracing-channel-promise.js subscribers=0 context='omitted' n=10000000          ***     45.56 %       ±7.16% ±9.44% ±12.13%
diagnostics_channel/tracing-channel-promise.js subscribers=0 context='provided' n=10000000                 -1.82 %       ±5.47% ±7.21%  ±9.25%
diagnostics_channel/tracing-channel-promise.js subscribers=0 context='undefined' n=10000000        ***     42.07 %       ±6.90% ±9.09% ±11.68%
diagnostics_channel/tracing-channel-promise.js subscribers=1 context='omitted' n=10000000                  -0.22 %       ±5.73% ±7.55%  ±9.69%
diagnostics_channel/tracing-channel-promise.js subscribers=1 context='provided' n=10000000                 -0.74 %       ±5.45% ±7.18%  ±9.22%
diagnostics_channel/tracing-channel-promise.js subscribers=1 context='undefined' n=10000000                -1.68 %       ±5.54% ±7.30%  ±9.36%
diagnostics_channel/tracing-channel-sync.js subscribers=0 context='omitted' n=10000000             ***     54.77 %       ±7.20% ±9.50% ±12.20%
diagnostics_channel/tracing-channel-sync.js subscribers=0 context='provided' n=10000000                    -1.21 %       ±5.94% ±7.83% ±10.04%
diagnostics_channel/tracing-channel-sync.js subscribers=0 context='undefined' n=10000000           ***     54.55 %       ±7.34% ±9.68% ±12.43%
diagnostics_channel/tracing-channel-sync.js subscribers=1 context='omitted' n=10000000                      0.07 %       ±6.56% ±8.64% ±11.09%
diagnostics_channel/tracing-channel-sync.js subscribers=1 context='provided' n=10000000                     0.70 %       ±6.48% ±8.55% ±10.97%
diagnostics_channel/tracing-channel-sync.js subscribers=1 context='undefined' n=10000000                    0.07 %       ±6.66% ±8.78% ±11.27%

Be aware that when doing many comparisons the risk of a false-positive
result increases. In this case, there are 12 comparisons, you can thus
expect the following amount of false-positive results:
  0.60 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.12 false positives, when considering a   1% risk acceptance (**, ***),
  0.01 false positives, when considering a 0.1% risk acceptance (***)

[!WARNING]
Do not take GHA benchmark results as face value, always confirm them
using a dedicated machine, e.g. Jenkins CI.

@legendecas legendecas added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 2, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 2befec5 into nodejs:main Sep 2, 2026
85 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 2befec5

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 2, 2026
@RomainLanz
RomainLanz deleted the perf/diagnostics-channel-lazy-context branch September 2, 2026 16:46
aduh95 pushed a commit that referenced this pull request Sep 3, 2026
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>
PR-URL: #65513
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. diagnostics_channel Issues and PRs related to the diagnostics_channel module. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants