Skip to content

Commit

Permalink
perf(types/buffer): skip copying in to_bytes when NonContiguous
Browse files Browse the repository at this point in the history
contains a single `Bytes`
  • Loading branch information
ever0de committed Dec 4, 2024
1 parent ec1c53a commit e6900f4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions core/src/types/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,19 @@ impl Buffer {
pub fn to_bytes(&self) -> Bytes {
match &self.0 {
Inner::Contiguous(bytes) => bytes.clone(),
Inner::NonContiguous { .. } => {
let mut ret = BytesMut::with_capacity(self.len());
ret.put(self.clone());
ret.freeze()
Inner::NonContiguous {
parts,
size,
idx: _,
offset,
} => {
if parts.len() == 1 {
parts[0].slice(*offset..(*offset + *size))
} else {
let mut ret = BytesMut::with_capacity(self.len());
ret.put(self.clone());
ret.freeze()
}
}
}
}
Expand Down

0 comments on commit e6900f4

Please sign in to comment.