Skip to content

Commit

Permalink
feat: Print the formatted bytecode size on forc build (#6214)
Browse files Browse the repository at this point in the history
## Description

Users can see the bytecode size for every compiled package using the
`--verbose` flag with `forc build`.

The PR makes it so they see the total bytecode size by default without
verbose mode in the `Finished` status message.

For workspaces, the bytecode size is the sum of all of the built
packages' bytecode.


![image](https://github.com/FuelLabs/sway/assets/47993817/5a09337f-95e8-4157-b9da-28371fb95a1e)

## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.
  • Loading branch information
sdankel authored Jul 4, 2024
1 parent e1b1c2b commit b7918e5
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 6 deletions.
196 changes: 192 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true
[dependencies]
ansi_term = "0.12"
anyhow = "1"
byte-unit = "5.1.4"
cid = "0.10"
forc-tracing = { version = "0.61.2", path = "../forc-tracing" }
forc-util = { version = "0.61.2", path = "../forc-util" }
Expand Down
21 changes: 19 additions & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
BuildProfile,
};
use anyhow::{anyhow, bail, Context, Error, Result};
use byte_unit::{Byte, UnitType};
use forc_tracing::{println_action_green, println_warning};
use forc_util::{
default_output_directory, find_file_name, kebab_to_snake_case, print_compiling,
Expand Down Expand Up @@ -497,7 +498,11 @@ impl BuiltPackage {
let json_abi_path = output_dir.join(program_abi_stem).with_extension("json");
self.write_json_abi(&json_abi_path, minify)?;

debug!(" Bytecode size: {} bytes", self.bytecode.bytes.len());
debug!(
" Bytecode size: {} bytes ({})",
self.bytecode.bytes.len(),
format_bytecode_size(self.bytecode.bytes.len())
);
// Additional ops required depending on the program type
match self.tree_type {
TreeType::Contract => {
Expand Down Expand Up @@ -2107,6 +2112,12 @@ fn profile_target_string(profile_name: &str, build_target: &BuildTarget) -> Stri
};
format!("{profile_name} [{}] target(s)", targets.join(" + "))
}
/// Returns the size of the bytecode in a human-readable format.
pub fn format_bytecode_size(bytes_len: usize) -> String {
let size = Byte::from_u64(bytes_len as u64);
let adjusted_byte = size.get_appropriate_unit(UnitType::Decimal);
adjusted_byte.to_string()
}

/// Check if the given node is a contract dependency of any node in the graph.
fn is_contract_dependency(graph: &Graph, node: NodeIx) -> bool {
Expand Down Expand Up @@ -2173,12 +2184,17 @@ pub fn build_with_options(build_options: &BuildOpts) -> Result<Built> {
},
)?;
let output_dir = pkg.output_directory.as_ref().map(PathBuf::from);
let total_size = built_packages
.iter()
.map(|(_, pkg)| pkg.bytecode.bytes.len())
.sum::<usize>();

println_action_green(
"Finished",
&format!(
"{} in {:.2}s",
"{} [{}] in {:.2}s",
profile_target_string(&build_profile.name, build_target),
format_bytecode_size(total_size),
build_start.elapsed().as_secs_f32()
),
);
Expand Down Expand Up @@ -2294,6 +2310,7 @@ pub fn build(

let mut lib_namespace_map = HashMap::default();
let mut compiled_contract_deps = HashMap::new();

for &node in plan
.compilation_order
.iter()
Expand Down

0 comments on commit b7918e5

Please sign in to comment.