Skip to content

Commit 123fa0c

Browse files
committed
perf_hooks: return different timerified function in timerify
Fixes: #42742
1 parent c4781ea commit 123fa0c

File tree

5 files changed

+70
-12
lines changed

5 files changed

+70
-12
lines changed

deps/v8/src/logging/counters.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class StatsTable {
6464
return lookup_function_(name);
6565
}
6666

67-
// Create a histogram by name. If the create is successful,
67+
// Create a histogram by name. If the creation is successful,
6868
// returns a non-nullptr pointer for use with AddHistogramSample
6969
// function. min and max define the expected minimum and maximum
7070
// sample values. buckets is the maximum number of buckets

lib/internal/histogram.js

+25
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ function isHistogram(object) {
5353
return object?.[kHandle] !== undefined;
5454
}
5555

56+
function* infinite() {
57+
let id = 0;
58+
59+
while (true) {
60+
yield id++;
61+
}
62+
}
63+
64+
const idWeakMap = new WeakMap();
65+
const gen = infinite()
66+
67+
function getHistogramID(object) {
68+
if (object == null) {
69+
return -1;
70+
}
71+
if (idWeakMap.has(object)) {
72+
return idWeakMap.get(object);
73+
} else {
74+
const id = gen.next().value;
75+
idWeakMap.set(object, id);
76+
return id
77+
}
78+
}
79+
5680
class Histogram {
5781
constructor() {
5882
throw new ERR_ILLEGAL_CONSTRUCTOR();
@@ -377,6 +401,7 @@ module.exports = {
377401
internalHistogram,
378402
internalRecordableHistogram,
379403
isHistogram,
404+
getHistogramID,
380405
kDestroy,
381406
kHandle,
382407
kMap,

lib/internal/perf/timerify.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const {
1818
} = require('internal/validators');
1919

2020
const {
21-
isHistogram
21+
isHistogram,
22+
getHistogramID
2223
} = require('internal/histogram');
2324

2425
const {
@@ -71,7 +72,9 @@ function timerify(fn, options = {}) {
7172
histogram);
7273
}
7374

74-
if (fn[kTimerified]) return fn[kTimerified];
75+
const id = getHistogramID(histogram)
76+
77+
if (fn[kTimerified]?.[id]) return fn[kTimerified][id];
7578

7679
const constructor = isConstructor(fn);
7780

@@ -112,13 +115,19 @@ function timerify(fn, options = {}) {
112115
}
113116
});
114117

115-
ObjectDefineProperties(fn, {
116-
[kTimerified]: {
117-
configurable: false,
118-
enumerable: false,
119-
value: timerified,
120-
}
121-
});
118+
if (fn[kTimerified]) {
119+
fn[kTimerified][id] = timerified
120+
} else {
121+
ObjectDefineProperties(fn, {
122+
[kTimerified]: {
123+
configurable: false,
124+
enumerable: false,
125+
value: {
126+
[id]: timerified
127+
},
128+
}
129+
});
130+
}
122131

123132
return timerified;
124133
}

test/parallel/test-performance-function.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ const {
8181
const n = performance.timerify(m);
8282
const o = performance.timerify(m);
8383
const p = performance.timerify(n);
84-
assert.strictEqual(n, o);
85-
assert.strictEqual(n, p);
84+
assert.notStrictEqual(n, o);
85+
assert.notStrictEqual(n, p);
8686
assert.strictEqual(n.length, m.length);
87+
assert.strictEqual(o.length, m.length);
88+
assert.strictEqual(p.length, m.length);
8789
assert.strictEqual(n.name, 'timerified m');
8890
}
8991

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const {
6+
createHistogram,
7+
performance: {
8+
timerify
9+
}
10+
} = require('perf_hooks');
11+
12+
const assert = require('assert');
13+
14+
let h1 = createHistogram();
15+
let h2 = createHistogram();
16+
17+
const fn = () => {};
18+
19+
const f1 = timerify(fn, { histogram: h1 });
20+
const f2 = timerify(fn, { histogram: h2 });
21+
22+
assert.notEqual(f1, f2);

0 commit comments

Comments
 (0)