Skip to content

Commit

Permalink
Fixed performance issue with contiguous (#1008) (#1009)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Luca Cominardi <[email protected]>
  • Loading branch information
kydos and Mallets authored May 4, 2024
1 parent f5195c0 commit e53364f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions commons/zenoh-buffers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,22 @@ pub mod buffer {
let mut slices = self.slices();
match slices.len() {
0 => Cow::Borrowed(b""),
1 => Cow::Borrowed(slices.next().unwrap()),
_ => Cow::Owned(slices.fold(Vec::new(), |mut acc, it| {
acc.extend(it);
acc
})),
1 => {
// SAFETY: unwrap here is safe because we have explicitly checked
// the iterator has 1 element.
Cow::Borrowed(unsafe { slices.next().unwrap_unchecked() })
}
_ => {
let mut l = 0;
for s in slices.by_ref() {
l += s.len();
}
let mut vec = Vec::with_capacity(l);
for slice in slices {
vec.extend_from_slice(slice);
}
Cow::Owned(vec)
}
}
}
}
Expand Down

0 comments on commit e53364f

Please sign in to comment.