Skip to content

Commit f895b5a

Browse files
joyeecheungTrott
authored andcommitted
src: cache the result of GetOptions() in JS land
Instead of calling into C++ each time we need to check the value of a command line option, cache the option map in a new `internal/options` module for faster access to the values in JS land. PR-URL: #24091 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 350bef6 commit f895b5a

File tree

15 files changed

+49
-44
lines changed

15 files changed

+49
-44
lines changed

lib/crypto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const {
3535
ERR_CRYPTO_FIPS_UNAVAILABLE
3636
} = require('internal/errors').codes;
3737
const constants = internalBinding('constants').crypto;
38-
const { getOptions } = internalBinding('options');
39-
const pendingDeprecation = getOptions('--pending-deprecation');
38+
const { getOptionValue } = require('internal/options');
39+
const pendingDeprecation = getOptionValue('--pending-deprecation');
4040
const {
4141
fipsMode,
4242
fipsForced

lib/internal/bash_completion.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
2-
const { getOptions } = internalBinding('options');
2+
const { options, aliases } = require('internal/options');
33

44
function print(stream) {
5-
const { options, aliases } = getOptions();
65
const all_opts = [...options.keys(), ...aliases.keys()];
76

87
stream.write(`_node_complete() {

lib/internal/bootstrap/loaders.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@
251251

252252
NativeModule.isInternal = function(id) {
253253
return id.startsWith('internal/') ||
254-
(id === 'worker_threads' &&
255-
!internalBinding('options').getOptions('--experimental-worker'));
254+
(id === 'worker_threads' && !config.experimentalWorker);
256255
};
257256
}
258257

lib/internal/bootstrap/node.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@
103103
NativeModule.require('internal/inspector_async_hook').setup();
104104
}
105105

106-
const { getOptions } = internalBinding('options');
107-
const helpOption = getOptions('--help');
108-
const completionBashOption = getOptions('--completion-bash');
109-
const experimentalModulesOption = getOptions('--experimental-modules');
110-
const experimentalVMModulesOption = getOptions('--experimental-vm-modules');
111-
const experimentalWorkerOption = getOptions('--experimental-worker');
106+
const { getOptionValue } = NativeModule.require('internal/options');
107+
const helpOption = getOptionValue('--help');
108+
const completionBashOption = getOptionValue('--completion-bash');
109+
const experimentalModulesOption = getOptionValue('--experimental-modules');
110+
const experimentalVMModulesOption =
111+
getOptionValue('--experimental-vm-modules');
112+
const experimentalWorkerOption = getOptionValue('--experimental-worker');
112113
if (helpOption) {
113114
NativeModule.require('internal/print_help').print(process.stdout);
114115
return;
@@ -721,10 +722,9 @@
721722

722723
const get = () => {
723724
const {
724-
getOptions,
725725
envSettings: { kAllowedInEnvironment }
726726
} = internalBinding('options');
727-
const { options, aliases } = getOptions();
727+
const { options, aliases } = NativeModule.require('internal/options');
728728

729729
const allowedNodeEnvironmentFlags = [];
730730
for (const [name, info] of options) {

lib/internal/modules/cjs/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const {
99
CHAR_HASH,
1010
} = require('internal/constants');
1111

12-
const { getOptions } = internalBinding('options');
12+
const { getOptionValue } = require('internal/options');
1313

1414
// Invoke with makeRequireFunction(module) where |module| is the Module object
1515
// to use as the context for the require() function.
@@ -107,7 +107,7 @@ const builtinLibs = [
107107
'v8', 'vm', 'zlib'
108108
];
109109

110-
if (getOptions('--experimental-worker')) {
110+
if (getOptionValue('--experimental-worker')) {
111111
builtinLibs.push('worker_threads');
112112
builtinLibs.sort();
113113
}

lib/internal/modules/cjs/loader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ const {
4141
stripBOM,
4242
stripShebang
4343
} = require('internal/modules/cjs/helpers');
44-
const options = internalBinding('options');
45-
const preserveSymlinks = options.getOptions('--preserve-symlinks');
46-
const preserveSymlinksMain = options.getOptions('--preserve-symlinks-main');
47-
const experimentalModules = options.getOptions('--experimental-modules');
44+
const { getOptionValue } = require('internal/options');
45+
const preserveSymlinks = getOptionValue('--preserve-symlinks');
46+
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
47+
const experimentalModules = getOptionValue('--experimental-modules');
4848

4949
const {
5050
ERR_INVALID_ARG_TYPE,

lib/internal/modules/esm/default_resolve.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const internalFS = require('internal/fs/utils');
66
const { NativeModule } = require('internal/bootstrap/loaders');
77
const { extname } = require('path');
88
const { realpathSync } = require('fs');
9-
const { getOptions } = internalBinding('options');
10-
const preserveSymlinks = getOptions('--preserve-symlinks');
11-
const preserveSymlinksMain = getOptions('--preserve-symlinks-main');
9+
const { getOptionValue } = require('internal/options');
10+
const preserveSymlinks = getOptionValue('--preserve-symlinks');
11+
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
1212
const {
1313
ERR_MISSING_MODULE,
1414
ERR_MODULE_RESOLUTION_LEGACY,

lib/internal/options.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const { getOptions } = internalBinding('options');
4+
const { options, aliases } = getOptions();
5+
6+
function getOptionValue(option) {
7+
const result = options.get(option);
8+
if (!result) {
9+
return undefined;
10+
}
11+
return result.value;
12+
}
13+
14+
module.exports = {
15+
options,
16+
aliases,
17+
getOptionValue
18+
};

lib/internal/print_help.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
const { getOptions, types } = internalBinding('options');
2+
3+
const { types } = internalBinding('options');
34

45
const typeLookup = [];
56
for (const key of Object.keys(types))
@@ -132,7 +133,7 @@ function format({ options, aliases = new Map(), firstColumn, secondColumn }) {
132133
}
133134

134135
function print(stream) {
135-
const { options, aliases } = getOptions();
136+
const { options, aliases } = require('internal/options');
136137

137138
// Use 75 % of the available width, and at least 70 characters.
138139
const width = Math.max(70, (stream.columns || 0) * 0.75);

lib/internal/process/esm_loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ exports.ESMLoader = undefined;
4848
exports.setup = function() {
4949
let ESMLoader = new Loader();
5050
const loaderPromise = (async () => {
51-
const userLoader = internalBinding('options').getOptions('--loader');
51+
const userLoader = require('internal/options').getOptionValue('--loader');
5252
if (userLoader) {
5353
const hooks = await ESMLoader.import(
5454
userLoader, pathToFileURL(`${process.cwd()}/`).href);

0 commit comments

Comments
 (0)