mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
perf_hooks: reduce overhead of createHistogram
PR-URL: https://github.com/nodejs/node/pull/50074 Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
parent
8e814e3b82
commit
adaef03216
3 changed files with 82 additions and 20 deletions
|
@ -52,9 +52,13 @@ function isHistogram(object) {
|
|||
return object?.[kHandle] !== undefined;
|
||||
}
|
||||
|
||||
const kSkipThrow = Symbol('kSkipThrow');
|
||||
|
||||
class Histogram {
|
||||
constructor() {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
constructor(skipThrowSymbol = undefined) {
|
||||
if (skipThrowSymbol !== kSkipThrow) {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
}
|
||||
}
|
||||
|
||||
[kInspect](depth, options) {
|
||||
|
@ -242,7 +246,7 @@ class Histogram {
|
|||
const handle = this[kHandle];
|
||||
return {
|
||||
data: { handle },
|
||||
deserializeInfo: 'internal/histogram:internalHistogram',
|
||||
deserializeInfo: 'internal/histogram:ClonedHistogram',
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -264,8 +268,12 @@ class Histogram {
|
|||
}
|
||||
|
||||
class RecordableHistogram extends Histogram {
|
||||
constructor() {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
constructor(skipThrowSymbol = undefined) {
|
||||
if (skipThrowSymbol !== kSkipThrow) {
|
||||
throw new ERR_ILLEGAL_CONSTRUCTOR();
|
||||
}
|
||||
|
||||
super(skipThrowSymbol);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -309,7 +317,7 @@ class RecordableHistogram extends Histogram {
|
|||
const handle = this[kHandle];
|
||||
return {
|
||||
data: { handle },
|
||||
deserializeInfo: 'internal/histogram:internalRecordableHistogram',
|
||||
deserializeInfo: 'internal/histogram:ClonedRecordableHistogram',
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -318,7 +326,7 @@ class RecordableHistogram extends Histogram {
|
|||
}
|
||||
}
|
||||
|
||||
function internalHistogram(handle) {
|
||||
function ClonedHistogram(handle) {
|
||||
return ReflectConstruct(
|
||||
function() {
|
||||
markTransferMode(this, true, false);
|
||||
|
@ -326,18 +334,26 @@ function internalHistogram(handle) {
|
|||
this[kMap] = new SafeMap();
|
||||
}, [], Histogram);
|
||||
}
|
||||
internalHistogram.prototype[kDeserialize] = () => {};
|
||||
|
||||
function internalRecordableHistogram(handle) {
|
||||
return ReflectConstruct(
|
||||
function() {
|
||||
markTransferMode(this, true, false);
|
||||
this[kHandle] = handle;
|
||||
this[kMap] = new SafeMap();
|
||||
this[kRecordable] = true;
|
||||
}, [], RecordableHistogram);
|
||||
ClonedHistogram.prototype[kDeserialize] = () => { };
|
||||
|
||||
function ClonedRecordableHistogram(handle) {
|
||||
const histogram = new RecordableHistogram(kSkipThrow);
|
||||
|
||||
markTransferMode(histogram, true, false);
|
||||
histogram[kRecordable] = true;
|
||||
histogram[kMap] = new SafeMap();
|
||||
histogram[kHandle] = handle;
|
||||
histogram.constructor = RecordableHistogram;
|
||||
|
||||
return histogram;
|
||||
}
|
||||
|
||||
ClonedRecordableHistogram.prototype[kDeserialize] = () => { };
|
||||
|
||||
function createRecordableHistogram(handle) {
|
||||
return new ClonedRecordableHistogram(handle);
|
||||
}
|
||||
internalRecordableHistogram.prototype[kDeserialize] = () => {};
|
||||
|
||||
/**
|
||||
* @param {{
|
||||
|
@ -363,14 +379,14 @@ function createHistogram(options = kEmptyObject) {
|
|||
throw new ERR_INVALID_ARG_VALUE.RangeError('options.highest', highest);
|
||||
}
|
||||
validateInteger(figures, 'options.figures', 1, 5);
|
||||
return internalRecordableHistogram(new _Histogram(lowest, highest, figures));
|
||||
return createRecordableHistogram(new _Histogram(lowest, highest, figures));
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
Histogram,
|
||||
RecordableHistogram,
|
||||
internalHistogram,
|
||||
internalRecordableHistogram,
|
||||
ClonedHistogram,
|
||||
ClonedRecordableHistogram,
|
||||
isHistogram,
|
||||
kDestroy,
|
||||
kHandle,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue