From 4db4e2146bd74c2f23b67037842c75a477023fb8 Mon Sep 17 00:00:00 2001 From: Gus Wynn Date: Fri, 5 Mar 2021 10:08:30 -0800 Subject: [PATCH 1/2] Avoid extraneous call to remaining in get_u8/i8 I have a chained datastructure that can dereference into a slice quite fast, but remaining requires walking the chain, which is relatively slow. I would hazard a guess that this is the more common perf characteristic of non-contiguous impls of Buf. Separately, this brings these impls in line with all the other get_ impls, which have a fast path with [u8].get(...), which does the same bounds check that I do in this patch I think clippy will say to use non_empty, here, I think in this case this is more clear, but I will do whatever is preferred. --- src/buf/buf_impl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index a33c8a42d..e319441d5 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -285,7 +285,7 @@ pub trait Buf { /// /// This function panics if there is no more remaining data in `self`. fn get_u8(&mut self) -> u8 { - assert!(self.remaining() >= 1); + assert!(self.chunk().len() >= 1); let ret = self.chunk()[0]; self.advance(1); ret @@ -308,7 +308,7 @@ pub trait Buf { /// /// This function panics if there is no more remaining data in `self`. fn get_i8(&mut self) -> i8 { - assert!(self.remaining() >= 1); + assert!(self.chunk().len() >= 1); let ret = self.chunk()[0] as i8; self.advance(1); ret From 2a6cc0f0abeeb256f4ff0f93c41d47239c822cb5 Mon Sep 17 00:00:00 2001 From: Gus Wynn Date: Fri, 5 Mar 2021 11:10:49 -0800 Subject: [PATCH 2/2] make docs more clear about implementation requirements --- src/buf/buf_impl.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs index e319441d5..a87bf3304 100644 --- a/src/buf/buf_impl.rs +++ b/src/buf/buf_impl.rs @@ -125,8 +125,9 @@ pub trait Buf { /// # Implementer notes /// /// This function should never panic. Once the end of the buffer is reached, - /// i.e., `Buf::remaining` returns 0, calls to `chunk()` should return an - /// empty slice. + /// i.e., `Buf::remaining` returns 0, calls to `chunk()` must return an + /// empty slice. If there is remaining data, calls must return a slice with at + /// least 1 byte. // The `chunk` method was previously called `bytes`. This alias makes the rename // more easily discoverable. #[cfg_attr(docsrs, doc(alias = "bytes"))]