Use std::fs read/write conveniences

In addition to being more ergonomic these are much more efficient at reading
large files as they preallocate internally. This provides a nice speed boost
locally, reducing the overhead of `wasm-bindgen-test-runner` from 0.23s to
0.19s, yay!
This commit is contained in:
Alex Crichton
2018-07-25 16:06:47 -07:00
parent f3942229fe
commit 0992e45e7f
4 changed files with 17 additions and 36 deletions

View File

@ -13,8 +13,7 @@ extern crate failure;
use std::any::Any; use std::any::Any;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fmt; use std::fmt;
use std::fs::File; use std::fs;
use std::io::{Read, Write};
use std::mem; use std::mem;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -158,9 +157,7 @@ impl Bindgen {
(module, &name[..]) (module, &name[..])
} }
Input::Path(ref path) => { Input::Path(ref path) => {
let mut contents = Vec::new(); let contents = fs::read(&path)
File::open(&path)
.and_then(|mut f| f.read_to_end(&mut contents))
.with_context(|_| format!("failed to read `{}`", path.display()))?; .with_context(|_| format!("failed to read `{}`", path.display()))?;
let module = parity_wasm::deserialize_buffer::<Module>(&contents) let module = parity_wasm::deserialize_buffer::<Module>(&contents)
.context("failed to parse input file as wasm")?; .context("failed to parse input file as wasm")?;
@ -232,14 +229,12 @@ impl Bindgen {
let extension = if self.nodejs_experimental_modules { "mjs" } else { "js" }; let extension = if self.nodejs_experimental_modules { "mjs" } else { "js" };
let js_path = out_dir.join(stem).with_extension(extension); let js_path = out_dir.join(stem).with_extension(extension);
File::create(&js_path) fs::write(&js_path, reset_indentation(&js))
.and_then(|mut f| f.write_all(reset_indentation(&js).as_bytes()))
.with_context(|_| format!("failed to write `{}`", js_path.display()))?; .with_context(|_| format!("failed to write `{}`", js_path.display()))?;
if self.typescript { if self.typescript {
let ts_path = out_dir.join(stem).with_extension("d.ts"); let ts_path = out_dir.join(stem).with_extension("d.ts");
File::create(&ts_path) fs::write(&ts_path, ts)
.and_then(|mut f| f.write_all(ts.as_bytes()))
.with_context(|_| format!("failed to write `{}`", ts_path.display()))?; .with_context(|_| format!("failed to write `{}`", ts_path.display()))?;
} }
@ -248,14 +243,12 @@ impl Bindgen {
if self.nodejs { if self.nodejs {
let js_path = wasm_path.with_extension(extension); let js_path = wasm_path.with_extension(extension);
let shim = self.generate_node_wasm_import(&module, &wasm_path); let shim = self.generate_node_wasm_import(&module, &wasm_path);
File::create(&js_path) fs::write(&js_path, shim)
.and_then(|mut f| f.write_all(shim.as_bytes()))
.with_context(|_| format!("failed to write `{}`", js_path.display()))?; .with_context(|_| format!("failed to write `{}`", js_path.display()))?;
} }
let wasm_bytes = parity_wasm::serialize(module)?; let wasm_bytes = parity_wasm::serialize(module)?;
File::create(&wasm_path) fs::write(&wasm_path, wasm_bytes)
.and_then(|mut f| f.write_all(&wasm_bytes))
.with_context(|_| format!("failed to write `{}`", wasm_path.display()))?; .with_context(|_| format!("failed to write `{}`", wasm_path.display()))?;
Ok(()) Ok(())
} }

View File

@ -2,8 +2,8 @@ extern crate base64;
extern crate tempfile; extern crate tempfile;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs::File; use std::fs;
use std::io::{self, Read, Write}; use std::io;
use std::process::Command; use std::process::Command;
use failure::{Error, ResultExt}; use failure::{Error, ResultExt};
@ -375,8 +375,7 @@ impl Output {
let td = tempfile::tempdir()?; let td = tempfile::tempdir()?;
let wasm = serialize(self.module)?; let wasm = serialize(self.module)?;
let wasm_file = td.as_ref().join("foo.wasm"); let wasm_file = td.as_ref().join("foo.wasm");
File::create(&wasm_file) fs::write(&wasm_file, wasm)
.and_then(|mut f| f.write_all(&wasm))
.with_context(|_| format!("failed to write wasm to `{}`", wasm_file.display()))?; .with_context(|_| format!("failed to write wasm to `{}`", wasm_file.display()))?;
let wast_file = td.as_ref().join("foo.wast"); let wast_file = td.as_ref().join("foo.wast");
@ -396,9 +395,7 @@ impl Output {
"wasm2asm", "wasm2asm",
)?; )?;
let mut asm_func = String::new(); let asm_func = fs::read_to_string(&js_file)
File::open(&js_file)
.and_then(|mut f| f.read_to_string(&mut asm_func))
.with_context(|_| format!("failed to read `{}`", js_file.display()))?; .with_context(|_| format!("failed to read `{}`", js_file.display()))?;
let mut make_imports = String::from( let mut make_imports = String::from(

View File

@ -4,8 +4,7 @@ extern crate wasm_bindgen_cli_support;
extern crate parity_wasm; extern crate parity_wasm;
use std::env; use std::env;
use std::fs::{self, File}; use std::fs;
use std::io::{Write, Read};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{self, Command}; use std::process::{self, Command};
@ -107,9 +106,7 @@ fn rmain() -> Result<(), Error> {
// Note that we're collecting *JS objects* that represent the functions to // Note that we're collecting *JS objects* that represent the functions to
// execute, and then those objects are passed into wasm for it to execute // execute, and then those objects are passed into wasm for it to execute
// when it sees fit. // when it sees fit.
let mut wasm = Vec::new(); let wasm = fs::read(&wasm_file_to_test)
File::open(&wasm_file_to_test)
.and_then(|mut f| f.read_to_end(&mut wasm))
.context("failed to read wasm file")?; .context("failed to read wasm file")?;
let wasm = Module::deserialize(&mut &wasm[..]) let wasm = Module::deserialize(&mut &wasm[..])
.context("failed to deserialize wasm module")?; .context("failed to deserialize wasm module")?;
@ -136,8 +133,7 @@ fn rmain() -> Result<(), Error> {
.context("executing `wasm-bindgen` over the wasm file")?; .context("executing `wasm-bindgen` over the wasm file")?;
let js_path = tmpdir.join("run.js"); let js_path = tmpdir.join("run.js");
File::create(&js_path) fs::write(&js_path, js_to_execute)
.and_then(|mut f| f.write_all(js_to_execute.as_bytes()))
.context("failed to write JS file")?; .context("failed to write JS file")?;
// Last but not least, execute `node`! Add an entry to `NODE_PATH` for the // Last but not least, execute `node`! Add an entry to `NODE_PATH` for the

View File

@ -4,8 +4,7 @@ extern crate docopt;
extern crate failure; extern crate failure;
extern crate wasm_bindgen_cli_support; extern crate wasm_bindgen_cli_support;
use std::fs::File; use std::fs;
use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process; use std::process;
@ -58,9 +57,7 @@ fn main() {
} }
fn rmain(args: &Args) -> Result<(), Error> { fn rmain(args: &Args) -> Result<(), Error> {
let mut wasm = Vec::new(); let wasm = fs::read(&args.arg_input)
File::open(&args.arg_input)
.and_then(|mut f| f.read_to_end(&mut wasm))
.with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?; .with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?;
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new() let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
@ -73,8 +70,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
if let Some(ref p) = args.flag_output { if let Some(ref p) = args.flag_output {
let dst = p.with_extension("d.ts"); let dst = p.with_extension("d.ts");
let ts = object.typescript(); let ts = object.typescript();
File::create(&dst) fs::write(&dst, ts)
.and_then(|mut f| f.write_all(ts.as_bytes()))
.with_context(|_| format!("failed to write `{}`", dst.display()))?; .with_context(|_| format!("failed to write `{}`", dst.display()))?;
} }
} }
@ -83,8 +79,7 @@ fn rmain(args: &Args) -> Result<(), Error> {
match args.flag_output { match args.flag_output {
Some(ref p) => { Some(ref p) => {
File::create(p) fs::write(p, js)
.and_then(|mut f| f.write_all(js.as_bytes()))
.with_context(|_| format!("failed to write `{}`", p.display()))?; .with_context(|_| format!("failed to write `{}`", p.display()))?;
} }
None => { None => {