Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
perf_hooks: add a test and drop isConstructor
  • Loading branch information
iam-frankqiu committed Oct 27, 2021
commit 304d5df1ad170983f929583a539bda879f0811a0
71 changes: 32 additions & 39 deletions lib/internal/perf/timerify.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,30 @@ const {
const { InternalPerformanceEntry } = require('internal/perf/performance_entry');
const { now } = require('internal/perf/utils');

const {
validateFunction,
validateObject,
} = require('internal/validators');
const { validateFunction, validateObject } = require('internal/validators');
Comment thread
iam-frankqiu marked this conversation as resolved.
Outdated

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

const {
codes: {
ERR_INVALID_ARG_TYPE,
},
codes: { ERR_INVALID_ARG_TYPE },
} = require('internal/errors');

const {
enqueue,
} = require('internal/perf/observe');
const { enqueue } = require('internal/perf/observe');

const kTimerified = Symbol('kTimerified');

function processComplete(name, start, args, histogram) {
const duration = now() - start;
if (histogram !== undefined)
histogram.record(MathCeil(duration * 1e6));
const entry =
new InternalPerformanceEntry(
name,
'function',
start,
duration,
args);

for (let n = 0; n < args.length; n++)
entry[n] = args[n];
if (histogram !== undefined) histogram.record(MathCeil(duration * 1e6));
const entry = new InternalPerformanceEntry(
name,
'function',
start,
duration,
args
);

for (let n = 0; n < args.length; n++) entry[n] = args[n];

enqueue(entry);
}
Expand All @@ -55,27 +44,29 @@ function timerify(fn, options = {}) {
validateFunction(fn, 'fn');

validateObject(options, 'options');
const {
histogram
} = options;
const { histogram } = options;

if (histogram !== undefined &&
(!isHistogram(histogram) || typeof histogram.record !== 'function')) {
if (
histogram !== undefined &&
(!isHistogram(histogram) || typeof histogram.record !== 'function')
) {
throw new ERR_INVALID_ARG_TYPE(
'options.histogram',
'RecordableHistogram',
histogram);
histogram
);
}

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

const isClass = (fn) => /^\s*class/.test(fn.toString());
// 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 = isClass(fn) ?
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

@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') {
Comment thread
iam-frankqiu marked this conversation as resolved.
return result.finally(
FunctionPrototypeBind(
Expand All @@ -84,7 +75,9 @@ function timerify(fn, options = {}) {
fn.name,
start,
args,
histogram));
histogram
)
);
}
processComplete(fn.name, start, args, histogram);
return result;
Expand All @@ -104,16 +97,16 @@ function timerify(fn, options = {}) {
name: {
configurable: false,
enumerable: true,
value: `timerified ${fn.name}`
}
value: `timerified ${fn.name}`,
},
});

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

return timerified;
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
12 changes: 12 additions & 0 deletions test/parallel/test-performance-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ const {
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((val) => assert.strictEqual(val, 'foo'));
Comment thread
iam-frankqiu marked this conversation as resolved.
Outdated
}

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