Skip to content

Commit

Permalink
libcore: rewrite io::MemBuffer.write to use memcpy (rust-lang#2004)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickt committed Aug 31, 2012
1 parent 7bb6584 commit 9074d9b
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,24 +663,21 @@ type MemBuffer = @{buf: DVec<u8>, mut pos: uint};
impl MemBuffer: Writer {
fn write(v: &[const u8]) {
// Fast path.
let vlen = vec::len(v);
let buf_len = self.buf.len();
if self.pos == buf_len {
self.buf.push_all(v);
self.pos += vlen;
return;
}
do self.buf.swap |buf| {
let mut buf <- buf;
let v_len = v.len();
let buf_len = buf.len();
let count = uint::max(&buf_len, &(self.pos + v_len));
vec::reserve(buf, count);
unsafe { vec::unsafe::set_len(buf, count); }
vec::u8::memcpy(vec::mut_view(buf, self.pos, count), v, v_len);
self.pos += v_len;
// FIXME #2004--use memcpy here?
let mut pos = self.pos, vpos = 0u;
while vpos < vlen && pos < buf_len {
self.buf.set_elt(pos, copy v[vpos]);
pos += 1u;
vpos += 1u;
buf
}
self.buf.push_slice(v, vpos, vlen);
self.pos += vlen;
}
fn seek(offset: int, whence: SeekStyle) {
let pos = self.pos;
Expand Down

0 comments on commit 9074d9b

Please sign in to comment.