From 758d3e8cf9dea57265db436cdc8e709f05e9596d Mon Sep 17 00:00:00 2001 From: Roman Timushev Date: Tue, 14 May 2024 16:50:56 +0200 Subject: [PATCH] backport: improve BufList::copy_to_bytes if len == remaining --- src/collect.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/collect.rs b/src/collect.rs index b0b0f0d..0d0d9ae 100644 --- a/src/collect.rs +++ b/src/collect.rs @@ -209,9 +209,15 @@ impl Buf for BufList { } Some(front) if front.remaining() > len => front.copy_to_bytes(len), _ => { - assert!(len <= self.remaining(), "`len` greater than remaining"); + let rem = self.remaining(); + assert!(len <= rem, "`len` greater than remaining"); let mut bm = BytesMut::with_capacity(len); - bm.put(self.take(len)); + if rem == len { + // .take() costs a lot more, so skip it if we don't need it + bm.put(self); + } else { + bm.put(self.take(len)); + } bm.freeze() } }