-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
perf_hooks: reduce overhead of createHistogram #50074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
84d042c
f0e81e0
8c2169f
bab454c
34c403e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common.js'); | ||
|
||
const { createHistogram } = require('perf_hooks'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e5], | ||
}); | ||
|
||
let _histogram; | ||
|
||
function main({ n }) { | ||
const histogram = createHistogram(); | ||
|
||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
_histogram = structuredClone(histogram); | ||
bench.end(n); | ||
|
||
// Avoid V8 deadcode (elimination) | ||
assert.ok(_histogram); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common.js'); | ||
|
||
const { createHistogram } = require('perf_hooks'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [1e5], | ||
}); | ||
|
||
let _histogram; | ||
|
||
function main({ n }) { | ||
bench.start(); | ||
for (let i = 0; i < n; i++) | ||
_histogram = createHistogram(); | ||
bench.end(n); | ||
|
||
// Avoid V8 deadcode (elimination) | ||
assert.ok(_histogram); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,26 +326,34 @@ class RecordableHistogram extends Histogram { | |
} | ||
} | ||
|
||
function internalHistogram(handle) { | ||
function ClonedHistogram(handle) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ReflectConstruct can be removed as well
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but since it is not exposed directly I didn't think that was worth it, if not for spec compliance, this code could be removed (I think). |
||
return ReflectConstruct( | ||
function() { | ||
markTransferMode(this, true, false); | ||
this[kHandle] = 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, | ||
|
Uh oh!
There was an error while loading. Please reload this page.