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

Suggestion: Make BytesMut::resize inline. #769

Open
Sol-Ell opened this issue Feb 7, 2025 · 0 comments
Open

Suggestion: Make BytesMut::resize inline. #769

Sol-Ell opened this issue Feb 7, 2025 · 0 comments

Comments

@Sol-Ell
Copy link

Sol-Ell commented Feb 7, 2025

Problem

The current implementation of BytesMut::resize is not inlined, which may lead to unnecessary function calls.

[Current implementation](

bytes/src/bytes_mut.rs

Lines 479 to 500 in e0f3a25

pub fn resize(&mut self, new_len: usize, value: u8) {
let additional = if let Some(additional) = new_len.checked_sub(self.len()) {
additional
} else {
self.truncate(new_len);
return;
};
if additional == 0 {
return;
}
self.reserve(additional);
let dst = self.spare_capacity_mut().as_mut_ptr();
// SAFETY: `spare_capacity_mut` returns a valid, properly aligned pointer and we've
// reserved enough space to write `additional` bytes.
unsafe { ptr::write_bytes(dst, value, additional) };
// SAFETY: There are at least `new_len` initialized bytes in the buffer so no
// uninitialized bytes are being exposed.
unsafe { self.set_len(new_len) };
}
)

Proposed Solution

Partially inline BytesMut::resize to reduce function call overhead and improve performance.

[Updated implementation](https://github.com/Sol-Ell/bytes/blob/ed9989b499cf390340cf255548d7a898aea29ad3/src/bytes_mut.rs#L479-L500)

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

No branches or pull requests

1 participant