Keep WashHash::generate function the same

This commit is contained in:
Mark McCaskey
2019-07-08 12:22:17 -07:00
parent f45d523012
commit 9a3fd82a4b
4 changed files with 24 additions and 8 deletions

View File

@ -51,6 +51,12 @@ impl Backend {
}
}
impl Default for Backend {
fn default() -> Self {
Backend::Cranelift
}
}
impl std::str::FromStr for Backend {
type Err = String;
fn from_str(s: &str) -> Result<Backend, String> {

View File

@ -37,12 +37,25 @@ impl From<io::Error> for Error {
pub struct WasmHash([u8; 32], [u8; 32]);
impl WasmHash {
/// Hash a wasm module.
/// Hash a wasm module for the default compiler backend.
///
/// See also: `WasmHash::generate_for_backend`.
///
/// # Note:
/// This does no verification that the supplied data
/// is, in fact, a wasm module.
pub fn generate(wasm: &[u8], backend: Backend) -> Self {
pub fn generate(wasm: &[u8]) -> Self {
WasmHash::generate_for_backend(wasm, Backend::default())
}
/// Hash a wasm module for a specific compiler backend.
/// This allows multiple cache entries containing the same compiled
/// module with different compiler backends.
///
/// # Note:
/// This function also does no verification that the supplied data
/// is a wasm module
pub fn generate_for_backend(wasm: &[u8], backend: Backend) -> Self {
let mut first_part = [0u8; 32];
let mut second_part = [0u8; 32];

View File

@ -6,7 +6,6 @@ use wasmer_runtime::{
cache::{Cache, FileSystemCache, WasmHash},
compile, validate,
};
use wasmer_runtime_core::backend::Backend;
static NGINX_WASM: &'static [u8] = include_bytes!("../../../examples/nginx/nginx.wasm");
@ -19,9 +18,7 @@ fn load_module(hash: WasmHash, cache: &impl Cache) {
}
fn hashing_benchmark(c: &mut Criterion) {
c.bench_function("nginx HASH", |b| {
b.iter(|| WasmHash::generate(NGINX_WASM, Backend::Cranelift))
});
c.bench_function("nginx HASH", |b| b.iter(|| WasmHash::generate(NGINX_WASM)));
}
fn validate_benchmark(c: &mut Criterion) {
@ -39,7 +36,7 @@ fn load_benchmark(c: &mut Criterion) {
FileSystemCache::new(tempdir.path()).expect("unable to create file system cache")
};
let module = compile(NGINX_WASM).unwrap();
let wasm_hash = WasmHash::generate(NGINX_WASM, Backend::Cranelift);
let wasm_hash = WasmHash::generate(NGINX_WASM);
cache
.store(wasm_hash, module)
.expect("unable to store into cache");

View File

@ -352,7 +352,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
// We generate a hash for the given binary, so we can use it as key
// for the Filesystem cache
let hash = WasmHash::generate(&wasm_binary, options.backend);
let hash = WasmHash::generate_for_backend(&wasm_binary, options.backend);
let wasmer_cache_dir = get_cache_dir();