From eaf6c1846f725b2bd31d049fab267e3799a3ea27 Mon Sep 17 00:00:00 2001 From: Jiseok CHOI Date: Wed, 4 Dec 2024 17:46:36 +0900 Subject: [PATCH] feat(types/buffer): skip copying in `to_bytes` when `NonContiguous` contains a single `Bytes` (#5388) --- core/src/types/buffer.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/core/src/types/buffer.rs b/core/src/types/buffer.rs index b4124a920147..2fccda3ad0d5 100644 --- a/core/src/types/buffer.rs +++ b/core/src/types/buffer.rs @@ -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() + } } } }