Skip to content
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

perf_hooks: reduce overhead of createHistogram #50074

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions benchmark/perf_hooks/histogram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

const assert = require('assert');
const common = require('../common.js');

const { createHistogram } = require('perf_hooks');

const bench = common.createBenchmark(main, {
n: [1e5],
operation: ['creation', 'clone'],
});

let _histogram;

function main({ n, operation }) {
switch (operation) {
case 'creation': {
bench.start();
for (let i = 0; i < n; i++)
_histogram = createHistogram();
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_histogram);
break;
}
case 'clone': {
const histogram = createHistogram();

bench.start();
for (let i = 0; i < n; i++)
_histogram = structuredClone(histogram);
H4ad marked this conversation as resolved.
Show resolved Hide resolved
bench.end(n);

// Avoid V8 deadcode (elimination)
assert.ok(_histogram);
break;
}
default:
throw new Error(`Unsupported operation ${operation}`);
}
}
56 changes: 36 additions & 20 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -242,7 +246,7 @@ class Histogram {
const handle = this[kHandle];
return {
data: { handle },
deserializeInfo: 'internal/histogram:internalHistogram',
deserializeInfo: 'internal/histogram:ClonedHistogram',
};
}

Expand All @@ -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();
}
Qard marked this conversation as resolved.
Show resolved Hide resolved

super(skipThrowSymbol);
}

/**
Expand Down Expand Up @@ -309,7 +317,7 @@ class RecordableHistogram extends Histogram {
const handle = this[kHandle];
return {
data: { handle },
deserializeInfo: 'internal/histogram:internalRecordableHistogram',
deserializeInfo: 'internal/histogram:ClonedRecordableHistogram',
};
}

Expand All @@ -318,26 +326,34 @@ class RecordableHistogram extends Histogram {
}
}

function internalHistogram(handle) {
function ClonedHistogram(handle) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ReflectConstruct can be removed as well

Copy link
Member Author

@H4ad H4ad Oct 7, 2023

Choose a reason for hiding this comment

The 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 {{
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion test/parallel/test-performance-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@
}

(async () => {
let _deadCode;

const histogram = createHistogram();
const m = (a, b = 1) => {};
const m = (a, b = 1) => {
for (let i = 0; i < 1e3; i++)
_deadCode = i;
};
assert.ok(_deadCode >= 0);
const n = performance.timerify(m, { histogram });
assert.strictEqual(histogram.max, 0);
for (let i = 0; i < 10; i++) {
Expand All @@ -104,7 +110,7 @@
code: 'ERR_INVALID_ARG_TYPE'
});
});
})().then(common.mustCall());

Check failure on line 113 in test/parallel/test-performance-function.js

View workflow job for this annotation

GitHub Actions / test-asan

--- stderr --- node:internal/process/promises:289 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert.ok(_deadCode >= 0) at /home/runner/work/node/node/test/parallel/test-performance-function.js:100:10 at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-performance-function.js:113:3) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1434:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:122:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==' } Node.js v21.0.0 Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-performance-function.js

Check failure on line 113 in test/parallel/test-performance-function.js

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- node:internal/process/promises:289 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert.ok(_deadCode >= 0) at /home/runner/work/node/node/test/parallel/test-performance-function.js:100:10 at Object.<anonymous> (/home/runner/work/node/node/test/parallel/test-performance-function.js:113:3) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1434:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:122:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==' } Node.js v21.0.0 Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /home/runner/work/node/node/test/parallel/test-performance-function.js

Check failure on line 113 in test/parallel/test-performance-function.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:internal/process/promises:289 triggerUncaughtException(err, true /* fromPromise */); ^ AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: assert.ok(_deadCode >= 0) at /Users/runner/work/node/node/test/parallel/test-performance-function.js:100:10 at Object.<anonymous> (/Users/runner/work/node/node/test/parallel/test-performance-function.js:113:3) at Module._compile (node:internal/modules/cjs/loader:1376:14) at Module._extensions..js (node:internal/modules/cjs/loader:1434:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:122:12) at node:internal/main/run_main_module:28:49 { generatedMessage: true, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==' } Node.js v21.0.0 Command: out/Release/node --test-reporter=spec --test-reporter-destination=stdout --test-reporter=./tools/github_reporter/index.js --test-reporter-destination=stdout /Users/runner/work/node/node/test/parallel/test-performance-function.js

(async () => {
const histogram = createHistogram();
Expand Down
Loading