Skip to content

Commit

Permalink
Remove a copy being made of wasm modules (bytecodealliance#1877)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alexcrichton authored Oct 22, 2024
1 parent 6bd500d commit 9ac69f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/wat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,18 @@ impl Parser {
}),
})?;
match self.parse_bytes(Some(file), &contents) {
Ok(bytes) => Ok(bytes.into_owned()),
// If the result here is borrowed then that means that the input
// `&contents` was itself already a wasm module. We've already got
// an owned copy of that so return `contents` directly after
// double-checking it is indeed the same as the `bytes` return value
// here. That helps avoid a copy of `bytes` via something like
// `Cow::to_owned` which would otherwise copy the bytes.
Ok(Cow::Borrowed(bytes)) => {
assert_eq!(bytes.len(), contents.len());
assert_eq!(bytes.as_ptr(), contents.as_ptr());
Ok(contents)
}
Ok(Cow::Owned(bytes)) => Ok(bytes),
Err(mut e) => {
e.set_path(file);
Err(e)
Expand Down
2 changes: 2 additions & 0 deletions src/bin/wasm-tools/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ impl Opts {
}

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

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

0 comments on commit 9ac69f3

Please sign in to comment.