-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
performance: fix timerify bug #40625
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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'); | ||||||||||||
|
|
||||||||||||
| 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); | ||||||||||||
| } | ||||||||||||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aduh95 wouldn't
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but that's the point. We want the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, we are returning |
||||||||||||
| if (!constructor && typeof result?.finally === 'function') { | ||||||||||||
|
iam-frankqiu marked this conversation as resolved.
|
||||||||||||
| return result.finally( | ||||||||||||
| FunctionPrototypeBind( | ||||||||||||
|
|
@@ -84,7 +75,9 @@ function timerify(fn, options = {}) { | |||||||||||
| fn.name, | ||||||||||||
| start, | ||||||||||||
| args, | ||||||||||||
| histogram)); | ||||||||||||
| histogram | ||||||||||||
| ) | ||||||||||||
| ); | ||||||||||||
| } | ||||||||||||
| processComplete(fn.name, start, args, histogram); | ||||||||||||
| return result; | ||||||||||||
|
|
@@ -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; | ||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.