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: static bootstrap options in snapshot #21213

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
perf: static bootstrap options in snapshot
  • Loading branch information
littledivy committed Nov 15, 2023
commit c051d4175e378cd7ced493be903e9c5b8e744daa
18 changes: 17 additions & 1 deletion cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,18 @@ fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
use deno_runtime::deno_cron::local::LocalCronHandler;
use deno_runtime::deno_http::DefaultHttpPropertyExtractor;
use deno_runtime::deno_kv::sqlite::SqliteDbHandler;
use deno_runtime::ops::bootstrap::SnapshotOptions;
use deno_runtime::permissions::PermissionsContainer;
use std::sync::Arc;

fn deno_version() -> String {
if env::var("DENO_CANARY").is_ok() {
format!("{}+{}", env!("CARGO_PKG_VERSION"), git_commit_hash())
} else {
env!("CARGO_PKG_VERSION").to_string()
}
}
Comment on lines +359 to +365
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this duplicated in cli/version.rs? Could we maybe reuse some of the code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

env!(DENO_CANARY) macro won't work from build.rs


// NOTE(bartlomieju): ordering is important here, keep it in sync with
// `runtime/worker.rs`, `runtime/web_worker.rs` and `runtime/build.rs`!
let fs = Arc::new(deno_fs::RealFs);
Expand Down Expand Up @@ -405,7 +414,14 @@ 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(),
deno_runtime::ops::bootstrap::deno_bootstrap::init_ops(Some(
SnapshotOptions {
deno_version: deno_version(),
ts_version: ts::version(),
v8_version: deno_core::v8_version(),
target: std::env::var("TARGET").unwrap(),
},
)),
cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm!
];

Expand Down
16 changes: 8 additions & 8 deletions runtime/js/99_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,20 +450,24 @@ const finalDenoNs = {
...denoNs,
};

const {
denoVersion,
tsVersion,
v8Version,
target,
} = ops.op_snapshot_options();

function bootstrapMainRuntime(runtimeOptions) {
if (hasBootstrapped) {
throw new Error("Worker runtime already bootstrapped");
}
const nodeBootstrap = globalThis.nodeBootstrap;

// location, unstable, node module dir
const {
0: denoVersion,
1: location_,
2: tsVersion,
3: unstableFlag,
4: unstableFeatures,
5: target,
6: v8Version,
7: inspectFlag,
9: hasNodeModulesDir,
10: maybeBinaryNpmCommandName,
Expand Down Expand Up @@ -583,13 +587,9 @@ function bootstrapWorkerRuntime(
const nodeBootstrap = globalThis.nodeBootstrap;

const {
0: denoVersion,
1: location_,
2: tsVersion,
3: unstableFlag,
4: unstableFeatures,
5: target,
6: v8Version,
8: enableTestingFeaturesFlag,
9: hasNodeModulesDir,
10: maybeBinaryNpmCommandName,
Expand Down
26 changes: 26 additions & 0 deletions runtime/ops/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use deno_core::op2;
use deno_core::OpState;
use serde::Serialize;

use crate::BootstrapOptions;

Expand All @@ -16,9 +17,34 @@ deno_core::extension!(
op_bootstrap_log_level,
op_bootstrap_no_color,
op_bootstrap_is_tty,
op_snapshot_options,
],
options = {
snapshot_options: Option<SnapshotOptions>,
},
state = |state, options| {
if let Some(snapshot_options) = options.snapshot_options {
state.put::<SnapshotOptions>(snapshot_options);
}
},
);

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SnapshotOptions {
pub deno_version: String,
pub ts_version: String,
pub v8_version: &'static str,
pub target: String,
}

// Note: Called at snapshot time, op perf is not a concern.
#[op2]
#[serde]
pub fn op_snapshot_options(state: &mut OpState) -> SnapshotOptions {
state.take::<SnapshotOptions>()
}

#[op2]
#[serde]
pub fn op_bootstrap_args(state: &mut OpState) -> Vec<String> {
Expand Down
2 changes: 1 addition & 1 deletion runtime/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub fn create_runtime_snapshot(snapshot_path: PathBuf) {
ops::signal::deno_signal::init_ops(),
ops::tty::deno_tty::init_ops(),
ops::http::deno_http_runtime::init_ops(),
ops::bootstrap::deno_bootstrap::init_ops(),
ops::bootstrap::deno_bootstrap::init_ops(None),
];

for extension in &mut extensions {
Expand Down
2 changes: 1 addition & 1 deletion runtime/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl WebWorker {
ops::signal::deno_signal::init_ops_and_esm(),
ops::tty::deno_tty::init_ops_and_esm(),
ops::http::deno_http_runtime::init_ops_and_esm(),
ops::bootstrap::deno_bootstrap::init_ops_and_esm(),
ops::bootstrap::deno_bootstrap::init_ops_and_esm(None),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like it, I wish we had an easy way to add a custom function just for snapshotting :(

deno_permissions_web_worker::init_ops_and_esm(
permissions,
enable_testing_features,
Expand Down
2 changes: 1 addition & 1 deletion runtime/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl MainWorker {
ops::signal::deno_signal::init_ops_and_esm(),
ops::tty::deno_tty::init_ops_and_esm(),
ops::http::deno_http_runtime::init_ops_and_esm(),
ops::bootstrap::deno_bootstrap::init_ops_and_esm(),
ops::bootstrap::deno_bootstrap::init_ops_and_esm(None),
deno_permissions_worker::init_ops_and_esm(
permissions,
enable_testing_features,
Expand Down
Loading