Skip to content

Commit e53364f

Browse files
kydosMallets
andauthored
Fixed performance issue with contiguous (#1008) (#1009)
* Fixed performance issue with contiguous (#1008) * fixed format issue slipped in when fixing (#1008) * Update commons/zenoh-buffers/src/lib.rs Co-authored-by: Luca Cominardi <luca.cominardi@gmail.com> --------- Co-authored-by: Luca Cominardi <luca.cominardi@gmail.com>
1 parent f5195c0 commit e53364f

File tree

1 file changed

+16
-5
lines changed
  • commons/zenoh-buffers/src

1 file changed

+16
-5
lines changed

commons/zenoh-buffers/src/lib.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,22 @@ pub mod buffer {
101101
let mut slices = self.slices();
102102
match slices.len() {
103103
0 => Cow::Borrowed(b""),
104-
1 => Cow::Borrowed(slices.next().unwrap()),
105-
_ => Cow::Owned(slices.fold(Vec::new(), |mut acc, it| {
106-
acc.extend(it);
107-
acc
108-
})),
104+
1 => {
105+
// SAFETY: unwrap here is safe because we have explicitly checked
106+
// the iterator has 1 element.
107+
Cow::Borrowed(unsafe { slices.next().unwrap_unchecked() })
108+
}
109+
_ => {
110+
let mut l = 0;
111+
for s in slices.by_ref() {
112+
l += s.len();
113+
}
114+
let mut vec = Vec::with_capacity(l);
115+
for slice in slices {
116+
vec.extend_from_slice(slice);
117+
}
118+
Cow::Owned(vec)
119+
}
109120
}
110121
}
111122
}

0 commit comments

Comments
 (0)