tokio-rs / bytes

Utilities for working with bytes
MIT License
1.86k stars 278 forks source link

perf: improve Bytes::copy_to_bytes #688

Closed tisonkun closed 5 months ago

tisonkun commented 5 months ago

Ref - https://greptime.com/blogs/2024-04-09-rust-protobuf-performance#step-5-why-is-bytes-slice-so-slow

We already use this tech for BytesMut, but not bring it for Bytes. Namely, reduce the overhead of slice.

impl Buf for BytesMut {
    ...
    fn copy_to_bytes(&mut self, len: usize) -> Bytes {
        self.split_to(len).freeze()
    }
}
tisonkun commented 5 months ago

cc @Darksonn

tisonkun commented 5 months ago

@braddunbar shall we merge this patch? Or it's still wait for further reviews?

tisonkun commented 5 months ago

Thank you!

Darksonn commented 5 months ago

Thank you for the PR :)