Skip to content

Commit a43aec0

Browse files
seem-lessWaseem Gmohammadfawazmitchmindtreeotrho
authored
Adding --time-phases to forc build (#2091)
* make items under [project] ordered alphabetically in forc.toml * issue #1893store/show bytecode hash * formatted * added cargo lock file * cargo toml dependencies in alphabetical order * hash bin of script or predicate only * format * generating bytecode hash only on scripts and predicates * removed option from Compiled::tree_type * ran clippy * added to forc_build documentation * made filename suffix containing bin hash a constant * get root of predicate bytecode * Apply suggestions from code review Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com> * if let to match on program type * Update forc/src/cli/commands/build.rs updating bin-root filename Co-authored-by: mitchmindtree <mail@mitchellnordine.com> * added benchmarks for compilation process * use macro instead of closure for wrapping parts of compilation process Co-authored-by: Waseem G <contact@waseem-g.com> Co-authored-by: Mohammad Fawaz <mohammadfawaz89@gmail.com> Co-authored-by: mitchmindtree <mail@mitchellnordine.com> Co-authored-by: Toby Hutton <toby@grusly.com>
1 parent c394bf1 commit a43aec0

File tree

8 files changed

+53
-5
lines changed

8 files changed

+53
-5
lines changed

forc-pkg/src/manifest.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub struct BuildProfile {
8181
pub print_finalized_asm: bool,
8282
pub print_intermediate_asm: bool,
8383
pub silent: bool,
84+
pub time_phases: bool,
8485
}
8586

8687
impl Dependency {
@@ -347,6 +348,7 @@ impl BuildProfile {
347348
print_finalized_asm: false,
348349
print_intermediate_asm: false,
349350
silent: false,
351+
time_phases: false,
350352
}
351353
}
352354

@@ -356,6 +358,7 @@ impl BuildProfile {
356358
print_finalized_asm: false,
357359
print_intermediate_asm: false,
358360
silent: false,
361+
time_phases: false,
359362
}
360363
}
361364
}

forc-pkg/src/pkg.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,12 +1409,36 @@ pub fn compile(
14091409
namespace: namespace::Module,
14101410
source_map: &mut SourceMap,
14111411
) -> Result<(Compiled, Option<namespace::Root>)> {
1412+
// Time the given expression and print the result if `build_config.time_phases` is true.
1413+
macro_rules! time_expr {
1414+
($description:expr, $expression:expr) => {{
1415+
if build_profile.time_phases {
1416+
let expr_start = std::time::Instant::now();
1417+
let output = { $expression };
1418+
println!(
1419+
" Time elapsed to {}: {:?}",
1420+
$description,
1421+
expr_start.elapsed()
1422+
);
1423+
output
1424+
} else {
1425+
$expression
1426+
}
1427+
}};
1428+
}
1429+
14121430
let entry_path = manifest.entry_path();
1413-
let sway_build_config = sway_build_config(manifest.dir(), &entry_path, build_profile)?;
1431+
let sway_build_config = time_expr!(
1432+
"produce `sway_core::BuildConfig`",
1433+
sway_build_config(manifest.dir(), &entry_path, build_profile)?
1434+
);
14141435
let silent_mode = build_profile.silent;
14151436

14161437
// First, compile to an AST. We'll update the namespace and check for JSON ABI output.
1417-
let ast_res = compile_ast(manifest, build_profile, namespace)?;
1438+
let ast_res = time_expr!(
1439+
"compile to ast",
1440+
compile_ast(manifest, build_profile, namespace)?
1441+
);
14181442
match &ast_res {
14191443
CompileAstResult::Failure { warnings, errors } => {
14201444
print_on_failure(silent_mode, warnings, errors);
@@ -1424,7 +1448,7 @@ pub fn compile(
14241448
typed_program,
14251449
warnings,
14261450
} => {
1427-
let json_abi = typed_program.kind.generate_json_abi();
1451+
let json_abi = time_expr!("generate JSON ABI", typed_program.kind.generate_json_abi());
14281452
let tree_type = typed_program.kind.tree_type();
14291453
match tree_type {
14301454
// If we're compiling a library, we don't need to compile any further.
@@ -1443,8 +1467,14 @@ pub fn compile(
14431467

14441468
// For all other program types, we'll compile the bytecode.
14451469
TreeType::Contract | TreeType::Predicate | TreeType::Script => {
1446-
let asm_res = sway_core::ast_to_asm(ast_res, &sway_build_config);
1447-
let bc_res = sway_core::asm_to_bytecode(asm_res, source_map);
1470+
let asm_res = time_expr!(
1471+
"compile ast to asm",
1472+
sway_core::ast_to_asm(ast_res, &sway_build_config)
1473+
);
1474+
let bc_res = time_expr!(
1475+
"compile asm to bytecode",
1476+
sway_core::asm_to_bytecode(asm_res, source_map)
1477+
);
14481478
match bc_res {
14491479
BytecodeCompilationResult::Success { bytes, warnings } => {
14501480
print_on_success(silent_mode, &pkg.name, &warnings, &tree_type);

forc/src/cli/commands/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub struct Command {
7070
/// If --build-profile is also provided, forc omits this flag and uses provided build-profile.
7171
#[clap(long)]
7272
pub release: bool,
73+
/// Output the time elapsed over each part of the compilation process.
74+
#[clap(long)]
75+
pub time_phases: bool,
7376
}
7477

7578
pub(crate) fn exec(command: Command) -> Result<()> {

forc/src/cli/commands/deploy.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ pub struct Command {
6262
/// If --build-profile is also provided, forc omits this flag and uses provided build-profile.
6363
#[clap(long)]
6464
pub release: bool,
65+
/// Output the time elapsed over each part of the compilation process.
66+
#[clap(long)]
67+
pub time_phases: bool,
6568
}
6669

6770
pub(crate) async fn exec(command: Command) -> Result<()> {

forc/src/cli/commands/run.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ pub struct Command {
6060
#[clap(long = "silent", short = 's')]
6161
pub silent_mode: bool,
6262

63+
/// Output the time elapsed over each part of the compilation process.
64+
#[clap(long)]
65+
pub time_phases: bool,
66+
6367
/// Pretty-print the outputs from the node.
6468
#[clap(long = "pretty-print", short = 'r')]
6569
pub pretty_print: bool,

forc/src/ops/forc_build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
2828
locked,
2929
build_profile,
3030
release,
31+
time_phases,
3132
} = command;
3233

3334
let key_debug: String = "debug".to_string();
@@ -74,6 +75,7 @@ pub fn build(command: BuildCommand) -> Result<pkg::Compiled> {
7475
profile.print_finalized_asm |= print_finalized_asm;
7576
profile.print_intermediate_asm |= print_intermediate_asm;
7677
profile.silent |= silent_mode;
78+
profile.time_phases |= time_phases;
7779

7880
// Build it!
7981
let (compiled, source_map) = pkg::build(&plan, &profile, SWAY_GIT_TAG)?;

forc/src/ops/forc_deploy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
3737
url,
3838
build_profile,
3939
release,
40+
time_phases,
4041
} = command;
4142

4243
let build_command = BuildCommand {
@@ -53,6 +54,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
5354
locked,
5455
build_profile,
5556
release,
57+
time_phases,
5658
};
5759

5860
let compiled = forc_build::build(build_command)?;

forc/src/ops/forc_run.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
4040
locked: command.locked,
4141
build_profile: None,
4242
release: false,
43+
time_phases: command.time_phases,
4344
};
4445

4546
let compiled = forc_build::build(build_command)?;

0 commit comments

Comments
 (0)