Skip to content

Commit

Permalink
perf: lazy bootstrap options - first pass (denoland#21164)
Browse files Browse the repository at this point in the history
Move most runtime options to be lazily loaded. Constant options will be
covered in a different PR.

Towards denoland#21133
  • Loading branch information
littledivy authored and zifeo committed Nov 22, 2023
1 parent becdd17 commit 7ae5eba
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 149 deletions.
1 change: 1 addition & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
deno_runtime::ops::signal::deno_signal::init_ops(),
deno_runtime::ops::tty::deno_tty::init_ops(),
deno_runtime::ops::http::deno_http_runtime::init_ops(),
deno_runtime::ops::bootstrap::deno_bootstrap::init_ops(),
cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm!
];

Expand Down
10 changes: 5 additions & 5 deletions ext/console/01_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ const {
isNaN,
} = primordials;

let noColor = false;
let noColor = () => false;

function setNoColor(value) {
noColor = value;
function setNoColorFn(fn) {
noColor = fn;
}

function getNoColor() {
return noColor;
return noColor();
}

function assert(cond, msg = "Assertion failed.") {
Expand Down Expand Up @@ -3646,7 +3646,7 @@ export {
inspect,
inspectArgs,
quoteString,
setNoColor,
setNoColorFn,
styles,
wrapConsole,
};
26 changes: 10 additions & 16 deletions runtime/js/06_util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
const {
Promise,
Expand All @@ -14,18 +16,18 @@ const LogLevel = {
Debug: 4,
};

let logLevel = 3;
let logSource = "JS";
const logSource = "JS";

function setLogLevel(level, source) {
logLevel = level;
if (source) {
logSource = source;
let logLevel_ = null;
function logLevel() {
if (logLevel_ === null) {
logLevel_ = ops.op_bootstrap_log_level() || 3;
}
return logLevel_;
}

function log(...args) {
if (logLevel >= LogLevel.Debug) {
if (logLevel() >= LogLevel.Debug) {
// if we destructure `console` off `globalThis` too early, we don't bind to
// the right console, therefore we don't log anything out.
globalThis.console.error(
Expand Down Expand Up @@ -83,12 +85,4 @@ function getterOnly(getter) {
};
}

export {
createResolvable,
getterOnly,
log,
nonEnumerable,
readOnly,
setLogLevel,
writable,
};
export { createResolvable, getterOnly, log, nonEnumerable, readOnly, writable };
43 changes: 21 additions & 22 deletions runtime/js/98_global_scope.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

const core = globalThis.Deno.core;
const ops = core.ops;
const primordials = globalThis.__bootstrap.primordials;
const {
ObjectDefineProperties,
Expand Down Expand Up @@ -157,51 +158,51 @@ class Navigator {

const navigator = webidl.createBranded(Navigator);

let numCpus, userAgent, language;

function setNumCpus(val) {
numCpus = val;
}

function setUserAgent(val) {
userAgent = val;
function memoizeLazy(f) {
let v_ = null;
return () => {
if (v_ === null) {
v_ = f();
}
return v_;
};
}

function setLanguage(val) {
language = val;
}
const numCpus = memoizeLazy(() => ops.op_bootstrap_numcpus());
const userAgent = memoizeLazy(() => ops.op_bootstrap_user_agent());
const language = memoizeLazy(() => ops.op_bootstrap_language());

ObjectDefineProperties(Navigator.prototype, {
hardwareConcurrency: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, NavigatorPrototype);
return numCpus;
return numCpus();
},
},
userAgent: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, NavigatorPrototype);
return userAgent;
return userAgent();
},
},
language: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, NavigatorPrototype);
return language;
return language();
},
},
languages: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, NavigatorPrototype);
return [language];
return [language()];
},
},
});
Expand All @@ -225,31 +226,31 @@ ObjectDefineProperties(WorkerNavigator.prototype, {
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return numCpus;
return numCpus();
},
},
userAgent: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return userAgent;
return userAgent();
},
},
language: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return language;
return language();
},
},
languages: {
configurable: true,
enumerable: true,
get() {
webidl.assertBranded(this, WorkerNavigatorPrototype);
return [language];
return [language()];
},
},
});
Expand Down Expand Up @@ -284,9 +285,7 @@ const workerRuntimeGlobalProperties = {

export {
mainRuntimeGlobalProperties,
setLanguage,
setNumCpus,
setUserAgent,
memoizeLazy,
unstableWindowOrWorkerGlobalScope,
windowOrWorkerGlobalScope,
workerRuntimeGlobalProperties,
Expand Down
Loading

0 comments on commit 7ae5eba

Please sign in to comment.