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(compile): show remote modules and metadata size when compiling #27415

Merged
merged 10 commits into from
Jan 23, 2025
43 changes: 40 additions & 3 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2025 the Deno authors. MIT license.

use std::borrow::Cow;
use std::cell::Cell;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::env;
Expand Down Expand Up @@ -699,7 +700,7 @@ impl<'a> DenoCompileBinaryWriter<'a> {
vfs_case_sensitivity: vfs.case_sensitivity,
};

let data_section_bytes = serialize_binary_data_section(
let (data_section_bytes, section_sizes) = serialize_binary_data_section(
&metadata,
npm_snapshot.map(|s| s.into_serialized()),
&specifier_store.for_serialization(&root_dir_url),
Expand All @@ -709,6 +710,22 @@ impl<'a> DenoCompileBinaryWriter<'a> {
)
.context("Serializing binary data section.")?;

log::info!(
"\n{} {}",
crate::colors::bold("Files:"),
crate::util::display::human_size(section_sizes.vfs as f64)
);
log::info!(
"{} {}",
crate::colors::bold("Metadata:"),
crate::util::display::human_size(section_sizes.metadata as f64)
);
log::info!(
"{} {}\n",
crate::colors::bold("Remote modules:"),
crate::util::display::human_size(section_sizes.remote_modules as f64)
);

write_binary_bytes(writer, original_bin, data_section_bytes, compile_flags)
.context("Writing binary bytes")
}
Expand Down Expand Up @@ -912,6 +929,12 @@ fn write_binary_bytes(
Ok(())
}

struct BinaryDataSectionSizes {
metadata: usize,
remote_modules: usize,
vfs: usize,
}

/// Binary format:
/// * d3n0l4nd
/// * <metadata_len><metadata>
Expand All @@ -930,12 +953,16 @@ fn serialize_binary_data_section(
redirects: &SpecifierDataStore<SpecifierId>,
remote_modules: &SpecifierDataStore<RemoteModuleEntry<'_>>,
vfs: &BuiltVfs,
) -> Result<Vec<u8>, AnyError> {
) -> Result<(Vec<u8>, BinaryDataSectionSizes), AnyError> {
let metadata = serde_json::to_string(metadata)?;
let npm_snapshot =
npm_snapshot.map(serialize_npm_snapshot).unwrap_or_default();
let serialized_vfs = serde_json::to_string(&vfs.entries)?;

let remote_modules_len = Cell::new(0);
let metadata_len = Cell::new(0);
let vfs_len = Cell::new(0);

let bytes = capacity_builder::BytesBuilder::build(|builder| {
builder.append(MAGIC_BYTES);
// 1. Metadata
Expand All @@ -948,12 +975,14 @@ fn serialize_binary_data_section(
builder.append_le(npm_snapshot.len() as u64);
builder.append(&npm_snapshot);
}
metadata_len.set(builder.len());
// 3. Specifiers
builder.append(specifiers);
// 4. Redirects
redirects.serialize(builder);
// 5. Remote modules
remote_modules.serialize(builder);
remote_modules_len.set(builder.len() - metadata_len.get());
// 6. VFS
{
builder.append_le(serialized_vfs.len() as u64);
Expand All @@ -964,13 +993,21 @@ fn serialize_binary_data_section(
builder.append(file);
}
}
vfs_len.set(builder.len() - remote_modules_len.get());

// write the magic bytes at the end so we can use it
// to make sure we've deserialized correctly
builder.append(MAGIC_BYTES);
})?;

Ok(bytes)
Ok((
bytes,
BinaryDataSectionSizes {
metadata: metadata_len.get(),
remote_modules: remote_modules_len.get(),
vfs: vfs_len.get(),
},
))
}

fn serialize_npm_snapshot(
Expand Down
6 changes: 1 addition & 5 deletions cli/standalone/virtual_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ pub fn output_vfs(vfs: &BuiltVfs, executable_name: &str) {
let display_tree = vfs_as_display_tree(vfs, executable_name);
display_tree.print(&mut text).unwrap(); // unwrap ok because it's writing to a string
log::info!("\n{}\n", deno_terminal::colors::bold("Embedded Files"));
log::info!("{}\n", text.trim());
log::info!(
"Size: {}\n",
human_size(vfs.files.iter().map(|f| f.len() as f64).sum())
);
log::info!("{}", text.trim());
}

fn vfs_as_display_tree(
Expand Down
4 changes: 3 additions & 1 deletion tests/specs/compile/case_insensitive_building/compile.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ main[WILDLINE]
├── file.txt ([WILDLINE])
└── main.js ([WILDLINE])

Size: [WILDLINE]
Files: [WILDLINE]
Metadata: [WILDLINE]
Remote modules: [WILDLINE]

4 changes: 3 additions & 1 deletion tests/specs/compile/env_vars_support/compile.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ Embedded Files
out[WILDLINE]
└── main.ts ([WILDLINE])

Size: [WILDLINE]
Files: [WILDLINE]
Metadata: [WILDLINE]
Remote modules: [WILDLINE]

Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ main[WILDLINE]
│ └── yargs-parser/18.1.3/* ([WILDLINE])
└── main.ts ([WILDLINE])

Size: [WILDLINE]
Files: [WILDLINE]
Metadata: [WILDLINE]
Remote modules: [WILDLINE]

4 changes: 3 additions & 1 deletion tests/specs/compile/include/symlink_twice/compile.out
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ main[WILDLINE]
├── link.js --> index.js
└── setup.js ([WILDLINE])

Size: [WILDLINE]
Files: [WILDLINE]
Metadata: [WILDLINE]
Remote modules: [WILDLINE]

4 changes: 3 additions & 1 deletion tests/specs/compile/npm_fs/compile.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ main[WILDLINE]
├── main.ts ([WILDLINE])
└── node_modules/* ([WILDLINE])

Size: [WILDLINE]
Files: [WILDLINE]
Metadata: [WILDLINE]
Remote modules: [WILDLINE]

Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ bin[WILDLINE]
│ └── node_modules/* ([WILDLINE])
└── some_folder/* ([WILDLINE])

Size: [WILDLINE]
Files: [WILDLINE]
Metadata: [WILDLINE]
Remote modules: [WILDLINE]

Loading