From 9074d9b5813c43cb1d4920dbc9de7dfde032577a Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 26 Aug 2012 21:21:59 -0700 Subject: [PATCH] libcore: rewrite io::MemBuffer.write to use memcpy (#2004) --- src/libcore/io.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 69e95274f7c..9ba942cb0b7 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -663,24 +663,21 @@ type MemBuffer = @{buf: DVec, 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(); - // 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; + 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; + + buf } - self.buf.push_slice(v, vpos, vlen); - self.pos += vlen; } fn seek(offset: int, whence: SeekStyle) { let pos = self.pos;