Skip to content

Commit db7459b

Browse files
Uzlopakrichardlau
authored andcommittedMar 25, 2024
errors: improve hideStackFrames
PR-URL: #49990 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ade6614 commit db7459b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+978
-564
lines changed
 
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
type: [
8+
'hide-stackframes',
9+
'direct-call',
10+
],
11+
n: [1e7],
12+
}, {
13+
flags: ['--expose-internals'],
14+
});
15+
16+
function main({ n, type }) {
17+
const {
18+
hideStackFrames,
19+
codes: {
20+
ERR_INVALID_ARG_TYPE,
21+
},
22+
} = require('internal/errors');
23+
24+
const testfn = (value) => {
25+
if (typeof value !== 'number') {
26+
throw new ERR_INVALID_ARG_TYPE('Benchmark', 'number', value);
27+
}
28+
};
29+
30+
const hideStackFramesTestfn = hideStackFrames((value) => {
31+
if (typeof value !== 'number') {
32+
throw new ERR_INVALID_ARG_TYPE.HideStackFrameError('Benchmark', 'number', value);
33+
}
34+
});
35+
36+
const fn = type === 'hide-stackframe' ? hideStackFramesTestfn : testfn;
37+
38+
const value = 42;
39+
40+
const length = 1024;
41+
const array = [];
42+
const errCase = false;
43+
44+
for (let i = 0; i < length; ++i) {
45+
array.push(fn(value));
46+
}
47+
48+
bench.start();
49+
50+
for (let i = 0; i < n; i++) {
51+
const index = i % length;
52+
array[index] = fn(value);
53+
}
54+
55+
bench.end(n);
56+
57+
// Verify the entries to prevent dead code elimination from making
58+
// the benchmark invalid.
59+
for (let i = 0; i < length; ++i) {
60+
assert.strictEqual(typeof array[i], errCase ? 'object' : 'undefined');
61+
}
62+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
6+
const bench = common.createBenchmark(main, {
7+
type: [
8+
'hide-stackframes',
9+
'direct-call',
10+
],
11+
double: ['true', 'false'],
12+
n: [1e5],
13+
}, {
14+
flags: ['--expose-internals'],
15+
});
16+
17+
function main({ n, type, double }) {
18+
const {
19+
hideStackFrames,
20+
codes: {
21+
ERR_INVALID_ARG_TYPE,
22+
},
23+
} = require('internal/errors');
24+
25+
const value = 'err';
26+
27+
const testfn = (value) => {
28+
if (typeof value !== 'number') {
29+
throw new ERR_INVALID_ARG_TYPE('Benchmark', 'number', value);
30+
}
31+
};
32+
33+
const hideStackFramesTestfn = hideStackFrames((value) => {
34+
if (typeof value !== 'number') {
35+
throw new ERR_INVALID_ARG_TYPE.HideStackFrameError('Benchmark', 'number', value);
36+
}
37+
});
38+
39+
function doubleTestfn(value) {
40+
testfn(value);
41+
}
42+
43+
const doubleHideStackFramesTestfn = hideStackFrames((value) => {
44+
hideStackFramesTestfn.withoutStackTrace(value);
45+
});
46+
47+
const fn = type === 'hide-stackframe' ?
48+
double === 'true' ?
49+
doubleHideStackFramesTestfn :
50+
hideStackFramesTestfn :
51+
double === 'true' ?
52+
doubleTestfn :
53+
testfn;
54+
55+
const length = 1024;
56+
const array = [];
57+
let errCase = false;
58+
59+
// Warm up.
60+
for (let i = 0; i < length; ++i) {
61+
try {
62+
fn(value);
63+
} catch (e) {
64+
errCase = true;
65+
array.push(e);
66+
}
67+
}
68+
69+
bench.start();
70+
71+
for (let i = 0; i < n; i++) {
72+
const index = i % length;
73+
try {
74+
fn(value);
75+
} catch (e) {
76+
array[index] = e;
77+
}
78+
}
79+
80+
bench.end(n);
81+
82+
// Verify the entries to prevent dead code elimination from making
83+
// the benchmark invalid.
84+
for (let i = 0; i < length; ++i) {
85+
assert.strictEqual(typeof array[i], errCase ? 'object' : 'undefined');
86+
}
87+
}

0 commit comments

Comments
 (0)
Please sign in to comment.