0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

build: add "include_js_files_for_snapshotting" Cargo feature (#17826)

This allows to not include source code into the binary (because
it will already be included in the V8 snapshot).

Nothing changes for the embedders - everything should still build the
same.

This commit brings the binary size from 87Mb to 82Mb on M1.

Alternative to https://github.com/denoland/deno/pull/17820 and
https://github.com/denoland/deno/pull/17653

---------

Co-authored-by: Leo Kettmeir <crowlkats@toaxl.com>
This commit is contained in:
Bartek Iwańczuk 2023-02-20 21:45:34 +01:00 committed by GitHub
parent 4d1a14ca7f
commit 914b08fc19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 125 additions and 49 deletions

View file

@ -27,8 +27,8 @@ harness = false
path = "./bench/lsp_bench_standalone.rs" path = "./bench/lsp_bench_standalone.rs"
[build-dependencies] [build-dependencies]
deno_runtime = { workspace = true, features = ["snapshot_from_snapshot"] } deno_runtime = { workspace = true, features = ["snapshot_from_snapshot", "include_js_files_for_snapshotting"] }
deno_core.workspace = true deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
regex.workspace = true regex.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
@ -43,13 +43,13 @@ winres.workspace = true
[dependencies] [dependencies]
deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] } deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_core.workspace = true deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
deno_doc = "0.55.0" deno_doc = "0.55.0"
deno_emit = "0.15.0" deno_emit = "0.15.0"
deno_graph = "0.43.2" deno_graph = "0.43.2"
deno_lint = { version = "0.38.0", features = ["docs"] } deno_lint = { version = "0.38.0", features = ["docs"] }
deno_lockfile.workspace = true deno_lockfile.workspace = true
deno_runtime.workspace = true deno_runtime = { workspace = true, features = ["dont_create_runtime_snapshot", "include_js_files_for_snapshotting"] }
deno_task_shell = "0.8.1" deno_task_shell = "0.8.1"
napi_sym.workspace = true napi_sym.workspace = true

View file

@ -360,8 +360,8 @@ fn create_cli_snapshot(snapshot_path: PathBuf) {
); );
esm_files.push(ExtensionFileSource { esm_files.push(ExtensionFileSource {
specifier: "runtime/js/99_main.js".to_string(), specifier: "runtime/js/99_main.js".to_string(),
code: ExtensionFileSourceCode::IncludedInBinary( code: ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
deno_runtime::js::SOURCE_CODE_FOR_99_MAIN_JS, std::path::PathBuf::from(deno_runtime::js::PATH_FOR_99_MAIN_JS),
), ),
}); });
let extensions_with_js = vec![Extension::builder("cli") let extensions_with_js = vec![Extension::builder("cli")

View file

@ -16,6 +16,7 @@ path = "lib.rs"
[features] [features]
default = ["v8_use_custom_libcxx"] default = ["v8_use_custom_libcxx"]
v8_use_custom_libcxx = ["v8/use_custom_libcxx"] v8_use_custom_libcxx = ["v8/use_custom_libcxx"]
include_js_files_for_snapshotting = []
[dependencies] [dependencies]
anyhow.workspace = true anyhow.workspace = true

View file

@ -1,7 +1,9 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::OpState; use crate::OpState;
use anyhow::Context as _;
use anyhow::Error; use anyhow::Error;
use std::cell::RefCell; use std::cell::RefCell;
use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use std::task::Context; use std::task::Context;
use v8::fast_api::FastFunction; use v8::fast_api::FastFunction;
@ -13,8 +15,24 @@ pub enum ExtensionFileSourceCode {
/// will result in two copies of the source code being included - one in the /// will result in two copies of the source code being included - one in the
/// snapshot, the other the static string in the `Extension`. /// snapshot, the other the static string in the `Extension`.
IncludedInBinary(&'static str), IncludedInBinary(&'static str),
// TODO(bartlomieju): add more variants that allow to read file from the disk,
// and not include it in the binary. // Source code is loaded from a file on disk. It's meant to be used if the
// embedder is creating snapshots. Files will be loaded from the filesystem
// during the build time and they will only be present in the V8 snapshot.
LoadedFromFsDuringSnapshot(PathBuf),
}
impl ExtensionFileSourceCode {
pub fn load(&self) -> Result<String, Error> {
match self {
ExtensionFileSourceCode::IncludedInBinary(code) => Ok(code.to_string()),
ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(path) => {
let msg = format!("Failed to read \"{}\"", path.display());
let code = std::fs::read_to_string(path).context(msg)?;
Ok(code)
}
}
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -299,6 +317,7 @@ impl ExtensionBuilder {
/// - "internal:my_extension/js/01_hello.js" /// - "internal:my_extension/js/01_hello.js"
/// - "internal:my_extension/js/02_goodbye.js" /// - "internal:my_extension/js/02_goodbye.js"
/// ``` /// ```
#[cfg(not(feature = "include_js_files_for_snapshotting"))]
#[macro_export] #[macro_export]
macro_rules! include_js_files { macro_rules! include_js_files {
(dir $dir:literal, $($file:literal,)+) => { (dir $dir:literal, $($file:literal,)+) => {
@ -323,3 +342,29 @@ macro_rules! include_js_files {
] ]
}; };
} }
#[cfg(feature = "include_js_files_for_snapshotting")]
#[macro_export]
macro_rules! include_js_files {
(dir $dir:literal, $($file:literal,)+) => {
vec![
$($crate::ExtensionFileSource {
specifier: concat!($dir, "/", $file).to_string(),
code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($dir).join($file)
),
},)+
]
};
($($file:literal,)+) => {
vec![
$($crate::ExtensionFileSource {
specifier: $file.to_string(),
code: $crate::ExtensionFileSourceCode::LoadedFromFsDuringSnapshot(
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join($file)
),
},)+
]
};
}

View file

@ -3,7 +3,6 @@
use crate::bindings; use crate::bindings;
use crate::error::generic_error; use crate::error::generic_error;
use crate::extensions::ExtensionFileSource; use crate::extensions::ExtensionFileSource;
use crate::extensions::ExtensionFileSourceCode;
use crate::module_specifier::ModuleSpecifier; use crate::module_specifier::ModuleSpecifier;
use crate::resolve_import; use crate::resolve_import;
use crate::resolve_url; use crate::resolve_url;
@ -403,10 +402,9 @@ impl ModuleLoader for InternalModuleLoader {
let result = if let Some(load_callback) = &self.maybe_load_callback { let result = if let Some(load_callback) = &self.maybe_load_callback {
load_callback(file_source) load_callback(file_source)
} else { } else {
match file_source.code { match file_source.code.load() {
ExtensionFileSourceCode::IncludedInBinary(code) => { Ok(code) => Ok(code),
Ok(code.to_string()) Err(err) => return futures::future::err(err).boxed_local(),
}
} }
}; };

View file

@ -21,7 +21,6 @@ use crate::source_map::SourceMapCache;
use crate::source_map::SourceMapGetter; use crate::source_map::SourceMapGetter;
use crate::Extension; use crate::Extension;
use crate::ExtensionFileSource; use crate::ExtensionFileSource;
use crate::ExtensionFileSourceCode;
use crate::NoopModuleLoader; use crate::NoopModuleLoader;
use crate::OpMiddlewareFn; use crate::OpMiddlewareFn;
use crate::OpResult; use crate::OpResult;
@ -869,14 +868,11 @@ impl JsRuntime {
{ {
let js_files = ext.get_js_sources(); let js_files = ext.get_js_sources();
for file_source in js_files { for file_source in js_files {
let ExtensionFileSourceCode::IncludedInBinary(code) =
file_source.code;
// TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap // TODO(@AaronO): use JsRuntime::execute_static() here to move src off heap
realm.execute_script( realm.execute_script(
self.v8_isolate(), self.v8_isolate(),
&file_source.specifier, &file_source.specifier,
code, &file_source.code.load()?,
)?; )?;
} }
} }

View file

@ -33,6 +33,12 @@ pub fn create_snapshot(create_snapshot_options: CreateSnapshotOptions) {
snapshot_module_load_cb: create_snapshot_options.snapshot_module_load_cb, snapshot_module_load_cb: create_snapshot_options.snapshot_module_load_cb,
..Default::default() ..Default::default()
}); });
println!(
"JsRuntime for snapshot prepared, took {:#?} ({})",
Instant::now().saturating_duration_since(mark),
create_snapshot_options.snapshot_path.display()
);
mark = Instant::now();
let snapshot = js_runtime.snapshot(); let snapshot = js_runtime.snapshot();
let snapshot_slice: &[u8] = &snapshot; let snapshot_slice: &[u8] = &snapshot;

View file

@ -12,8 +12,17 @@ description = "Provides the deno runtime library"
[features] [features]
# "fake" feature that allows to generate docs on docs.rs # "fake" feature that allows to generate docs on docs.rs
docsrs = [] docsrs = []
# feature that modifies the snapshot to allow extending it # A feature that disables creation of startup snapshot during in the build script.
dont_create_runtime_snapshot = []
# A feature that changes how startup snapshot is generated, that allows
# extending it in embedder crates.
snapshot_from_snapshot = [] snapshot_from_snapshot = []
# A feature that disables embedding of the JavaScript source files in the binary.
# With this feature enabled, the sources must be consumed during build time,
# by creating a startup snapshot.
include_js_files_for_snapshotting = [
"deno_core/include_js_files_for_snapshotting",
]
[lib] [lib]
name = "deno_runtime" name = "deno_runtime"

View file

@ -1,24 +1,25 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use deno_core::include_js_files;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
// This is a shim that allows to generate documentation on docs.rs #[cfg(all(
mod not_docs { not(feature = "docsrs"),
not(feature = "dont_create_runtime_snapshot")
))]
mod startup_snapshot {
use std::path::Path; use std::path::Path;
use super::*; use super::*;
use deno_cache::SqliteBackedCache;
use deno_core::snapshot_util::*;
use deno_core::Extension;
use deno_ast::MediaType; use deno_ast::MediaType;
use deno_ast::ParseParams; use deno_ast::ParseParams;
use deno_ast::SourceTextInfo; use deno_ast::SourceTextInfo;
use deno_cache::SqliteBackedCache;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::include_js_files;
use deno_core::snapshot_util::*;
use deno_core::Extension;
use deno_core::ExtensionFileSource; use deno_core::ExtensionFileSource;
use deno_core::ExtensionFileSourceCode;
fn transpile_ts_for_snapshotting( fn transpile_ts_for_snapshotting(
file_source: &ExtensionFileSource, file_source: &ExtensionFileSource,
@ -34,16 +35,15 @@ mod not_docs {
file_source.specifier file_source.specifier
), ),
}; };
let code = file_source.code.load()?;
let ExtensionFileSourceCode::IncludedInBinary(code) = file_source.code;
if !should_transpile { if !should_transpile {
return Ok(code.to_string()); return Ok(code);
} }
let parsed = deno_ast::parse_module(ParseParams { let parsed = deno_ast::parse_module(ParseParams {
specifier: file_source.specifier.to_string(), specifier: file_source.specifier.to_string(),
text_info: SourceTextInfo::from_string(code.to_string()), text_info: SourceTextInfo::from_string(code),
media_type, media_type,
capture_tokens: false, capture_tokens: false,
scope_analysis: false, scope_analysis: false,
@ -281,6 +281,7 @@ mod not_docs {
#[cfg(not(feature = "snapshot_from_snapshot"))] #[cfg(not(feature = "snapshot_from_snapshot"))]
{ {
use deno_core::ExtensionFileSourceCode;
maybe_additional_extension = Some( maybe_additional_extension = Some(
Extension::builder("runtime_main") Extension::builder("runtime_main")
.dependencies(vec!["runtime"]) .dependencies(vec!["runtime"])
@ -314,9 +315,13 @@ fn main() {
// doesn't actually compile on docs.rs // doesn't actually compile on docs.rs
if env::var_os("DOCS_RS").is_some() { if env::var_os("DOCS_RS").is_some() {
let snapshot_slice = &[]; let snapshot_slice = &[];
#[allow(clippy::needless_borrow)]
std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap(); std::fs::write(&runtime_snapshot_path, snapshot_slice).unwrap();
} }
#[cfg(not(feature = "docsrs"))] #[cfg(all(
not_docs::build_snapshot(runtime_snapshot_path) not(feature = "docsrs"),
not(feature = "dont_create_runtime_snapshot")
))]
startup_snapshot::build_snapshot(runtime_snapshot_path)
} }

View file

@ -1,16 +1,21 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
use deno_core::Snapshot; use deno_core::Snapshot;
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
use log::debug; use log::debug;
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
static COMPRESSED_RUNTIME_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/RUNTIME_SNAPSHOT.bin"));
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
pub static RUNTIME_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new( pub static RUNTIME_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new(
#[allow(clippy::uninit_vec)] #[allow(clippy::uninit_vec)]
#[cold] #[cold]
#[inline(never)] #[inline(never)]
|| { || {
static COMPRESSED_RUNTIME_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/RUNTIME_SNAPSHOT.bin"));
let size = let size =
u32::from_le_bytes(COMPRESSED_RUNTIME_SNAPSHOT[0..4].try_into().unwrap()) u32::from_le_bytes(COMPRESSED_RUNTIME_SNAPSHOT[0..4].try_into().unwrap())
as usize; as usize;
@ -29,10 +34,15 @@ pub static RUNTIME_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new(
}, },
); );
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
pub fn deno_isolate_init() -> Snapshot { pub fn deno_isolate_init() -> Snapshot {
debug!("Deno isolate init with snapshots."); debug!("Deno isolate init with snapshots.");
Snapshot::Static(&RUNTIME_SNAPSHOT) Snapshot::Static(&RUNTIME_SNAPSHOT)
} }
#[cfg(feature = "snapshot_from_snapshot")] #[cfg(not(feature = "include_js_files_for_snapshotting"))]
pub static SOURCE_CODE_FOR_99_MAIN_JS: &str = include_str!("js/99_main.js"); pub static SOURCE_CODE_FOR_99_MAIN_JS: &str = include_str!("js/99_main.js");
#[cfg(feature = "include_js_files_for_snapshotting")]
pub static PATH_FOR_99_MAIN_JS: &str =
concat!(env!("CARGO_MANIFEST_DIR"), "/js/99_main.js");

View file

@ -1,7 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::colors; use crate::colors;
use crate::inspector_server::InspectorServer; use crate::inspector_server::InspectorServer;
use crate::js;
use crate::ops; use crate::ops;
use crate::ops::io::Stdio; use crate::ops::io::Stdio;
use crate::permissions::PermissionsContainer; use crate::permissions::PermissionsContainer;
@ -453,13 +452,17 @@ impl WebWorker {
// Append exts // Append exts
extensions.extend(std::mem::take(&mut options.extensions)); extensions.extend(std::mem::take(&mut options.extensions));
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
let startup_snapshot = options
.startup_snapshot
.unwrap_or_else(crate::js::deno_isolate_init);
#[cfg(feature = "dont_create_runtime_snapshot")]
let startup_snapshot = options.startup_snapshot
.expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature.");
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()), module_loader: Some(options.module_loader.clone()),
startup_snapshot: Some( startup_snapshot: Some(startup_snapshot),
options
.startup_snapshot
.unwrap_or_else(js::deno_isolate_init),
),
source_map_getter: options.source_map_getter, source_map_getter: options.source_map_getter,
get_error_class_fn: options.get_error_class_fn, get_error_class_fn: options.get_error_class_fn,
shared_array_buffer_store: options.shared_array_buffer_store.clone(), shared_array_buffer_store: options.shared_array_buffer_store.clone(),

View file

@ -35,7 +35,6 @@ use deno_web::BlobStore;
use log::debug; use log::debug;
use crate::inspector_server::InspectorServer; use crate::inspector_server::InspectorServer;
use crate::js;
use crate::ops; use crate::ops;
use crate::ops::io::Stdio; use crate::ops::io::Stdio;
use crate::permissions::PermissionsContainer; use crate::permissions::PermissionsContainer;
@ -282,13 +281,17 @@ impl MainWorker {
]; ];
extensions.extend(std::mem::take(&mut options.extensions)); extensions.extend(std::mem::take(&mut options.extensions));
#[cfg(not(feature = "dont_create_runtime_snapshot"))]
let startup_snapshot = options
.startup_snapshot
.unwrap_or_else(crate::js::deno_isolate_init);
#[cfg(feature = "dont_create_runtime_snapshot")]
let startup_snapshot = options.startup_snapshot
.expect("deno_runtime startup snapshot is not available with 'create_runtime_snapshot' Cargo feature.");
let mut js_runtime = JsRuntime::new(RuntimeOptions { let mut js_runtime = JsRuntime::new(RuntimeOptions {
module_loader: Some(options.module_loader.clone()), module_loader: Some(options.module_loader.clone()),
startup_snapshot: Some( startup_snapshot: Some(startup_snapshot),
options
.startup_snapshot
.unwrap_or_else(js::deno_isolate_init),
),
source_map_getter: options.source_map_getter, source_map_getter: options.source_map_getter,
get_error_class_fn: options.get_error_class_fn, get_error_class_fn: options.get_error_class_fn,
shared_array_buffer_store: options.shared_array_buffer_store.clone(), shared_array_buffer_store: options.shared_array_buffer_store.clone(),