perf_hooks: load internal/errors eagerly

Since `internal/errors` is loaded by many builtin modules and is
currently the first module loaded during bootstrap, it is
fine to load it eagerly. We just need to make sure
that `internal/errors` itself load other modules lazily.

PR-URL: https://github.com/nodejs/node/pull/26771
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2019-03-19 18:17:49 +08:00 committed by Ruben Bridgewater
parent d81ea10f66
commit 245c96e7e5
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -36,6 +36,15 @@ const { AsyncResource } = require('async_hooks');
const L = require('internal/linkedlist'); const L = require('internal/linkedlist');
const kInspect = require('internal/util').customInspectSymbol; const kInspect = require('internal/util').customInspectSymbol;
const {
ERR_INVALID_CALLBACK,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE,
ERR_VALID_PERFORMANCE_ENTRY_TYPE,
ERR_INVALID_PERFORMANCE_MARK
} = require('internal/errors').codes;
const kHandle = Symbol('handle'); const kHandle = Symbol('handle');
const kMap = Symbol('map'); const kMap = Symbol('map');
const kCallback = Symbol('callback'); const kCallback = Symbol('callback');
@ -125,14 +134,6 @@ function collectHttp2Stats(entry) {
} }
} }
let errors;
function lazyErrors() {
if (errors === undefined)
errors = require('internal/errors').codes;
return errors;
}
function now() { function now() {
const hr = process.hrtime(); const hr = process.hrtime();
return hr[0] * 1000 + hr[1] / 1e6; return hr[0] * 1000 + hr[1] / 1e6;
@ -282,8 +283,7 @@ let gcTrackingIsEnabled = false;
class PerformanceObserver extends AsyncResource { class PerformanceObserver extends AsyncResource {
constructor(callback) { constructor(callback) {
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
const errors = lazyErrors(); throw new ERR_INVALID_CALLBACK();
throw new errors.ERR_INVALID_CALLBACK();
} }
super('PerformanceObserver'); super('PerformanceObserver');
Object.defineProperties(this, { Object.defineProperties(this, {
@ -329,16 +329,15 @@ class PerformanceObserver extends AsyncResource {
} }
observe(options) { observe(options) {
const errors = lazyErrors();
if (typeof options !== 'object' || options == null) { if (typeof options !== 'object' || options == null) {
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options); throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
} }
if (!Array.isArray(options.entryTypes)) { if (!Array.isArray(options.entryTypes)) {
throw new errors.ERR_INVALID_OPT_VALUE('entryTypes', options); throw new ERR_INVALID_OPT_VALUE('entryTypes', options);
} }
const entryTypes = options.entryTypes.filter(filterTypes).map(mapTypes); const entryTypes = options.entryTypes.filter(filterTypes).map(mapTypes);
if (entryTypes.length === 0) { if (entryTypes.length === 0) {
throw new errors.ERR_VALID_PERFORMANCE_ENTRY_TYPE(); throw new ERR_VALID_PERFORMANCE_ENTRY_TYPE();
} }
if (entryTypes.includes(NODE_PERFORMANCE_ENTRY_TYPE_GC) && if (entryTypes.includes(NODE_PERFORMANCE_ENTRY_TYPE_GC) &&
!gcTrackingIsEnabled) { !gcTrackingIsEnabled) {
@ -391,8 +390,7 @@ class Performance {
startMark = startMark !== undefined ? `${startMark}` : ''; startMark = startMark !== undefined ? `${startMark}` : '';
const marks = this[kIndex][kMarks]; const marks = this[kIndex][kMarks];
if (!marks.has(endMark) && !(endMark in nodeTiming)) { if (!marks.has(endMark) && !(endMark in nodeTiming)) {
const errors = lazyErrors(); throw new ERR_INVALID_PERFORMANCE_MARK(endMark);
throw new errors.ERR_INVALID_PERFORMANCE_MARK(endMark);
} }
_measure(name, startMark, endMark); _measure(name, startMark, endMark);
} }
@ -410,8 +408,7 @@ class Performance {
timerify(fn) { timerify(fn) {
if (typeof fn !== 'function') { if (typeof fn !== 'function') {
const errors = lazyErrors(); throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
throw new errors.ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
} }
if (fn[kTimerified]) if (fn[kTimerified])
return fn[kTimerified]; return fn[kTimerified];
@ -565,13 +562,11 @@ class ELDHistogram {
get stddev() { return this[kHandle].stddev(); } get stddev() { return this[kHandle].stddev(); }
percentile(percentile) { percentile(percentile) {
if (typeof percentile !== 'number') { if (typeof percentile !== 'number') {
const errors = lazyErrors(); throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
throw new errors.ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
} }
if (percentile <= 0 || percentile > 100) { if (percentile <= 0 || percentile > 100) {
const errors = lazyErrors(); throw new ERR_INVALID_ARG_VALUE.RangeError('percentile',
throw new errors.ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
percentile);
} }
return this[kHandle].percentile(percentile); return this[kHandle].percentile(percentile);
} }
@ -595,18 +590,15 @@ class ELDHistogram {
function monitorEventLoopDelay(options = {}) { function monitorEventLoopDelay(options = {}) {
if (typeof options !== 'object' || options === null) { if (typeof options !== 'object' || options === null) {
const errors = lazyErrors(); throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
throw new errors.ERR_INVALID_ARG_TYPE('options', 'Object', options);
} }
const { resolution = 10 } = options; const { resolution = 10 } = options;
if (typeof resolution !== 'number') { if (typeof resolution !== 'number') {
const errors = lazyErrors(); throw new ERR_INVALID_ARG_TYPE('options.resolution',
throw new errors.ERR_INVALID_ARG_TYPE('options.resolution', 'number', resolution);
'number', resolution);
} }
if (resolution <= 0 || !Number.isSafeInteger(resolution)) { if (resolution <= 0 || !Number.isSafeInteger(resolution)) {
const errors = lazyErrors(); throw new ERR_INVALID_OPT_VALUE.RangeError('resolution', resolution);
throw new errors.ERR_INVALID_OPT_VALUE.RangeError('resolution', resolution);
} }
return new ELDHistogram(new _ELDHistogram(resolution)); return new ELDHistogram(new _ELDHistogram(resolution));
} }