From 67a163184d5266e4263dcbf758fa6475730a182f Mon Sep 17 00:00:00 2001 From: polyphemus Date: Thu, 10 Jul 2014 20:58:11 +0200 Subject: [PATCH] Small performance enhancements Get rid of half filled heuristic and use unsafe array indexing because the indices should always be correct. --- cut/buffer.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/cut/buffer.rs b/cut/buffer.rs index 9fe691c3eea..91c464a5f2a 100644 --- a/cut/buffer.rs +++ b/cut/buffer.rs @@ -35,9 +35,9 @@ impl BufReader { } } + #[inline] fn read(&mut self) -> IoResult { - let buf_len = self.buffer.len(); - let buffer_fill = self.buffer.mut_slice(self.end, buf_len); + let buffer_fill = self.buffer.mut_slice_from(self.end); match self.reader.read(buffer_fill) { Ok(nread) => { @@ -53,9 +53,11 @@ impl BufReader { if self.end == self.start { self.start = 0; self.end = 0; - } - if self.end <= 2048 { self.read() } else { Ok(0) } + self.read() + } else { + Ok(0) + } } pub fn consume_line(&mut self) -> uint { @@ -73,7 +75,8 @@ impl BufReader { if buffer_used == 0 { return bytes_consumed; } for idx in range(self.start, self.end) { - if self.buffer[idx] == b'\n' { + // the indices are always correct, use unsafe for speed + if unsafe { *self.buffer.unsafe_ref(idx) } == b'\n' { self.start = idx + 1; return bytes_consumed + idx + 1; } @@ -108,7 +111,8 @@ impl Bytes::Select for BufReader { }; for idx in range(self.start, self.start + max_segment_len) { - if self.buffer[idx] == b'\n' { + // the indices are always correct, use unsafe for speed + if unsafe { *self.buffer.unsafe_ref(idx) } == b'\n' { let segment = self.buffer.slice(self.start, idx + 1); self.start = idx + 1;