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: fix timerify bug #40625

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion doc/api/perf_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ changes:
to time async functions.
-->

* `fn` {Function}
* `fn` {Function} Only accept function and class. If the fn is a function(
includes "old style" class) will be executed directly or will be executed
by the `new` operator.
* `options` {Object}
* `histogram` {RecordableHistogram} A histogram object created using
`perf_hooks.createHistogram()` that will record runtime durations in
Expand Down
13 changes: 5 additions & 8 deletions lib/internal/perf/timerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ const {
isHistogram
} = require('internal/histogram');

const {
isConstructor,
} = internalBinding('util');

const {
codes: {
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -73,13 +69,14 @@ function timerify(fn, options = {}) {

if (fn[kTimerified]) return fn[kTimerified];

const constructor = isConstructor(fn);
// If the parameter is a function and it will be called directly
// or used as `new` operator.
const isCalledAsConstructor = (fn) => /^\s*class/.test(fn.toString());

function timerified(...args) {
const start = now();
const result = constructor ?
ReflectConstruct(fn, args, fn) :
ReflectApply(fn, this, args);
const result = isCalledAsConstructor(fn) ?
ReflectConstruct(fn, args, fn) : ReflectApply(fn, this, args);
Comment on lines +78 to +79
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const result = isCalledAsConstructor(fn) ?
ReflectConstruct(fn, args, fn) : ReflectApply(fn, this, args);
const result = new.target !== undefined ?
ReflectConstruct(fn, args, fn) :
ReflectApply(fn, this, args);

Copy link
Contributor

Choose a reason for hiding this comment

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

You marked this conversation as resolved but didn't take my suggestion. Is it because you disagree with it, or because you forgot to push?

Copy link
Contributor

Choose a reason for hiding this comment

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

@aduh95 wouldn't new.target not be undefined only if timerified() is called with new instead of fn()?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but that's the point. We want the timerified function to behave the same as the original function in both cases.

Copy link
Contributor

@RaisinTen RaisinTen Oct 31, 2021

Choose a reason for hiding this comment

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

Ah, we are returning timerified from timerify. Yea, this makes sense 👍

if (!constructor && typeof result?.finally === 'function') {
iam-frankqiu marked this conversation as resolved.
Show resolved Hide resolved
return result.finally(
FunctionPrototypeBind(
Expand Down
7 changes: 0 additions & 7 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,6 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
}

static void IsConstructor(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsFunction());
args.GetReturnValue().Set(args[0].As<v8::Function>()->IsConstructor());
}

static void ToUSVString(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_GE(args.Length(), 2);
Expand Down Expand Up @@ -344,7 +339,6 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(WeakReference::IncRef);
registry->Register(WeakReference::DecRef);
registry->Register(GuessHandleType);
registry->Register(IsConstructor);
registry->Register(ToUSVString);
}

Expand Down Expand Up @@ -384,7 +378,6 @@ void Initialize(Local<Object> target,
env->SetMethodNoSideEffect(target, "getConstructorName", GetConstructorName);
env->SetMethodNoSideEffect(target, "getExternalValue", GetExternalValue);
env->SetMethod(target, "sleep", Sleep);
env->SetMethodNoSideEffect(target, "isConstructor", IsConstructor);

env->SetMethod(target, "arrayBufferViewHasBuffer", ArrayBufferViewHasBuffer);
Local<Object> constants = Object::New(env->isolate());
Expand Down
24 changes: 24 additions & 0 deletions test/parallel/test-performance-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ const {
setTimeout: sleep
} = require('timers/promises');

{
const perf = performance.timerify(function foo() {
let sum = 0;
for (let i = 0; i < 101; i++) {
sum += i;
}
return sum;
});
const result = perf();
assert.strictEqual(result, 5050);
}

{
class Foo {
constructor() {
/* eslint-disable no-constructor-return */
return Promise.resolve('foo');
}
}
const perf = performance.timerify(Foo);
const result = perf();
result.then(common.mustCall((val) => assert.strictEqual(val, 'foo')));
}

{
// Intentional non-op. Do not wrap in common.mustCall();
const n = performance.timerify(function noop() {});
Expand Down