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 workspace packages in parallel #2612

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
81 changes: 49 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,54 @@ 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();
#[allow(clippy::type_complexity)]
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