diagnostics_channel: lazily create tracing context · nodejs/node@7003159 · GitHub
Skip to content

Commit 7003159

Browse files
RomainLanzaduh95
authored andcommitted
diagnostics_channel: lazily create tracing context
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>
1 parent 09b333b commit 7003159

4 files changed

Lines changed: 191 additions & 2 deletions

File tree

Lines changed: 53 additions & 0 deletions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const dc = require('node:diagnostics_channel');
5+
6+
const bench = common.createBenchmark(main, {
7+
n: [1e7],
8+
context: ['omitted', 'undefined', 'provided'],
9+
subscribers: [0, 1],
10+
});
11+
12+
function noop() {}
13+
14+
function main({ n, context, subscribers }) {
15+
const channel = dc.tracingChannel('test');
16+
const providedContext = { __proto__: null };
17+
18+
if (subscribers) {
19+
channel.subscribe({ start: noop });
20+
}
21+
22+
bench.start();
23+
switch (context) {
24+
case 'omitted':
25+
for (let i = 0; i < n; i++) {
26+
channel.traceSync(noop);
27+
}
28+
break;
29+
case 'undefined':
30+
for (let i = 0; i < n; i++) {
31+
channel.traceSync(noop, undefined);
32+
}
33+
break;
34+
case 'provided':
35+
for (let i = 0; i < n; i++) {
36+
channel.traceSync(noop, providedContext);
37+
}
38+
break;
39+
default:
40+
throw new Error(`Unsupported context value: ${context}`);
41+
}
42+
bench.end(n);
43+
}

‎lib/diagnostics_channel.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,15 @@ class TracingChannel {
522522
return done;
523523
}
524524

525-
traceSync(fn, context = { __proto__: null }, thisArg, ...args) {
525+
traceSync(fn, context = undefined, thisArg, ...args) {
526526
if (!this.hasSubscribers) {
527527
return ReflectApply(fn, thisArg, args);
528528
}
529529

530+
if (context === undefined) {
531+
context = { __proto__: null };
532+
}
533+
530534
const { error } = this;
531535

532536
// eslint-disable-next-line no-unused-vars
@@ -542,7 +546,7 @@ class TracingChannel {
542546
}
543547
}
544548

545-
tracePromise(fn, context = { __proto__: null }, thisArg, ...args) {
549+
tracePromise(fn, context = undefined, thisArg, ...args) {
546550
if (!this.hasSubscribers) {
547551
const result = ReflectApply(fn, thisArg, args);
548552
if (typeof result?.then !== 'function') {
@@ -551,6 +555,10 @@ class TracingChannel {
551555
return result;
552556
}
553557

558+
if (context === undefined) {
559+
context = { __proto__: null };
560+
}
561+
554562
const { error } = this;
555563
const continuationWindow = this.#continuationWindow;
556564

Lines changed: 85 additions & 0 deletions

0 commit comments

Comments
 (0)