Skip to content

Commit

Permalink
feat(types/buffer): skip copying in to_bytes when NonContiguous c…
Browse files Browse the repository at this point in the history
…ontains a single `Bytes` (#5388)
  • Loading branch information
ever0de authored Dec 4, 2024
1 parent 6cf1269 commit eaf6c18
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 eaf6c18

Please sign in to comment.