Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid large reallocations when freezing BytesMut #592

Merged
merged 1 commit into from
Jan 31, 2023

Commits on Jan 31, 2023

  1. Avoid large reallocations when freezing BytesMut (tokio-rs#587)

    When we freeze a BytesMut, we turn it into a Vec, and then convert that
    to a Bytes.  Currently, this happen using Vec::into_boxed_slice, which
    reallocates to a slice of the same length as the Vev if the length and
    the capacity are not equal.  This can pose a performance problem if the
    Vec is large or if this happens many times in a loop.
    
    Instead, let's compare the length and capacity, and if they're the same,
    continue to handle this using into_boxed_slice.  Otherwise, since we
    have a type of vtable which can handle a separate capacity, the shared
    vtable, let's turn our Vec into that kind of Bytes.  While this does not
    avoid allocation altogether, it performs a fixed size allocation and
    avoids any need to memcpy.
    bk2204 committed Jan 31, 2023
    Configuration menu
    Copy the full SHA
    9e0d5b6 View commit details
    Browse the repository at this point in the history