Skip to content

Commit

Permalink
Test unsafe copying.
Browse files Browse the repository at this point in the history
Use ptr::copy and ptr::copy_nonoverlapping instead of drain/extend and
split_at_mut/copy_from slice.

I can find no appreciable difference, but the code might as well live on
in history.
  • Loading branch information
Freaky committed Apr 23, 2018
1 parent f5ed673 commit 5ccea2c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -219,12 +220,29 @@ impl<R: io::Read> LineReader<R> {
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 {
Expand Down

0 comments on commit 5ccea2c

Please sign in to comment.