| 1 |
use anyhow::Result; |
| 2 |
use criterion::{Criterion, criterion_group, criterion_main}; |
| 3 |
use once_cell::unsync::Lazy; |
| 4 |
use std::fs; |
| 5 |
use std::mem; |
| 6 |
use std::path::Path; |
| 7 |
use std::path::PathBuf; |
| 8 |
use wasmparser::VisitSimdOperator; |
| 9 |
use wasmparser::{ |
| 10 |
BinaryReader, DataKind, ElementKind, OperatorsReader, Parser, Payload, Validator, |
| 11 |
VisitOperator, WasmFeatures, |
| 12 |
}; |
| 13 |
|
| 14 |
/// A benchmark input. |
| 15 |
pub struct BenchmarkInput { |
| 16 |
/// The path to the benchmark file important for handling errors. |
| 17 |
pub path: PathBuf, |
| 18 |
/// The encoded Wasm module that is run by the benchmark. |
| 19 |
pub wasm: Vec<u8>, |
| 20 |
} |
| 21 |
|
| 22 |
impl BenchmarkInput { |
| 23 |
/// Creates a new benchmark input. |
| 24 |
pub fn new(test_path: PathBuf, encoded_wasm: Vec<u8>) -> Self { |
| 25 |
Self { |
| 26 |
path: test_path, |
| 27 |
wasm: encoded_wasm, |
| 28 |
} |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/// Returns a vector of all found benchmark input files under the given directory. |
| 33 |
/// |
| 34 |
/// Benchmark input files can be `.wat` or `.wast` formatted files. |
| 35 |
/// For `.wast` files we pull out all the module directives and run them in the benchmarks. |
| 36 |
fn collect_test_files(path: &Path, list: &mut Vec<BenchmarkInput>) -> Result<()> { |
| 37 |
for entry in path.read_dir()? { |
| 38 |
let entry = entry?; |
| 39 |
let path = entry.path(); |
| 40 |
if path.is_dir() { |
| 41 |
collect_test_files(&path, list)?; |
| 42 |
continue; |
| 43 |
} |
| 44 |
match path.extension().and_then(|ext| ext.to_str()) { |
| 45 |
Some("wasm") => { |
| 46 |
let wasm = fs::read(&path)?; |
| 47 |
list.push(BenchmarkInput::new(path, wasm)); |
| 48 |
} |
| 49 |
Some("wat") | Some("txt") => { |
| 50 |
if let Ok(wasm) = wat::parse_file(&path) { |
| 51 |
list.push(BenchmarkInput::new(path, wasm)); |
| 52 |
} |
| 53 |
} |
| 54 |
Some("wast") => { |
| 55 |
let contents = fs::read_to_string(&path)?; |
| 56 |
let buf = match wast::parser::ParseBuffer::new(&contents) { |
| 57 |
Ok(buf) => buf, |
| 58 |
Err(_) => continue, |
| 59 |
}; |
| 60 |
let wast: wast::Wast<'_> = match wast::parser::parse(&buf) { |
| 61 |
Ok(wast) => wast, |
| 62 |
Err(_) => continue, |
| 63 |
}; |
| 64 |
for directive in wast.directives { |
| 65 |
match directive { |
| 66 |
wast::WastDirective::Module(mut module) |
| 67 |
| wast::WastDirective::ModuleDefinition(mut module) => { |
| 68 |
let wasm = module.encode()?; |
| 69 |
list.push(BenchmarkInput::new(path.clone(), wasm)); |
| 70 |
} |
| 71 |
_ => continue, |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
_ => (), |
| 76 |
} |
| 77 |
} |
| 78 |
Ok(()) |
| 79 |
} |
| 80 |
|
| 81 |
/// Reads the input given the Wasm parser or validator. |
| 82 |
/// |
| 83 |
/// The `path` specifies which benchmark input file we are currently operating on |
| 84 |
/// so that we can report better errors in case of failures. |
| 85 |
fn read_all_wasm(wasm: &[u8]) -> Result<()> { |
| 86 |
use Payload::*; |
| 87 |
let mut allocs = wasmparser::OperatorsReaderAllocations::default(); |
| 88 |
let mut read_expr = |reader: BinaryReader<'_>| -> Result<_> { |
| 89 |
let mut ops = OperatorsReader::new_with_allocs(reader, mem::take(&mut allocs)); |
| 90 |
|
| 91 |
while !ops.eof() { |
| 92 |
ops.visit_operator(&mut NopVisit)?; |
| 93 |
} |
| 94 |
ops.finish()?; |
| 95 |
allocs = ops.into_allocations(); |
| 96 |
Ok(()) |
| 97 |
}; |
| 98 |
for item in Parser::new(0).parse_all(wasm) { |
| 99 |
match item? { |
| 100 |
TypeSection(s) => { |
| 101 |
for item in s { |
| 102 |
item?; |
| 103 |
} |
| 104 |
} |
| 105 |
ImportSection(s) => { |
| 106 |
for item in s { |
| 107 |
item?; |
| 108 |
} |
| 109 |
} |
| 110 |
FunctionSection(s) => { |
| 111 |
for item in s { |
| 112 |
item?; |
| 113 |
} |
| 114 |
} |
| 115 |
TableSection(s) => { |
| 116 |
for item in s { |
| 117 |
item?; |
| 118 |
} |
| 119 |
} |
| 120 |
MemorySection(s) => { |
| 121 |
for item in s { |
| 122 |
item?; |
| 123 |
} |
| 124 |
} |
| 125 |
TagSection(s) => { |
| 126 |
for item in s { |
| 127 |
item?; |
| 128 |
} |
| 129 |
} |
| 130 |
GlobalSection(s) => { |
| 131 |
for item in s { |
| 132 |
read_expr(item?.init_expr.get_binary_reader())?; |
| 133 |
} |
| 134 |
} |
| 135 |
ExportSection(s) => { |
| 136 |
for item in s { |
| 137 |
item?; |
| 138 |
} |
| 139 |
} |
| 140 |
ElementSection(s) => { |
| 141 |
for item in s { |
| 142 |
let item = item?; |
| 143 |
if let ElementKind::Active { offset_expr, .. } = item.kind { |
| 144 |
read_expr(offset_expr.get_binary_reader())?; |
| 145 |
} |
| 146 |
match item.items { |
| 147 |
wasmparser::ElementItems::Functions(r) => { |
| 148 |
for op in r { |
| 149 |
op?; |
| 150 |
} |
| 151 |
} |
| 152 |
wasmparser::ElementItems::Expressions(_, r) => { |
| 153 |
for op in r { |
| 154 |
op?; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
DataSection(s) => { |
| 161 |
for item in s { |
| 162 |
let item = item?; |
| 163 |
if let DataKind::Active { offset_expr, .. } = item.kind { |
| 164 |
read_expr(offset_expr.get_binary_reader())?; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
CodeSectionEntry(body) => { |
| 169 |
let mut locals = body.get_locals_reader()?.into_iter(); |
| 170 |
for item in locals.by_ref() { |
| 171 |
let _ = item?; |
| 172 |
} |
| 173 |
read_expr(locals.into_binary_reader_for_operators())?; |
| 174 |
} |
| 175 |
|
| 176 |
// Component sections |
| 177 |
ModuleSection { .. } => {} |
| 178 |
InstanceSection(s) => { |
| 179 |
for item in s { |
| 180 |
item?; |
| 181 |
} |
| 182 |
} |
| 183 |
CoreTypeSection(s) => { |
| 184 |
for item in s { |
| 185 |
item?; |
| 186 |
} |
| 187 |
} |
| 188 |
ComponentSection { .. } => {} |
| 189 |
ComponentInstanceSection(s) => { |
| 190 |
for item in s { |
| 191 |
item?; |
| 192 |
} |
| 193 |
} |
| 194 |
ComponentAliasSection(s) => { |
| 195 |
for item in s { |
| 196 |
item?; |
| 197 |
} |
| 198 |
} |
| 199 |
ComponentTypeSection(s) => { |
| 200 |
for item in s { |
| 201 |
item?; |
| 202 |
} |
| 203 |
} |
| 204 |
ComponentCanonicalSection(s) => { |
| 205 |
for item in s { |
| 206 |
item?; |
| 207 |
} |
| 208 |
} |
| 209 |
ComponentStartSection { .. } => {} |
| 210 |
ComponentImportSection(s) => { |
| 211 |
for item in s { |
| 212 |
item?; |
| 213 |
} |
| 214 |
} |
| 215 |
ComponentExportSection(s) => { |
| 216 |
for item in s { |
| 217 |
item?; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
Version { .. } |
| 222 |
| StartSection { .. } |
| 223 |
| DataCountSection { .. } |
| 224 |
| UnknownSection { .. } |
| 225 |
| CustomSection { .. } |
| 226 |
| CodeSectionStart { .. } |
| 227 |
| End(_) => {} |
| 228 |
|
| 229 |
other => { |
| 230 |
// NB: if you hit this panic if you'd be so kind as to grep |
| 231 |
// through other locations in the code base that need to be |
| 232 |
// updated as well. As of the time of this writing the locations |
| 233 |
// might be: |
| 234 |
// |
| 235 |
// * src/bin/wasm-tools/objdump.rs |
| 236 |
// * src/bin/wasm-tools/dump.rs |
| 237 |
// * crates/wasm-encoder/src/reencode.rs |
| 238 |
// * crates/wasm-encoder/src/reencode/component.rs |
| 239 |
// * crates/wasmprinter/src/lib.rs |
| 240 |
// * crates/wit-component/src/gc.rs |
| 241 |
// |
| 242 |
// This is required due to the `#[non_exhaustive]` nature of |
| 243 |
// the `Payload` enum. |
| 244 |
panic!("a new match statement should be added above for this case: {other:?}") |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
Ok(()) |
| 249 |
} |
| 250 |
|
| 251 |
/// Returns the default benchmark inputs that are proper `wasmparser` benchmark |
| 252 |
/// test inputs. |
| 253 |
fn collect_benchmark_inputs() -> Vec<BenchmarkInput> { |
| 254 |
let mut ret = Vec::new(); |
| 255 |
collect_test_files("../../tests".as_ref(), &mut ret).unwrap(); |
| 256 |
// Sort to ideally get more deterministic perf that ignores filesystems |
| 257 |
ret.sort_by_key(|p| p.path.clone()); |
| 258 |
ret |
| 259 |
} |
| 260 |
|
| 261 |
fn skip_validation(test: &Path) -> bool { |
| 262 |
let broken = [ |
| 263 |
"gc/gc-rec-sub.wat", |
| 264 |
"proposals/gc/type-equivalence.wast", |
| 265 |
"proposals/gc/type-subtyping.wast", |
| 266 |
]; |
| 267 |
|
| 268 |
let test_path = test.to_str().unwrap().replace("\\", "/"); // for windows paths |
| 269 |
if broken.iter().any(|x| test_path.contains(x)) { |
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
false |
| 274 |
} |
| 275 |
|
| 276 |
fn define_benchmarks(c: &mut Criterion) { |
| 277 |
let _ = env_logger::try_init(); |
| 278 |
|
| 279 |
fn validator() -> Validator { |
| 280 |
Validator::new_with_features(WasmFeatures::all()) |
| 281 |
} |
| 282 |
fn old_validator() -> Validator { |
| 283 |
Validator::new_with_features(WasmFeatures::WASM2) |
| 284 |
} |
| 285 |
|
| 286 |
let test_inputs = once_cell::unsync::Lazy::new(collect_benchmark_inputs); |
| 287 |
|
| 288 |
let parse_inputs = once_cell::unsync::Lazy::new(|| { |
| 289 |
let mut list = Vec::new(); |
| 290 |
for input in test_inputs.iter() { |
| 291 |
if read_all_wasm(&input.wasm).is_ok() { |
| 292 |
list.push(&input.wasm); |
| 293 |
} |
| 294 |
} |
| 295 |
list |
| 296 |
}); |
| 297 |
c.bench_function("parse/tests", |b| { |
| 298 |
Lazy::force(&parse_inputs); |
| 299 |
b.iter(|| { |
| 300 |
for wasm in parse_inputs.iter() { |
| 301 |
read_all_wasm(wasm).unwrap(); |
| 302 |
} |
| 303 |
}) |
| 304 |
}); |
| 305 |
|
| 306 |
let validate_inputs = once_cell::unsync::Lazy::new(|| { |
| 307 |
let mut list = Vec::new(); |
| 308 |
for input in test_inputs.iter() { |
| 309 |
if skip_validation(&input.path) { |
| 310 |
continue; |
| 311 |
} |
| 312 |
log::debug!("Validating {}", input.path.display()); |
| 313 |
if validator().validate_all(&input.wasm).is_ok() { |
| 314 |
list.push(&input.wasm); |
| 315 |
} |
| 316 |
} |
| 317 |
list |
| 318 |
}); |
| 319 |
c.bench_function("validate/tests", |b| { |
| 320 |
Lazy::force(&validate_inputs); |
| 321 |
b.iter(|| { |
| 322 |
for wasm in validate_inputs.iter() { |
| 323 |
validator().validate_all(wasm).unwrap(); |
| 324 |
} |
| 325 |
}) |
| 326 |
}); |
| 327 |
|
| 328 |
for file in std::fs::read_dir("benches").unwrap() { |
| 329 |
let file = file.unwrap(); |
| 330 |
let path = file.path(); |
| 331 |
if path.extension().and_then(|s| s.to_str()) != Some("wasm") { |
| 332 |
continue; |
| 333 |
} |
| 334 |
let name = path.file_stem().unwrap().to_str().unwrap(); |
| 335 |
let wasm = Lazy::new(|| std::fs::read(&path).unwrap()); |
| 336 |
c.bench_function(&format!("validate/{name}"), |b| { |
| 337 |
Lazy::force(&wasm); |
| 338 |
b.iter(|| { |
| 339 |
validator().validate_all(&wasm).unwrap(); |
| 340 |
}) |
| 341 |
}); |
| 342 |
if old_validator().validate_all(&wasm).is_ok() { |
| 343 |
c.bench_function(&format!("validate-old/{name}"), |b| { |
| 344 |
Lazy::force(&wasm); |
| 345 |
b.iter(|| { |
| 346 |
old_validator().validate_all(&wasm).unwrap(); |
| 347 |
}) |
| 348 |
}); |
| 349 |
} |
| 350 |
c.bench_function(&format!("parse/{name}"), |b| { |
| 351 |
Lazy::force(&wasm); |
| 352 |
b.iter(|| { |
| 353 |
read_all_wasm(&wasm).unwrap(); |
| 354 |
}) |
| 355 |
}); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
criterion_group!(benchmark, define_benchmarks); |
| 360 |
criterion_main!(benchmark); |
| 361 |
|
| 362 |
struct NopVisit; |
| 363 |
|
| 364 |
macro_rules! define_visit_operator { |
| 365 |
($(@$proposal:ident $op:ident $({ $($arg:ident: $argty:ty),* })? => $visit:ident ($($ann:tt)*))*) => { |
| 366 |
$( |
| 367 |
fn $visit(&mut self $($(,$arg: $argty)*)?) { |
| 368 |
define_visit_operator!(@visit $op $( $($arg)* )?); |
| 369 |
} |
| 370 |
)* |
| 371 |
}; |
| 372 |
|
| 373 |
(@visit BrTable $table:ident) => { |
| 374 |
for target in $table.targets() { |
| 375 |
target.unwrap(); |
| 376 |
} |
| 377 |
}; |
| 378 |
(@visit $($rest:tt)*) => {} |
| 379 |
} |
| 380 |
|
| 381 |
#[allow(unused_variables)] |
| 382 |
impl<'a> VisitOperator<'a> for NopVisit { |
| 383 |
type Output = (); |
| 384 |
|
| 385 |
fn simd_visitor(&mut self) -> Option<&mut dyn VisitSimdOperator<'a, Output = Self::Output>> { |
| 386 |
Some(self) |
| 387 |
} |
| 388 |
|
| 389 |
wasmparser::for_each_visit_operator!(define_visit_operator); |
| 390 |
} |
| 391 |
|
| 392 |
#[allow(unused_variables)] |
| 393 |
impl<'a> VisitSimdOperator<'a> for NopVisit { |
| 394 |
wasmparser::for_each_visit_simd_operator!(define_visit_operator); |
| 395 |
} |