Skip to content

Commit a9053c3

Browse files
joyeecheungBridgeAR
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 <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 1225a0a commit a9053c3

15 files changed

+49
-44
lines changed

lib/crypto.js

+2-2
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

+1-2
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

+1-2
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

+8-8
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

+2-2
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

+4-4
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

+3-3
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

+18
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

+3-2
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

+1-1
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);

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const {
7070
ERR_SCRIPT_EXECUTION_INTERRUPTED
7171
} = require('internal/errors').codes;
7272
const { sendInspectorCommand } = require('internal/util/inspector');
73-
const experimentalREPLAwait = internalBinding('options').getOptions(
73+
const experimentalREPLAwait = require('internal/options').getOptionValue(
7474
'--experimental-repl-await'
7575
);
7676
const { isRecoverableError } = require('internal/repl/recoverable');

lib/vm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ module.exports = {
418418
compileFunction,
419419
};
420420

421-
if (internalBinding('options').getOptions('--experimental-vm-modules')) {
421+
if (require('internal/options').getOptionValue('--experimental-vm-modules')) {
422422
const { SourceTextModule } = require('internal/vm/source_text_module');
423423
module.exports.SourceTextModule = SourceTextModule;
424424
}

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
'lib/internal/modules/esm/translators.js',
134134
'lib/internal/safe_globals.js',
135135
'lib/internal/net.js',
136+
'lib/internal/options.js',
136137
'lib/internal/print_help.js',
137138
'lib/internal/priority_queue.js',
138139
'lib/internal/process/esm_loader.js',

src/node_options.cc

+2-15
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,8 @@ HostPort SplitHostPort(const std::string& arg,
365365
ParseAndValidatePort(arg.substr(colon + 1), errors) };
366366
}
367367

368-
// Usage: Either:
369-
// - getOptions() to get all options + metadata or
370-
// - getOptions(string) to get the value of a particular option
368+
// Return a map containing all the options and their metadata as well
369+
// as the aliases
371370
void GetOptions(const FunctionCallbackInfo<Value>& args) {
372371
Mutex::ScopedLock lock(per_process_opts_mutex);
373372
Environment* env = Environment::GetCurrent(args);
@@ -388,13 +387,8 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
388387

389388
const auto& parser = PerProcessOptionsParser::instance;
390389

391-
std::string filter;
392-
if (args[0]->IsString()) filter = *node::Utf8Value(isolate, args[0]);
393-
394390
Local<Map> options = Map::New(isolate);
395391
for (const auto& item : parser.options_) {
396-
if (!filter.empty() && item.first != filter) continue;
397-
398392
Local<Value> value;
399393
const auto& option_info = item.second;
400394
auto field = option_info.field;
@@ -443,11 +437,6 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
443437
}
444438
CHECK(!value.IsEmpty());
445439

446-
if (!filter.empty()) {
447-
args.GetReturnValue().Set(value);
448-
return;
449-
}
450-
451440
Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
452441
Local<Object> info = Object::New(isolate);
453442
Local<Value> help_text;
@@ -469,8 +458,6 @@ void GetOptions(const FunctionCallbackInfo<Value>& args) {
469458
}
470459
}
471460

472-
if (!filter.empty()) return;
473-
474461
Local<Value> aliases;
475462
if (!ToV8Value(context, parser.aliases_).ToLocal(&aliases)) return;
476463

test/parallel/test-bootstrap-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ const list = process.moduleLoadList.slice();
1111

1212
const assert = require('assert');
1313

14-
assert(list.length <= 77, list);
14+
assert(list.length <= 78, list);

0 commit comments

Comments
 (0)