Skip to content

Commit

Permalink
feat(nargo): compile packages within a workspace in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Sep 12, 2023
1 parent 224bc48 commit fbe34c1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
1 change: 1 addition & 0 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 tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ toml.workspace = true
serde.workspace = true
serde_json.workspace = true
prettytable-rs = "0.10"
rayon = "1.7.0"
thiserror.workspace = true
tower.workspace = true
async-lsp = { version = "0.0.5", default-features = false, features = [
Expand Down
80 changes: 48 additions & 32 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::fs::program::{
save_contract_to_file, save_debug_artifact_to_file, save_program_to_file,
};
use super::NargoConfig;
use rayon::prelude::*;

// TODO(#1388): pull this from backend.
const BACKEND_IDENTIFIER: &str = "acvm-backend-barretenberg";
Expand Down Expand Up @@ -63,38 +64,53 @@ pub(crate) fn run(
let circuit_dir = workspace.target_directory_path();

let (np_language, is_opcode_supported) = backend.get_backend_info()?;
for package in &workspace {
// If `contract` package type, we're compiling every function in a 'contract' rather than just 'main'.
if package.is_contract() {
let (file_manager, compilation_result) = compile_contracts(
package,
&args.compile_options,
np_language,
&is_opcode_supported,
);
let contracts_with_debug_artifacts = report_errors(
compilation_result,
&file_manager,
args.compile_options.deny_warnings,
)?;

save_contracts(
contracts_with_debug_artifacts,
package,
&circuit_dir,
args.output_debug,
);
} else {
let (file_manager, compilation_result) =
compile_program(package, &args.compile_options, np_language, &is_opcode_supported);

let (program, debug_artifact) = report_errors(
compilation_result,
&file_manager,
args.compile_options.deny_warnings,
)?;
save_program(debug_artifact, program, package, &circuit_dir, args.output_debug);
}

let (binary_packages, contract_packages): (Vec<_>, Vec<_>) = workspace
.members
.iter()
.filter(|package| !package.is_library())
.partition(|package| package.is_binary());

// Compile all of the packages in parallel.
let program_results: Vec<(FileManager, CompilationResult<(CompiledProgram, DebugArtifact)>)> =
binary_packages
.par_iter()
.map(|package| {
compile_program(package, &args.compile_options, np_language, &is_opcode_supported)
})
.collect();
let contract_results: Vec<(
FileManager,
CompilationResult<Vec<(CompiledContract, DebugArtifact)>>,
)> = contract_packages
.par_iter()
.map(|package| {
compile_contracts(package, &args.compile_options, np_language, &is_opcode_supported)
})
.collect();

// Report any warnings/errors which were encountered during compilation.
let compiled_programs: Vec<(CompiledProgram, DebugArtifact)> = program_results
.into_iter()
.map(|(file_manager, compilation_result)| {
report_errors(compilation_result, &file_manager, args.compile_options.deny_warnings)
})
.collect::<Result<_, _>>()?;
let compiled_contracts: Vec<Vec<(CompiledContract, DebugArtifact)>> = contract_results
.into_iter()
.map(|(file_manager, compilation_result)| {
report_errors(compilation_result, &file_manager, args.compile_options.deny_warnings)
})
.collect::<Result<_, _>>()?;

// Save build artifacts to disk.
for (package, (program, debug_artifact)) in binary_packages.into_iter().zip(compiled_programs) {
save_program(debug_artifact, program, package, &circuit_dir, args.output_debug);
}
for (package, contracts_with_debug_artifacts) in
contract_packages.into_iter().zip(compiled_contracts)
{
save_contracts(contracts_with_debug_artifacts, package, &circuit_dir, args.output_debug);
}

Ok(())
Expand Down

0 comments on commit fbe34c1

Please sign in to comment.