diff options
| -rw-r--r-- | benches/concat.rs | 2 | ||||
| -rw-r--r-- | src/rope.rs | 24 |
2 files changed, 11 insertions, 15 deletions
diff --git a/benches/concat.rs b/benches/concat.rs index a07f908..0b31ebb 100644 --- a/benches/concat.rs +++ b/benches/concat.rs @@ -11,7 +11,7 @@ fn concat(buf: &[u8]) -> Rope { } fn criterion_benchmark(c: &mut Criterion) { - let bytes = vec![0; 10000]; + let bytes = vec![0; 1000]; c.bench_function("concat asdfasdf", |b| b.iter(|| concat(black_box(&bytes)))); } diff --git a/src/rope.rs b/src/rope.rs index 62c6a89..56f53d0 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -105,17 +105,15 @@ impl Rope { fn concat_height(&self, self_height: usize, other: &Rope, other_height: usize) -> Insert { if let (Rope(Node::Leaf(l)), Rope(Node::Leaf(r))) = (self, other) { if self.len() + other.len() <= MAX_NODE_SIZE { - let mut buf: Rc<[u8]> = Rc::from(vec![0; self.len() + other.len()]); - let buf_m = Rc::get_mut(&mut buf) - .expect("r was just created, so there can't be any other references"); - buf_m[..self.len()].copy_from_slice(l.bytes()); - buf_m[self.len()..].copy_from_slice(r.bytes()); - return Insert::Node(Rope::leaf(buf)); + let mut buf = Vec::with_capacity(self.len() + other.len()); + buf.extend_from_slice(l.bytes()); + buf.extend_from_slice(r.bytes()); + return Insert::Node(Rope::leaf(Rc::from(buf))); } if self.len() < MAX_NODE_SIZE / 2 || other.len() < MAX_NODE_SIZE / 2 { - let mut buf = vec![0; self.len() + other.len()]; - buf[..self.len()].copy_from_slice(l.bytes()); - buf[self.len()..].copy_from_slice(r.bytes()); + let mut buf = Vec::with_capacity(self.len() + other.len()); + buf.extend_from_slice(l.bytes()); + buf.extend_from_slice(r.bytes()); return Insert::Split( Rope::leaf(Rc::from(&buf[..buf.len() / 2])), Rope::leaf(Rc::from(&buf[buf.len() / 2..])), @@ -214,16 +212,14 @@ impl Rope { } pub fn read(r: &mut dyn Read) -> Result<Rope, std::io::Error> { + let mut buf = vec![0; MAX_NODE_SIZE]; let mut rope = Rope::empty(); loop { - let mut buf = Rc::new([0; MAX_NODE_SIZE]); - let buf_m = Rc::get_mut(&mut buf) - .expect("buf was just created, so there should be no other references"); - let n = r.read(buf_m)?; + let n = r.read(&mut buf)?; if n == 0 { break; } - rope = rope.concat(&Rope::from_slice(buf, 0, n)); + rope = rope.concat(&Rope::leaf(Rc::from(&buf[..n]))); } Ok(rope) } |
