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

feat(cli, runtime): compress snapshots #13320

Merged
merged 3 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ opt-level = 3
opt-level = 3
[profile.bench.package.tokio]
opt-level = 3
[profile.bench.package.zstd]
opt-level = 3
[profile.bench.package.lzzzz]
opt-level = 3
[profile.bench.package.zstd-sys]
opt-level = 3

# NB: the `bench` and `release` profiles must remain EXACTLY the same.
[profile.release.package.rand]
Expand Down Expand Up @@ -130,3 +136,9 @@ opt-level = 3
opt-level = 3
[profile.release.package.tokio]
opt-level = 3
[profile.release.package.zstd]
opt-level = 3
[profile.release.package.lzzzz]
opt-level = 3
[profile.release.package.zstd-sys]
opt-level = 3
2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ deno_websocket = { version = "0.36.0", path = "../ext/websocket" }
deno_webstorage = { version = "0.26.0", path = "../ext/webstorage" }
regex = "=1.5.4"
serde = { version = "=1.0.133", features = ["derive"] }
zstd = '=0.9.2'

[target.'cfg(windows)'.build-dependencies]
winapi = "=0.3.9"
Expand Down Expand Up @@ -85,6 +86,7 @@ text_lines = "=0.4.1"
tokio = { version = "=1.14", features = ["full"] }
uuid = { version = "=0.8.2", features = ["v4", "serde"] }
walkdir = "=2.3.2"
zstd = '=0.9.2'

[target.'cfg(windows)'.dependencies]
fwdansi = "=1.1.0"
Expand Down
25 changes: 24 additions & 1 deletion cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,30 @@ fn create_snapshot(
let snapshot = js_runtime.snapshot();
let snapshot_slice: &[u8] = &*snapshot;
println!("Snapshot size: {}", snapshot_slice.len());
std::fs::write(&snapshot_path, snapshot_slice).unwrap();

let compressed_snapshot_with_size = {
let mut vec = vec![];

vec.extend_from_slice(
&u32::try_from(snapshot.len())
.expect("snapshot larger than 4gb")
.to_le_bytes(),
);

vec.extend_from_slice(
&zstd::block::compress(snapshot_slice, 22)
.expect("snapshot compression failed"),
);

vec
};

println!(
"Snapshot compressed size: {}",
compressed_snapshot_with_size.len()
);

std::fs::write(&snapshot_path, compressed_snapshot_with_size).unwrap();
println!("Snapshot written to: {} ", snapshot_path.display());
}

Expand Down
20 changes: 17 additions & 3 deletions cli/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,25 @@ pub static SHARED_GLOBALS_LIB: &str =
pub static WINDOW_LIB: &str = include_str!("dts/lib.deno.window.d.ts");
pub static UNSTABLE_NS_LIB: &str = include_str!("dts/lib.deno.unstable.d.ts");

pub static COMPILER_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));
pub static COMPILER_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new(
#[cold]
#[inline(never)]
|| {
static COMPRESSED_COMPILER_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));

zstd::block::decompress(
&COMPRESSED_COMPILER_SNAPSHOT[4..],
u32::from_le_bytes(COMPRESSED_COMPILER_SNAPSHOT[0..4].try_into().unwrap())
as usize,
)
.unwrap()
.into_boxed_slice()
},
);

pub fn compiler_snapshot() -> Snapshot {
Snapshot::Static(COMPILER_SNAPSHOT)
Snapshot::Static(&*COMPILER_SNAPSHOT)
}

macro_rules! inc {
Expand Down
3 changes: 3 additions & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ deno_webidl = { version = "0.31.0", path = "../ext/webidl" }
deno_websocket = { version = "0.36.0", path = "../ext/websocket" }
deno_webstorage = { version = "0.26.0", path = "../ext/webstorage" }

lzzzz = '=0.8.0'

[target.'cfg(windows)'.build-dependencies]
winres = "0.1.11"
winapi = "0.3.9"
Expand Down Expand Up @@ -70,6 +72,7 @@ http = "0.2.4"
hyper = { version = "0.14.12", features = ["server", "stream", "http1", "http2", "runtime"] }
libc = "0.2.106"
log = "0.4.14"
lzzzz = '=0.8.0'
netif = "0.1.0"
notify = "=5.0.0-pre.12"
once_cell = "=1.9.0"
Expand Down
27 changes: 26 additions & 1 deletion runtime/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,32 @@ mod not_docs {
let snapshot = js_runtime.snapshot();
let snapshot_slice: &[u8] = &*snapshot;
println!("Snapshot size: {}", snapshot_slice.len());
std::fs::write(&snapshot_path, snapshot_slice).unwrap();

let compressed_snapshot_with_size = {
let mut vec = vec![];

vec.extend_from_slice(
&u32::try_from(snapshot.len())
.expect("snapshot larger than 4gb")
.to_le_bytes(),
);

lzzzz::lz4_hc::compress_to_vec(
snapshot_slice,
&mut vec,
lzzzz::lz4_hc::CLEVEL_MAX,
)
.expect("snapshot compression failed");

vec
};

println!(
"Snapshot compressed size: {}",
compressed_snapshot_with_size.len()
);

std::fs::write(&snapshot_path, compressed_snapshot_with_size).unwrap();
println!("Snapshot written to: {} ", snapshot_path.display());
}

Expand Down
29 changes: 25 additions & 4 deletions runtime/js.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use deno_core::Snapshot;
use log::debug;
use once_cell::sync::Lazy;

pub static CLI_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.bin"));
pub static CLI_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new(
#[cold]
#[inline(never)]
|| {
static COMPRESSED_CLI_SNAPSHOT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/CLI_SNAPSHOT.bin"));

bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
let size =
u32::from_le_bytes(COMPRESSED_CLI_SNAPSHOT[0..4].try_into().unwrap())
as usize;
let mut vec = Vec::with_capacity(size);

// SAFETY: vec is allocated with exact snapshot size (+ alignment)
// SAFETY: non zeroed bytes are overwritten with decompressed snapshot
unsafe {
vec.set_len(size);
}

lzzzz::lz4::decompress(&COMPRESSED_CLI_SNAPSHOT[4..], &mut vec).unwrap();

vec.into_boxed_slice()
},
);

pub fn deno_isolate_init() -> Snapshot {
debug!("Deno isolate init with snapshots.");
let data = CLI_SNAPSHOT;
Snapshot::Static(data)
Snapshot::Static(&*CLI_SNAPSHOT)
}

#[cfg(test)]
Expand Down