Skip to content

Commit 9ac69f3

Browse files
authored
Remove a copy being made of wasm modules (#1877)
* Remove a copy being made of wasm modules This commit removes a copy performed when the input file is WebAssembly when parsing input files on the CLI. This happened due to the implementation of `Cow::to_owned` but isn't necessary since we have the original owned copy around to return that instead. * Review comments
1 parent 6bd500d commit 9ac69f3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

crates/wat/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,18 @@ impl Parser {
235235
}),
236236
})?;
237237
match self.parse_bytes(Some(file), &contents) {
238-
Ok(bytes) => Ok(bytes.into_owned()),
238+
// If the result here is borrowed then that means that the input
239+
// `&contents` was itself already a wasm module. We've already got
240+
// an owned copy of that so return `contents` directly after
241+
// double-checking it is indeed the same as the `bytes` return value
242+
// here. That helps avoid a copy of `bytes` via something like
243+
// `Cow::to_owned` which would otherwise copy the bytes.
244+
Ok(Cow::Borrowed(bytes)) => {
245+
assert_eq!(bytes.len(), contents.len());
246+
assert_eq!(bytes.as_ptr(), contents.as_ptr());
247+
Ok(contents)
248+
}
249+
Ok(Cow::Owned(bytes)) => Ok(bytes),
239250
Err(mut e) => {
240251
e.set_path(file);
241252
Err(e)

src/bin/wasm-tools/validate.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl Opts {
6666
}
6767

6868
pub fn run(&self) -> Result<()> {
69+
let start = Instant::now();
6970
let wasm = self.io.parse_input_wasm()?;
71+
log::info!("read module in {:?}", start.elapsed());
7072

7173
// If validation fails then try to attach extra information to the
7274
// error based on DWARF information in the input wasm binary. If

0 commit comments

Comments
 (0)