Skip to content

Commit 905ca00

Browse files
H4adtargos
authored andcommitted
perf_hooks: reduce overhead of createHistogram
PR-URL: #50074 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 766bd9c commit 905ca00

File tree

3 files changed

+82
-20
lines changed

3 files changed

+82
-20
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const { createHistogram } = require('perf_hooks');
7+
8+
const bench = common.createBenchmark(main, {
9+
n: [1e5],
10+
});
11+
12+
let _histogram;
13+
14+
function main({ n }) {
15+
const histogram = createHistogram();
16+
17+
bench.start();
18+
for (let i = 0; i < n; i++)
19+
_histogram = structuredClone(histogram);
20+
bench.end(n);
21+
22+
// Avoid V8 deadcode (elimination)
23+
assert.ok(_histogram);
24+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const common = require('../common.js');
5+
6+
const { createHistogram } = require('perf_hooks');
7+
8+
const bench = common.createBenchmark(main, {
9+
n: [1e5],
10+
});
11+
12+
let _histogram;
13+
14+
function main({ n }) {
15+
bench.start();
16+
for (let i = 0; i < n; i++)
17+
_histogram = createHistogram();
18+
bench.end(n);
19+
20+
// Avoid V8 deadcode (elimination)
21+
assert.ok(_histogram);
22+
}

lib/internal/histogram.js

+36-20
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ function isHistogram(object) {
5252
return object?.[kHandle] !== undefined;
5353
}
5454

55+
const kSkipThrow = Symbol('kSkipThrow');
56+
5557
class Histogram {
56-
constructor() {
57-
throw new ERR_ILLEGAL_CONSTRUCTOR();
58+
constructor(skipThrowSymbol = undefined) {
59+
if (skipThrowSymbol !== kSkipThrow) {
60+
throw new ERR_ILLEGAL_CONSTRUCTOR();
61+
}
5862
}
5963

6064
[kInspect](depth, options) {
@@ -242,7 +246,7 @@ class Histogram {
242246
const handle = this[kHandle];
243247
return {
244248
data: { handle },
245-
deserializeInfo: 'internal/histogram:internalHistogram',
249+
deserializeInfo: 'internal/histogram:ClonedHistogram',
246250
};
247251
}
248252

@@ -264,8 +268,12 @@ class Histogram {
264268
}
265269

266270
class RecordableHistogram extends Histogram {
267-
constructor() {
268-
throw new ERR_ILLEGAL_CONSTRUCTOR();
271+
constructor(skipThrowSymbol = undefined) {
272+
if (skipThrowSymbol !== kSkipThrow) {
273+
throw new ERR_ILLEGAL_CONSTRUCTOR();
274+
}
275+
276+
super(skipThrowSymbol);
269277
}
270278

271279
/**
@@ -309,7 +317,7 @@ class RecordableHistogram extends Histogram {
309317
const handle = this[kHandle];
310318
return {
311319
data: { handle },
312-
deserializeInfo: 'internal/histogram:internalRecordableHistogram',
320+
deserializeInfo: 'internal/histogram:ClonedRecordableHistogram',
313321
};
314322
}
315323

@@ -318,26 +326,34 @@ class RecordableHistogram extends Histogram {
318326
}
319327
}
320328

321-
function internalHistogram(handle) {
329+
function ClonedHistogram(handle) {
322330
return ReflectConstruct(
323331
function() {
324332
markTransferMode(this, true, false);
325333
this[kHandle] = handle;
326334
this[kMap] = new SafeMap();
327335
}, [], Histogram);
328336
}
329-
internalHistogram.prototype[kDeserialize] = () => {};
330337

331-
function internalRecordableHistogram(handle) {
332-
return ReflectConstruct(
333-
function() {
334-
markTransferMode(this, true, false);
335-
this[kHandle] = handle;
336-
this[kMap] = new SafeMap();
337-
this[kRecordable] = true;
338-
}, [], RecordableHistogram);
338+
ClonedHistogram.prototype[kDeserialize] = () => { };
339+
340+
function ClonedRecordableHistogram(handle) {
341+
const histogram = new RecordableHistogram(kSkipThrow);
342+
343+
markTransferMode(histogram, true, false);
344+
histogram[kRecordable] = true;
345+
histogram[kMap] = new SafeMap();
346+
histogram[kHandle] = handle;
347+
histogram.constructor = RecordableHistogram;
348+
349+
return histogram;
350+
}
351+
352+
ClonedRecordableHistogram.prototype[kDeserialize] = () => { };
353+
354+
function createRecordableHistogram(handle) {
355+
return new ClonedRecordableHistogram(handle);
339356
}
340-
internalRecordableHistogram.prototype[kDeserialize] = () => {};
341357

342358
/**
343359
* @param {{
@@ -363,14 +379,14 @@ function createHistogram(options = kEmptyObject) {
363379
throw new ERR_INVALID_ARG_VALUE.RangeError('options.highest', highest);
364380
}
365381
validateInteger(figures, 'options.figures', 1, 5);
366-
return internalRecordableHistogram(new _Histogram(lowest, highest, figures));
382+
return createRecordableHistogram(new _Histogram(lowest, highest, figures));
367383
}
368384

369385
module.exports = {
370386
Histogram,
371387
RecordableHistogram,
372-
internalHistogram,
373-
internalRecordableHistogram,
388+
ClonedHistogram,
389+
ClonedRecordableHistogram,
374390
isHistogram,
375391
kDestroy,
376392
kHandle,

0 commit comments

Comments
 (0)