From 9f38aea9bbd1d23354382dd0ddc7357d54ce10c3 Mon Sep 17 00:00:00 2001 From: Brad Dunbar Date: Sat, 13 Apr 2024 09:26:13 -0400 Subject: [PATCH] Bytes::split_off - check fast path first Follow up to https://github.com/tokio-rs/bytes/pull/689 * If `at == self.len()`, we already know `at <= self.len()`. * If `at == 0`, we already know `at <= self.len()`. --- src/bytes.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index c3240ce09..908cee9ad 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -385,13 +385,6 @@ impl Bytes { /// Panics if `at > len`. #[must_use = "consider Bytes::truncate if you don't need the other half"] pub fn split_off(&mut self, at: usize) -> Self { - assert!( - at <= self.len(), - "split_off out of bounds: {:?} <= {:?}", - at, - self.len(), - ); - if at == self.len() { return Bytes::new(); } @@ -400,6 +393,13 @@ impl Bytes { return mem::replace(self, Bytes::new()); } + assert!( + at <= self.len(), + "split_off out of bounds: {:?} <= {:?}", + at, + self.len(), + ); + let mut ret = self.clone(); self.len = at;