The internal buffers within the codec EncodeBuf/DecodeBuf implementations contain what looks like more optimized implementations of BufMut/Buf, which we can take advantage of.
Solution
For DecodeBuf, we can reuse BytesMut's copy_to_bytes implementation, we just need to also update the length.
For EncodeBuf, we should be able to just reuse all of BytesMut's implementation of BufMut, which looks a bit simpler than the default implementation.
Motivation
The internal buffers within the codec
EncodeBuf
/DecodeBuf
implementations contain what looks like more optimized implementations ofBufMut
/Buf
, which we can take advantage of.Solution
For
DecodeBuf
, we can reuseBytesMut
'scopy_to_bytes
implementation, we just need to also update the length.For
EncodeBuf
, we should be able to just reuse all ofBytesMut
's implementation ofBufMut
, which looks a bit simpler than the default implementation.Out of curiosity, I tried running the benchmarks:
Before:
After: