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

performance.timerify(fn, options) always return the same timerifed function #42742

Closed
hax opened this issue Apr 14, 2022 · 3 comments
Closed
Labels
confirmed-bug Issues with confirmed bugs. perf_hooks Issues and PRs related to the implementation of the Performance Timing API.

Comments

@hax
Copy link
Contributor

hax commented Apr 14, 2022

Version

master branch

Platform

All

Subsystem

perf_hooks

What steps will reproduce the bug?

function f() {}
let h1 = perf_hooks.createHistogram()
let h2 = perf_hooks.createHistogram()
let f1 = perf_hooks.performance.timerify(f, {histogram: h1})
let f2 = perf_hooks.performance.timerify(f, {histogram: h2})
f1 !== f2 // expect true, actual false

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

Every calls of performance.timerify(fn) should return a new timerified function instance for fn, at least if options is provided.

What do you see instead?

Currently timerify will cache the timerfied function as fn[kTimerified], so timerify(fn) always return same timerified function for fn, even different options was given.

Additional information

Current cache behavior is not very useful, and cause problems for the use cases which need multiple timerified versions (for example, record timing for different usage of the same function).

PS. It also will throw if fn is frozen/sealed/non-extensible.

@VoltrexKeyva VoltrexKeyva added the perf_hooks Issues and PRs related to the implementation of the Performance Timing API. label Apr 14, 2022
@himself65 himself65 added the confirmed-bug Issues with confirmed bugs. label Apr 23, 2022
@himself65
Copy link
Member

Related:

if (fn[kTimerified]) return fn[kTimerified];
const constructor = isConstructor(fn);
function timerified(...args) {
const start = now();
const result = constructor ?
ReflectConstruct(fn, args, fn) :
ReflectApply(fn, this, args);
if (!constructor && typeof result?.finally === 'function') {
return result.finally(
FunctionPrototypeBind(
processComplete,
result,
fn.name,
start,
args,
histogram));
}
processComplete(fn.name, start, args, histogram);
return result;
}
ObjectDefineProperties(timerified, {
[kTimerified]: {
configurable: false,
enumerable: false,
value: timerified,
},
length: {
configurable: false,
enumerable: true,
value: fn.length,
},
name: {
configurable: false,
enumerable: true,
value: `timerified ${fn.name}`
}
});
ObjectDefineProperties(fn, {
[kTimerified]: {
configurable: false,
enumerable: false,
value: timerified,
}
});
return timerified;

@himself65
Copy link
Member

Upstream PR: #37136

It seems like the memorization is for the performance

@himself65
Copy link
Member

I'm working on this issue

himself65 added a commit to himself65/node that referenced this issue Apr 24, 2022
himself65 added a commit to himself65/node that referenced this issue Apr 24, 2022
himself65 added a commit to himself65/node that referenced this issue Apr 28, 2022
himself65 added a commit to himself65/node that referenced this issue Apr 28, 2022
himself65 added a commit to himself65/node that referenced this issue Apr 29, 2022
himself65 added a commit to himself65/node that referenced this issue Apr 30, 2022
targos pushed a commit that referenced this issue May 2, 2022
Fixes: #42742

PR-URL: #42854
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Co-authored-by: HE Shi-Jun <[email protected]>
juanarbol pushed a commit that referenced this issue May 31, 2022
Fixes: #42742

PR-URL: #42854
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Co-authored-by: HE Shi-Jun <[email protected]>
danielleadams pushed a commit that referenced this issue Jun 27, 2022
Fixes: #42742

PR-URL: #42854
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Co-authored-by: HE Shi-Jun <[email protected]>
targos pushed a commit that referenced this issue Jul 12, 2022
Fixes: #42742

PR-URL: #42854
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Co-authored-by: HE Shi-Jun <[email protected]>
targos pushed a commit that referenced this issue Jul 31, 2022
Fixes: #42742

PR-URL: #42854
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Co-authored-by: HE Shi-Jun <[email protected]>
guangwong pushed a commit to noslate-project/node that referenced this issue Oct 10, 2022
Fixes: nodejs/node#42742

PR-URL: nodejs/node#42854
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Co-authored-by: HE Shi-Jun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. perf_hooks Issues and PRs related to the implementation of the Performance Timing API.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants