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
Next Next commit
experiment: lazy bootstrap options
  • Loading branch information
littledivy committed Nov 11, 2023
commit 69ca03a63ef94116254b304b3a48667baeae7633
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,
};
24 changes: 9 additions & 15 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";

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 };
35 changes: 17 additions & 18 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 Down Expand Up @@ -284,9 +285,7 @@ const workerRuntimeGlobalProperties = {

export {
mainRuntimeGlobalProperties,
setLanguage,
setNumCpus,
setUserAgent,
makeLazy,
unstableWindowOrWorkerGlobalScope,
windowOrWorkerGlobalScope,
workerRuntimeGlobalProperties,
Expand Down
111 changes: 33 additions & 78 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
getNoColor,
inspectArgs,
quoteString,
setNoColor,
setNoColorFn,
wrapConsole,
} from "ext:deno_console/01_console.js";
import * as performance from "ext:deno_web/15_performance.js";
Expand All @@ -67,9 +67,7 @@ import * as webidl from "ext:deno_webidl/00_webidl.js";
import DOMException from "ext:deno_web/01_dom_exception.js";
import {
mainRuntimeGlobalProperties,
setLanguage,
setNumCpus,
setUserAgent,
makeLazy,
unstableWindowOrWorkerGlobalScope,
windowOrWorkerGlobalScope,
workerRuntimeGlobalProperties,
Expand Down Expand Up @@ -241,6 +239,11 @@ function opMainModule() {
return ops.op_main_module();
}

const opArgs = makeLazy(() => ops.op_bootstrap_args());
const opPid = makeLazy(() => ops.op_bootstrap_pid());
const opPpid = makeLazy(() => ops.op_ppid());
setNoColorFn(() => ops.op_bootstrap_no_color());

function formatException(error) {
if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, error)) {
return null;
Expand Down Expand Up @@ -327,10 +330,6 @@ function runtimeStart(
v8Version,
tsVersion,
target,
logLevel,
noColor,
isTty,
source,
) {
core.setMacrotaskCallback(timers.handleTimerMacrotask);
core.setMacrotaskCallback(promiseRejectMacrotaskCallback);
Expand All @@ -343,8 +342,6 @@ function runtimeStart(
tsVersion,
);
core.setBuildInfo(target);
util.setLogLevel(logLevel, source);
setNoColor(noColor || !isTty);
}

const pendingRejections = [];
Expand Down Expand Up @@ -461,25 +458,16 @@ function bootstrapMainRuntime(runtimeOptions) {
const nodeBootstrap = globalThis.nodeBootstrap;

const {
0: args,
1: cpuCount,
2: logLevel,
3: denoVersion,
4: locale,
5: location_,
6: noColor,
7: isTty,
8: tsVersion,
9: unstableFlag,
10: unstableFeatures,
11: pid,
12: target,
13: v8Version,
14: userAgent,
15: inspectFlag,
// 16: enableTestingFeaturesFlag
17: hasNodeModulesDir,
18: maybeBinaryNpmCommandName,
0: denoVersion,
1: location_,
2: tsVersion,
3: unstableFlag,
4: unstableFeatures,
5: target,
6: v8Version,
7: inspectFlag,
8: hasNodeModulesDir,
9: maybeBinaryNpmCommandName,
} = runtimeOptions;

performance.setTimeOrigin(DateNow());
Expand Down Expand Up @@ -538,27 +526,13 @@ function bootstrapMainRuntime(runtimeOptions) {
v8Version,
tsVersion,
target,
logLevel,
noColor,
isTty,
);

setNumCpus(cpuCount);
setUserAgent(userAgent);
setLanguage(locale);

let ppid = undefined;
ObjectDefineProperties(finalDenoNs, {
pid: util.readOnly(pid),
ppid: util.getterOnly(() => {
// lazy because it's expensive
if (ppid === undefined) {
ppid = ops.op_ppid();
}
return ppid;
}),
noColor: util.readOnly(noColor),
args: util.readOnly(ObjectFreeze(args)),
pid: util.getterOnly(opPid),
ppid: util.getterOnly(opPpid),
noColor: util.getterOnly(getNoColor),
args: util.getterOnly(opArgs),
mainModule: util.getterOnly(opMainModule),
});

Expand Down Expand Up @@ -593,8 +567,6 @@ function bootstrapMainRuntime(runtimeOptions) {
// `Deno` with `Deno` namespace from "./deno.ts".
ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs));

util.log("args", args);

if (nodeBootstrap) {
nodeBootstrap(hasNodeModulesDir, maybeBinaryNpmCommandName);
}
Expand All @@ -612,25 +584,15 @@ function bootstrapWorkerRuntime(
const nodeBootstrap = globalThis.nodeBootstrap;

const {
0: args,
1: cpuCount,
2: logLevel,
3: denoVersion,
4: locale,
5: location_,
6: noColor,
7: isTty,
8: tsVersion,
9: unstableFlag,
10: unstableFeatures,
11: pid,
12: target,
13: v8Version,
14: userAgent,
// 15: inspectFlag,
16: enableTestingFeaturesFlag,
17: hasNodeModulesDir,
18: maybeBinaryNpmCommandName,
0: denoVersion,
1: location_,
2: tsVersion,
3: unstableFlag,
4: unstableFeatures,
5: target,
6: v8Version,
8: hasNodeModulesDir,
9: maybeBinaryNpmCommandName,
} = runtimeOptions;

performance.setTimeOrigin(DateNow());
Expand Down Expand Up @@ -686,18 +648,11 @@ function bootstrapWorkerRuntime(
v8Version,
tsVersion,
target,
logLevel,
noColor,
isTty,
internalName ?? name,
);

location.setLocationHref(location_);

setNumCpus(cpuCount);
setUserAgent(userAgent);
setLanguage(locale);

globalThis.pollForMessages = pollForMessages;

// TODO(bartlomieju): deprecate --unstable
Expand All @@ -710,9 +665,9 @@ function bootstrapWorkerRuntime(
}
}
ObjectDefineProperties(finalDenoNs, {
pid: util.readOnly(pid),
noColor: util.readOnly(noColor),
args: util.readOnly(ObjectFreeze(args)),
pid: util.getterOnly(opPid),
noColor: util.getterOnly(getNoColor),
args: util.getterOnly(opArgs),
});
// Setup `Deno` global - we're actually overriding already
// existing global `Deno` with `Deno` namespace from "./deno.ts".
Expand Down
Loading
Loading