From 8e4681e20fb60e974001d470244b5f6469994b24 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Fri, 5 Jun 2020 19:25:43 +0200 Subject: [PATCH 01/11] [wasmparser] try to fix benchmark tests #2 Not yet running but got way closer. It seems some test .wast files are plain broken. --- crates/wasmparser/benches/benchmark.rs | 51 ++++++++++++++++++++------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index d76bbaa633..aaa739aaeb 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -2,7 +2,9 @@ extern crate criterion; use criterion::Criterion; -use std::fs::{read_dir, File}; +use std::fs; +use std::fs::File; +use std::io; use std::io::Read; use std::path::Path; use std::path::PathBuf; @@ -36,10 +38,32 @@ where } fn read_file_data(path: &PathBuf) -> Vec { - let mut data = Vec::new(); - let mut f = File::open(path).ok().unwrap(); - f.read_to_end(&mut data).unwrap(); - data + wat::parse_file(path).expect("encountered error while parsing Wasm text format file") +} + +/// Visits all directory entries within the given directory path. +/// +/// - `pred` can be used to filter some directories, e.g. all directories named +/// `"proposals"`. +/// - `cb` is the callback that is being called for every file within the non +/// filtered and visited directories. +fn visit_dirs(dir: &Path, pred: &P, cb: &mut F) -> io::Result<()> +where + P: Fn(&fs::DirEntry) -> bool, + F: FnMut(&fs::DirEntry), +{ + if dir.is_dir() { + for entry in fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + if path.is_dir() && pred(&entry) { + visit_dirs(&path, pred, cb)?; + } else { + cb(&entry); + } + } + } + Ok(()) } fn collect_test_files

(path: P) -> Vec> @@ -47,13 +71,16 @@ where P: AsRef, { let mut file_contents: Vec> = vec![]; - for entry in read_dir(path).expect("cannot find the benchmark test files") { - let dir = entry.unwrap(); - if !dir.file_type().unwrap().is_file() { - continue; - } - file_contents.push(read_file_data(&dir.path())); - } + visit_dirs( + path.as_ref(), + &|dir_entry| dir_entry.file_name().to_str() != Some("proposals"), + &mut |dir_entry| { + if dir_entry.path().extension() == Some(std::ffi::OsStr::new("wast")) { + file_contents.push(read_file_data(&dir_entry.path())) + } + }, + ) + .expect("encountered error while reading test directory"); file_contents } From 8ad76bb0772c8cfcb79e8191994be2aa73827268 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Fri, 5 Jun 2020 19:35:42 +0200 Subject: [PATCH 02/11] simplify conditional for extension of file paths --- crates/wasmparser/benches/benchmark.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index aaa739aaeb..e29ad6fac7 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -75,7 +75,7 @@ where path.as_ref(), &|dir_entry| dir_entry.file_name().to_str() != Some("proposals"), &mut |dir_entry| { - if dir_entry.path().extension() == Some(std::ffi::OsStr::new("wast")) { + if dir_entry.path().extension().and_then(|ext| ext.to_str()) == Some("wast") { file_contents.push(read_file_data(&dir_entry.path())) } }, From 2542842ff8eb42e81aad5a25969beb787f20cb8d Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Fri, 5 Jun 2020 19:36:26 +0200 Subject: [PATCH 03/11] [wasmparser] remove unused imports in benchmarks --- crates/wasmparser/benches/benchmark.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index e29ad6fac7..c7d96cb121 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -3,9 +3,7 @@ extern crate criterion; use criterion::Criterion; use std::fs; -use std::fs::File; use std::io; -use std::io::Read; use std::path::Path; use std::path::PathBuf; use wasmparser::{ From e4baf9c7a39b6dd0bf985dc148f499d03a9060e8 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Fri, 5 Jun 2020 19:40:39 +0200 Subject: [PATCH 04/11] [wasmparser] move some code around --- crates/wasmparser/benches/benchmark.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index c7d96cb121..f37d2333ab 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -22,19 +22,6 @@ const VALIDATOR_CONFIG: Option = Some(ValidatingParserCo }, }); -fn read_all_wasm<'a, T>(mut d: T) -where - T: WasmDecoder<'a>, -{ - loop { - match *d.read() { - ParserState::Error(ref e) => panic!("unexpected error while reading Wasm: {}", e), - ParserState::EndWasm => return, - _ => (), - } - } -} - fn read_file_data(path: &PathBuf) -> Vec { wat::parse_file(path).expect("encountered error while parsing Wasm text format file") } @@ -82,6 +69,19 @@ where file_contents } +fn read_all_wasm<'a, T>(mut d: T) +where + T: WasmDecoder<'a>, +{ + loop { + match *d.read() { + ParserState::Error(ref e) => panic!("unexpected error while reading Wasm: {}", e), + ParserState::EndWasm => return, + _ => (), + } + } +} + fn it_works_benchmark(c: &mut Criterion) { let mut data = collect_test_files("../../tests"); c.bench_function("it works benchmark", move |b| { From c7afc96e73fe28757441fd89d51486f756c9a709 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Thu, 11 Jun 2020 13:14:28 +0200 Subject: [PATCH 05/11] make .wat and .wast parsing work, improve error reporting --- crates/wasmparser/benches/benchmark.rs | 92 +++++++++++++++++++++----- 1 file changed, 74 insertions(+), 18 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index f37d2333ab..4599554ea0 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -22,8 +22,49 @@ const VALIDATOR_CONFIG: Option = Some(ValidatingParserCo }, }); -fn read_file_data(path: &PathBuf) -> Vec { - wat::parse_file(path).expect("encountered error while parsing Wasm text format file") +/// A benchmark input. +pub struct BenchmarkInput { + /// The path to the benchmark file important for handling errors. + pub path: PathBuf, + /// The encoded Wasm module that is run by the benchmark. + pub wasm: Vec, +} + +impl BenchmarkInput { + /// Creates a new benchmark input. + pub fn new(test_path: PathBuf, encoded_wasm: Vec) -> Self { + Self { path: test_path, wasm: encoded_wasm } + } +} + +/// Read a `.wat` formatted benchmark test file as benchmark input. +fn read_wat_module(path: &PathBuf) -> BenchmarkInput { + let encoded_wasm = wat::parse_file(path).expect("encountered error while parsing `.wat` file into `.wasm`"); + BenchmarkInput::new(path.clone(), encoded_wasm) +} + +/// Read a `.wast` formatted benchmark test file as benchmark input. +/// +/// We simply pull out all the module directives of the `.wast` file and return them. +fn read_wast_module(path: &PathBuf) -> Vec { + let mut inputs = Vec::new(); + let mut wast_file = fs::File::open(path).ok().unwrap(); + let mut wast_file_contents = String::new(); + use io::Read as _; + wast_file.read_to_string(&mut wast_file_contents).unwrap(); + let parse_buffer = wast::parser::ParseBuffer::new(&wast_file_contents).unwrap(); + 'outer: while let Ok(directive) = wast::parser::parse::(&parse_buffer) { + match directive { + wast::WastDirective::Module(mut module) => { + let encoded_wasm = module.encode().expect("encountered error while encoding the Wast module into Wasm"); + inputs.push(BenchmarkInput::new(path.clone(), encoded_wasm)); + } + _ => { + continue 'outer + } + } + } + inputs } /// Visits all directory entries within the given directory path. @@ -51,17 +92,28 @@ where Ok(()) } -fn collect_test_files

(path: P) -> Vec> +/// Returns a vector of all found benchmark input files under the given directory. +/// +/// Benchmark input files can be `.wat` or `.wast` formatted files. +/// For `.wast` files we pull out all the module directives and run them in the benchmarks. +fn collect_test_files

(path: P) -> Vec where P: AsRef, { - let mut file_contents: Vec> = vec![]; + let mut file_contents: Vec = vec![]; visit_dirs( path.as_ref(), - &|dir_entry| dir_entry.file_name().to_str() != Some("proposals"), + &|_| true, // accept all benchmarks &mut |dir_entry| { - if dir_entry.path().extension().and_then(|ext| ext.to_str()) == Some("wast") { - file_contents.push(read_file_data(&dir_entry.path())) + let ext: Option = dir_entry.path().extension().and_then(|ext| ext.to_str().map(|str| str.to_string())); + match ext.as_ref().map(|string| string.as_str()) { + Some("wat") => file_contents.push(read_wat_module(&dir_entry.path())), + Some("wast") => { + for wasm_module in read_wast_module(&dir_entry.path()) { + file_contents.push(wasm_module) + } + } + _ => (), } }, ) @@ -69,13 +121,17 @@ where file_contents } -fn read_all_wasm<'a, T>(mut d: T) +/// Reads the input given the Wasm parser or validator. +/// +/// The `path` specifies which benchmark input file we are currently operating on +/// so that we can report better errors in case of failures. +fn read_all_wasm<'a, T>(path: &PathBuf, mut d: T) where T: WasmDecoder<'a>, { loop { match *d.read() { - ParserState::Error(ref e) => panic!("unexpected error while reading Wasm: {}", e), + ParserState::Error(ref e) => panic!("unexpected error while reading Wasm at {:?}: {}", path, e), ParserState::EndWasm => return, _ => (), } @@ -83,28 +139,28 @@ where } fn it_works_benchmark(c: &mut Criterion) { - let mut data = collect_test_files("../../tests"); + let mut inputs = collect_test_files("../../testsuite"); c.bench_function("it works benchmark", move |b| { - for d in &mut data { - b.iter(|| read_all_wasm(Parser::new(d.as_slice()))); + for input in &mut inputs { + b.iter(|| read_all_wasm(&input.path, Parser::new(input.wasm.as_slice()))); } }); } fn validator_not_fails_benchmark(c: &mut Criterion) { - let mut data = collect_test_files("../../tests"); + let mut inputs = collect_test_files("../../testsuite"); c.bench_function("validator no fails benchmark", move |b| { - for d in &mut data { - b.iter(|| read_all_wasm(ValidatingParser::new(d.as_slice(), VALIDATOR_CONFIG))); + for input in &mut inputs { + b.iter(|| read_all_wasm(&input.path, ValidatingParser::new(input.wasm.as_slice(), VALIDATOR_CONFIG))); } }); } fn validate_benchmark(c: &mut Criterion) { - let mut data = collect_test_files("../../tests"); + let mut inputs = collect_test_files("../../testsuite"); c.bench_function("validate benchmark", move |b| { - for d in &mut data { - b.iter(|| validate(&d, VALIDATOR_CONFIG)); + for input in &mut inputs { + b.iter(|| validate(input.wasm.as_slice(), VALIDATOR_CONFIG)); } }); } From 8d59ff9481a1f54e1d65ddc4df4b438c0f1305ad Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Thu, 11 Jun 2020 13:31:00 +0200 Subject: [PATCH 06/11] apply rustfmt --- crates/wasmparser/benches/benchmark.rs | 32 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index 4599554ea0..21c02449dc 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -33,13 +33,17 @@ pub struct BenchmarkInput { impl BenchmarkInput { /// Creates a new benchmark input. pub fn new(test_path: PathBuf, encoded_wasm: Vec) -> Self { - Self { path: test_path, wasm: encoded_wasm } + Self { + path: test_path, + wasm: encoded_wasm, + } } } /// Read a `.wat` formatted benchmark test file as benchmark input. fn read_wat_module(path: &PathBuf) -> BenchmarkInput { - let encoded_wasm = wat::parse_file(path).expect("encountered error while parsing `.wat` file into `.wasm`"); + let encoded_wasm = + wat::parse_file(path).expect("encountered error while parsing `.wat` file into `.wasm`"); BenchmarkInput::new(path.clone(), encoded_wasm) } @@ -56,12 +60,12 @@ fn read_wast_module(path: &PathBuf) -> Vec { 'outer: while let Ok(directive) = wast::parser::parse::(&parse_buffer) { match directive { wast::WastDirective::Module(mut module) => { - let encoded_wasm = module.encode().expect("encountered error while encoding the Wast module into Wasm"); + let encoded_wasm = module + .encode() + .expect("encountered error while encoding the Wast module into Wasm"); inputs.push(BenchmarkInput::new(path.clone(), encoded_wasm)); } - _ => { - continue 'outer - } + _ => continue 'outer, } } inputs @@ -105,7 +109,10 @@ where path.as_ref(), &|_| true, // accept all benchmarks &mut |dir_entry| { - let ext: Option = dir_entry.path().extension().and_then(|ext| ext.to_str().map(|str| str.to_string())); + let ext: Option = dir_entry + .path() + .extension() + .and_then(|ext| ext.to_str().map(|str| str.to_string())); match ext.as_ref().map(|string| string.as_str()) { Some("wat") => file_contents.push(read_wat_module(&dir_entry.path())), Some("wast") => { @@ -131,7 +138,9 @@ where { loop { match *d.read() { - ParserState::Error(ref e) => panic!("unexpected error while reading Wasm at {:?}: {}", path, e), + ParserState::Error(ref e) => { + panic!("unexpected error while reading Wasm at {:?}: {}", path, e) + } ParserState::EndWasm => return, _ => (), } @@ -151,7 +160,12 @@ fn validator_not_fails_benchmark(c: &mut Criterion) { let mut inputs = collect_test_files("../../testsuite"); c.bench_function("validator no fails benchmark", move |b| { for input in &mut inputs { - b.iter(|| read_all_wasm(&input.path, ValidatingParser::new(input.wasm.as_slice(), VALIDATOR_CONFIG))); + b.iter(|| { + read_all_wasm( + &input.path, + ValidatingParser::new(input.wasm.as_slice(), VALIDATOR_CONFIG), + ) + }); } }); } From 0cb6898b9559d1f87827be53c73dcd5966c1d29a Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Thu, 11 Jun 2020 18:05:15 +0200 Subject: [PATCH 07/11] no longer fail when a benchmark input cannot be parsed, instead skip --- crates/wasmparser/benches/benchmark.rs | 29 +++++++++++++++----------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index 21c02449dc..fd9580c67e 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -51,21 +51,26 @@ fn read_wat_module(path: &PathBuf) -> BenchmarkInput { /// /// We simply pull out all the module directives of the `.wast` file and return them. fn read_wast_module(path: &PathBuf) -> Vec { - let mut inputs = Vec::new(); - let mut wast_file = fs::File::open(path).ok().unwrap(); + let mut wast_file = fs::File::open(path) + .ok() + .expect("encountered error while reading `.wast` benchmark file"); let mut wast_file_contents = String::new(); use io::Read as _; - wast_file.read_to_string(&mut wast_file_contents).unwrap(); - let parse_buffer = wast::parser::ParseBuffer::new(&wast_file_contents).unwrap(); - 'outer: while let Ok(directive) = wast::parser::parse::(&parse_buffer) { - match directive { - wast::WastDirective::Module(mut module) => { - let encoded_wasm = module - .encode() - .expect("encountered error while encoding the Wast module into Wasm"); - inputs.push(BenchmarkInput::new(path.clone(), encoded_wasm)); + wast_file + .read_to_string(&mut wast_file_contents) + .expect("encountered error while reading `.wast` benchmark file to string"); + let mut inputs = Vec::new(); + if let Ok(parse_buffer) = wast::parser::ParseBuffer::new(&wast_file_contents) { + 'outer: while let Ok(directive) = wast::parser::parse::(&parse_buffer) { + match directive { + wast::WastDirective::Module(mut module) => { + let encoded_wasm = module + .encode() + .expect("encountered error while encoding the Wast module into Wasm"); + inputs.push(BenchmarkInput::new(path.clone(), encoded_wasm)); + } + _ => continue 'outer, } - _ => continue 'outer, } } inputs From 957cf1ae9ec4367cee6b2ebecccff7e58ff0ce42 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Thu, 11 Jun 2020 18:05:51 +0200 Subject: [PATCH 08/11] filter out all invalid benchmark inputs for each of the three benchmarks --- crates/wasmparser/benches/benchmark.rs | 40 ++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index fd9580c67e..ee3b03e54c 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -152,8 +152,27 @@ where } } +/// Returns the default benchmark inputs that are proper `wasmparser` benchmark +/// test inputs. +fn collect_benchmark_inputs() -> Vec { + let from_testsuite = collect_test_files("../../testsuite"); + let from_tests = collect_test_files("../../tests"); + from_testsuite.into_iter().chain(from_tests.into_iter()).collect::>() +} + fn it_works_benchmark(c: &mut Criterion) { - let mut inputs = collect_test_files("../../testsuite"); + let mut inputs = collect_benchmark_inputs(); + // Filter out all benchmark inputs that fail to parse via `wasmparser`. + inputs.retain(|input| { + let mut parser = Parser::new(input.wasm.as_slice()); + 'outer: loop { + match parser.read() { + ParserState::Error(_) => break 'outer false, + ParserState::EndWasm => break 'outer true, + _ => continue, + } + } + }); c.bench_function("it works benchmark", move |b| { for input in &mut inputs { b.iter(|| read_all_wasm(&input.path, Parser::new(input.wasm.as_slice()))); @@ -162,7 +181,18 @@ fn it_works_benchmark(c: &mut Criterion) { } fn validator_not_fails_benchmark(c: &mut Criterion) { - let mut inputs = collect_test_files("../../testsuite"); + let mut inputs = collect_benchmark_inputs(); + // Filter out all benchmark inputs that fail to validate via `wasmparser`. + inputs.retain(|input| { + let mut parser = ValidatingParser::new(input.wasm.as_slice(), VALIDATOR_CONFIG); + 'outer: loop { + match parser.read() { + ParserState::Error(_) => break 'outer false, + ParserState::EndWasm => break 'outer true, + _ => continue, + } + } + }); c.bench_function("validator no fails benchmark", move |b| { for input in &mut inputs { b.iter(|| { @@ -176,7 +206,11 @@ fn validator_not_fails_benchmark(c: &mut Criterion) { } fn validate_benchmark(c: &mut Criterion) { - let mut inputs = collect_test_files("../../testsuite"); + let mut inputs = collect_benchmark_inputs(); + // Filter out all benchmark inputs that fail to validate via `wasmparser`. + inputs.retain(|input| { + validate(input.wasm.as_slice(), VALIDATOR_CONFIG).is_ok() + }); c.bench_function("validate benchmark", move |b| { for input in &mut inputs { b.iter(|| validate(input.wasm.as_slice(), VALIDATOR_CONFIG)); From 347439e8e1369e5f83177484fea0967e383fb774 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Thu, 11 Jun 2020 18:06:03 +0200 Subject: [PATCH 09/11] minor panic message improvement --- crates/wasmparser/benches/benchmark.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index ee3b03e54c..29cba69201 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -144,7 +144,7 @@ where loop { match *d.read() { ParserState::Error(ref e) => { - panic!("unexpected error while reading Wasm at {:?}: {}", path, e) + panic!("unexpected error while reading Wasm from {:?}: {}", path, e) } ParserState::EndWasm => return, _ => (), From 993382ce0d1f0e155f8391c06abdb75f6d240c64 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Thu, 11 Jun 2020 18:11:06 +0200 Subject: [PATCH 10/11] apply rustfmt --- crates/wasmparser/benches/benchmark.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index 29cba69201..a6f578f633 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -61,7 +61,8 @@ fn read_wast_module(path: &PathBuf) -> Vec { .expect("encountered error while reading `.wast` benchmark file to string"); let mut inputs = Vec::new(); if let Ok(parse_buffer) = wast::parser::ParseBuffer::new(&wast_file_contents) { - 'outer: while let Ok(directive) = wast::parser::parse::(&parse_buffer) { + 'outer: while let Ok(directive) = wast::parser::parse::(&parse_buffer) + { match directive { wast::WastDirective::Module(mut module) => { let encoded_wasm = module @@ -157,7 +158,10 @@ where fn collect_benchmark_inputs() -> Vec { let from_testsuite = collect_test_files("../../testsuite"); let from_tests = collect_test_files("../../tests"); - from_testsuite.into_iter().chain(from_tests.into_iter()).collect::>() + from_testsuite + .into_iter() + .chain(from_tests.into_iter()) + .collect::>() } fn it_works_benchmark(c: &mut Criterion) { @@ -208,9 +212,7 @@ fn validator_not_fails_benchmark(c: &mut Criterion) { fn validate_benchmark(c: &mut Criterion) { let mut inputs = collect_benchmark_inputs(); // Filter out all benchmark inputs that fail to validate via `wasmparser`. - inputs.retain(|input| { - validate(input.wasm.as_slice(), VALIDATOR_CONFIG).is_ok() - }); + inputs.retain(|input| validate(input.wasm.as_slice(), VALIDATOR_CONFIG).is_ok()); c.bench_function("validate benchmark", move |b| { for input in &mut inputs { b.iter(|| validate(input.wasm.as_slice(), VALIDATOR_CONFIG)); From beff3e93e718fbdc8e372a8e244bc6f79c193e48 Mon Sep 17 00:00:00 2001 From: Robin Freyler Date: Fri, 12 Jun 2020 22:57:38 +0200 Subject: [PATCH 11/11] make benchmarks work again !!! --- crates/wasmparser/benches/benchmark.rs | 33 ++++++++++++++++---------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/crates/wasmparser/benches/benchmark.rs b/crates/wasmparser/benches/benchmark.rs index a6f578f633..d05c24ea82 100644 --- a/crates/wasmparser/benches/benchmark.rs +++ b/crates/wasmparser/benches/benchmark.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate criterion; -use criterion::Criterion; +use criterion::{black_box, Criterion}; use std::fs; use std::io; use std::path::Path; @@ -178,9 +178,14 @@ fn it_works_benchmark(c: &mut Criterion) { } }); c.bench_function("it works benchmark", move |b| { - for input in &mut inputs { - b.iter(|| read_all_wasm(&input.path, Parser::new(input.wasm.as_slice()))); - } + b.iter(|| { + for input in &mut inputs { + let _ = black_box(read_all_wasm( + &input.path, + Parser::new(input.wasm.as_slice()), + )); + } + }) }); } @@ -198,14 +203,14 @@ fn validator_not_fails_benchmark(c: &mut Criterion) { } }); c.bench_function("validator no fails benchmark", move |b| { - for input in &mut inputs { - b.iter(|| { - read_all_wasm( + b.iter(|| { + for input in &mut inputs { + let _ = black_box(read_all_wasm( &input.path, ValidatingParser::new(input.wasm.as_slice(), VALIDATOR_CONFIG), - ) - }); - } + )); + } + }); }); } @@ -214,9 +219,11 @@ fn validate_benchmark(c: &mut Criterion) { // Filter out all benchmark inputs that fail to validate via `wasmparser`. inputs.retain(|input| validate(input.wasm.as_slice(), VALIDATOR_CONFIG).is_ok()); c.bench_function("validate benchmark", move |b| { - for input in &mut inputs { - b.iter(|| validate(input.wasm.as_slice(), VALIDATOR_CONFIG)); - } + b.iter(|| { + for input in &mut inputs { + let _ = black_box(validate(input.wasm.as_slice(), VALIDATOR_CONFIG)); + } + }) }); }