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

Add BytesMut::zeroed #517

Merged
merged 1 commit into from Nov 24, 2021
Merged

Add BytesMut::zeroed #517

merged 1 commit into from Nov 24, 2021

Conversation

ldm0
Copy link
Contributor

@ldm0 ldm0 commented Nov 21, 2021

This PR adds a convenient method BytesMut::zeroed for creating zero-filled BytesMut, which is more convenient than new a BytesMut and resize it with zero afterward. BytesMut::zeroed is also usually much faster.

Closes #270

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me.

@Darksonn Darksonn merged commit 68afb40 into tokio-rs:master Nov 24, 2021
@ldm0 ldm0 deleted the zeroed branch November 24, 2021 12:06
lelongg pushed a commit to lelongg/bytes that referenced this pull request Jan 9, 2023
bk2204 added a commit to bk2204/bytes that referenced this pull request Jan 29, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support zeroed allocations (e.g reserve_zeroed, with_capacity_zeroed)
2 participants