summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorevan <github@evan.lol>2022-01-10 18:51:23 -0500
committerGitHub <noreply@github.com>2022-01-10 15:51:23 -0800
commitb66afa2518042cda239ef07f221722781ca660f6 (patch)
tree629ebc8c74496df7bf1588e2e4f5ec06fcb4e788 /runtime
parenta3b3a792b5c8dd2fa3e1b29f7fe5a7f3423f25c0 (diff)
feat(cli, runtime): compress snapshots (#13320)
Diffstat (limited to 'runtime')
-rw-r--r--runtime/Cargo.toml3
-rw-r--r--runtime/build.rs27
-rw-r--r--runtime/js.rs29
3 files changed, 54 insertions, 5 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml
index 67fc6a82d..8c88ce2e3 100644
--- a/runtime/Cargo.toml
+++ b/runtime/Cargo.toml
@@ -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"
@@ -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"
diff --git a/runtime/build.rs b/runtime/build.rs
index d1e8517b8..e2fe21b9e 100644
--- a/runtime/build.rs
+++ b/runtime/build.rs
@@ -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());
}
diff --git a/runtime/js.rs b/runtime/js.rs
index 58053ac02..c259ff5ee 100644
--- a/runtime/js.rs
+++ b/runtime/js.rs
@@ -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"));
+
+ 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)]