Skip to content

Commit

Permalink
perf_hooks: return different timerified function when different histo…
Browse files Browse the repository at this point in the history
…gram

Fixes: nodejs#42742
  • Loading branch information
himself65 committed Apr 24, 2022
1 parent c4781ea commit f6dc051
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion deps/v8/src/logging/counters.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StatsTable {
return lookup_function_(name);
}

// Create a histogram by name. If the create is successful,
// Create a histogram by name. If the creation is successful,
// returns a non-nullptr pointer for use with AddHistogramSample
// function. min and max define the expected minimum and maximum
// sample values. buckets is the maximum number of buckets
Expand Down
25 changes: 25 additions & 0 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ function isHistogram(object) {
return object?.[kHandle] !== undefined;
}

function* infinite() {
let id = 0;

while (true) {
yield id++;
}
}

const idWeakMap = new WeakMap();
const gen = infinite()

function getHistogramID(object) {
if (object == null) {
return -1;
}
if (idWeakMap.has(object)) {
return idWeakMap.get(object);
} else {
const id = gen.next().value;
idWeakMap.set(object, id);
return id
}
}

class Histogram {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
Expand Down Expand Up @@ -377,6 +401,7 @@ module.exports = {
internalHistogram,
internalRecordableHistogram,
isHistogram,
getHistogramID,
kDestroy,
kHandle,
kMap,
Expand Down
27 changes: 18 additions & 9 deletions lib/internal/perf/timerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const {
} = require('internal/validators');

const {
isHistogram
isHistogram,
getHistogramID
} = require('internal/histogram');

const {
Expand Down Expand Up @@ -71,7 +72,9 @@ function timerify(fn, options = {}) {
histogram);
}

if (fn[kTimerified]) return fn[kTimerified];
const id = getHistogramID(histogram)

if (fn[kTimerified]?.[id]) return fn[kTimerified][id];

const constructor = isConstructor(fn);

Expand Down Expand Up @@ -112,13 +115,19 @@ function timerify(fn, options = {}) {
}
});

ObjectDefineProperties(fn, {
[kTimerified]: {
configurable: false,
enumerable: false,
value: timerified,
}
});
if (fn[kTimerified]) {
fn[kTimerified][id] = timerified
} else {
ObjectDefineProperties(fn, {
[kTimerified]: {
configurable: false,
enumerable: false,
value: {
[id]: timerified
},
}
});
}

return timerified;
}
Expand Down

0 comments on commit f6dc051

Please sign in to comment.