From f910e013a6aa65d3138fae3a585cdaab322e1fae Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Fri, 12 Jun 2020 21:07:18 +0200 Subject: [PATCH] use take() for extra fast processing of binary files Suggested by @notoria in #589. Many thanks! --- src/machine/system_calls.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/machine/system_calls.rs b/src/machine/system_calls.rs index 28339acd..e18da09b 100644 --- a/src/machine/system_calls.rs +++ b/src/machine/system_calls.rs @@ -2277,18 +2277,11 @@ impl MachineState { let mut string = String::new(); if stream.options.stream_type == StreamType::Binary { - let mut mstream = stream.clone(); - for _ in 0..num { - let mut b = [0u8; 1]; - - match mstream.read(&mut b) { - Ok(1) => { - string.push(b[0] as char); - } - _ => { - break; - } - } + let mut buf = vec![]; + let mut chunk = stream.take(num as u64); + chunk.read_to_end(&mut buf).ok(); + for c in buf { + string.push(c as char); } } else { let mut iter = self.open_parsing_stream(stream.clone(), -- 2.54.0