Parallelize the compilation loop with Rayon (#58)

This commit is contained in:
Max McDonnell
2019-03-04 20:27:57 -05:00
committed by Dan Gohman
parent 04d4b20df4
commit 6eb09d9edd
2 changed files with 34 additions and 22 deletions

View File

@@ -19,6 +19,7 @@ cast = { version = "0.2.2", default-features = false }
failure = { version = "0.1.3", default-features = false }
failure_derive = { version = "0.1.3", default-features = false }
indexmap = "1.0.2"
rayon = "1.0"
[features]
default = ["std"]

View File

@@ -13,6 +13,7 @@ use cranelift_codegen::isa;
use cranelift_codegen::Context;
use cranelift_entity::PrimaryMap;
use cranelift_wasm::{DefinedFuncIndex, FuncIndex, FuncTranslator};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::vec::Vec;
/// Implementation of a relocation sink that just saves all the information for later
@@ -89,8 +90,13 @@ pub fn compile_module<'data, 'module>(
) -> Result<(Compilation, Relocations), CompileError> {
let mut functions = PrimaryMap::with_capacity(function_body_inputs.len());
let mut relocations = PrimaryMap::with_capacity(function_body_inputs.len());
for (i, input) in function_body_inputs.into_iter() {
let func_index = module.func_index(i);
function_body_inputs
.into_iter()
.collect::<Vec<(DefinedFuncIndex, &&'data [u8])>>()
.par_iter()
.map(|(i, input)| {
let func_index = module.func_index(*i);
let mut context = Context::new();
context.func.name = get_func_name(func_index);
context.func.signature = module.signatures[module.functions[func_index]].clone();
@@ -110,9 +116,14 @@ pub fn compile_module<'data, 'module>(
context
.compile_and_emit(isa, &mut code_buf, &mut reloc_sink, &mut trap_sink)
.map_err(CompileError::Codegen)?;
functions.push(code_buf);
relocations.push(reloc_sink.func_relocs);
}
Ok((code_buf, reloc_sink.func_relocs))
})
.collect::<Result<Vec<_>, CompileError>>()?
.into_iter()
.for_each(|(function, relocs)| {
functions.push(function);
relocations.push(relocs);
});
// TODO: Reorganize where we create the Vec for the resolved imports.
Ok((Compilation::new(functions), relocations))