diff --git a/src/lib.rs b/src/lib.rs index 23be1a2..90dc37c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ lines(): 501636842 lines 30599847362 bytes in 167.17s (174.57 MB/s) use std::cmp; use std::io; use std::io::ErrorKind; +use std::ptr; extern crate memchr; use memchr::{memchr, memrchr}; @@ -219,12 +220,29 @@ impl LineReader { let fragment_len = self.end_of_buffer - self.end_of_complete; if fragment_len > 0 { if fragment_len > self.end_of_complete { - // Overlaps - hopefully quite rare. - self.buf.drain(..self.end_of_complete); - self.buf.extend(vec![0; self.end_of_complete]); + unsafe { + ptr::copy( + (&mut self.buf[self.end_of_complete..self.end_of_buffer]).as_mut_ptr(), + self.buf.as_mut_ptr(), + fragment_len, + ); + } + + // Overlaps - hopefully quite rare. + + // self.buf.drain(..self.end_of_complete); + // self.buf.extend(vec![0_u8; self.end_of_complete]); } else { - let (start, rest) = self.buf.split_at_mut(self.end_of_complete); - start[0..fragment_len].copy_from_slice(&rest[0..fragment_len]); + unsafe { + ptr::copy_nonoverlapping( + (&mut self.buf[self.end_of_complete..self.end_of_buffer]).as_mut_ptr(), + self.buf.as_mut_ptr(), + fragment_len, + ); + } + + // let (start, rest) = self.buf.split_at_mut(self.end_of_complete); + // start[0..fragment_len].copy_from_slice(&rest[0..fragment_len]); } self.end_of_buffer = fragment_len; } else {