Skip to content
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

perf: lazy bootstrap options - first pass #21164

Merged
merged 10 commits into from
Nov 13, 2023
Merged
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 makeLazy(f) {
littledivy marked this conversation as resolved.
Show resolved Hide resolved
let v_ = null;
return () => {
if (v_ === null) {
v_ = f();
}
return v_;
};
}

function setLanguage(val) {
language = val;
}
const numCpus = makeLazy(() => ops.op_bootstrap_numcpus());
const userAgent = makeLazy(() => ops.op_bootstrap_user_agent());
const language = makeLazy(() => 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,
makeLazy,
unstableWindowOrWorkerGlobalScope,
windowOrWorkerGlobalScope,
workerRuntimeGlobalProperties,
Expand Down
Loading