From b7a783d4038aa9d8705da9fd07da3653e87c3713 Mon Sep 17 00:00:00 2001 From: Jaby Date: Wed, 28 Sep 2022 21:21:15 +0200 Subject: [PATCH] Use buffered read and write for better performance --- src/Tools/cpp_out/src/lib.rs | 24 ++++++++++++------------ src/Tools/tool_helper/src/lib.rs | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Tools/cpp_out/src/lib.rs b/src/Tools/cpp_out/src/lib.rs index 7db1f67f..b7a2427a 100644 --- a/src/Tools/cpp_out/src/lib.rs +++ b/src/Tools/cpp_out/src/lib.rs @@ -32,21 +32,21 @@ const WINDOWS_LINEFEED: &'static str = "\r\n"; const C_DECLARATIONS: FileDeclarations = FileDeclarations{include: "", var_type: "char"}; const CPP_DECLARATIONS: FileDeclarations = FileDeclarations{include: "#include ", var_type: "uint8_t"}; -fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<(), std::io::Error> { - let mut byte_line_count = 1; - let mut bytes = input.bytes(); +fn output_bytes(input: Input, output: &mut Output, line_feed: &str) -> Result<(), std::io::Error> { + let mut byte_line_count = 0; - if let Some(byte) = bytes.next() { - write!(output, "\t{:#04X}", byte?)?; + write!(output, "\t")?; + for byte in input.bytes() { + if byte_line_count > 0 { + write!(output, ", ")?; + } - for byte in bytes { - write!(output, ", {:#04X}", byte?)?; - byte_line_count += 1; + write!(output, "{:#04X}", byte?)?; + byte_line_count += 1; - if byte_line_count >= 16 { - write!(output, "{}\t", line_feed)?; - byte_line_count = 0; - } + if byte_line_count >= 16 { + write!(output, ",{}\t", line_feed)?; + byte_line_count = 0; } } diff --git a/src/Tools/tool_helper/src/lib.rs b/src/Tools/tool_helper/src/lib.rs index 7ad5ad19..f77cd044 100644 --- a/src/Tools/tool_helper/src/lib.rs +++ b/src/Tools/tool_helper/src/lib.rs @@ -76,15 +76,15 @@ impl std::convert::From for Error { pub fn open_output(output_file: Option) -> Result { match output_file { - Some(output_path) => Ok(Box::new(std::fs::File::create(output_path)?)), - None => Ok(Box::new(std::io::stdout())), + Some(output_path) => Ok(Box::new(std::io::BufWriter::new(std::fs::File::create(output_path)?))), + None => Ok(Box::new(std::io::BufWriter::new(std::io::stdout()))), } } pub fn open_input(input_file: Option) -> Result { match input_file { - Some(input_path) => Ok(Box::new(std::fs::File::open(input_path)?)), - None => Ok(Box::new(std::io::stdin())), + Some(input_path) => Ok(Box::new(std::io::BufReader::new(std::fs::File::open(input_path)?))), + None => Ok(Box::new(std::io::BufReader::new(std::io::stdin()))), } }