-
Notifications
You must be signed in to change notification settings - Fork 13k
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
cstring: avoid excessive growth just to 0-terminate #35871
Conversation
Based on following what happens in CString::new("string literal"): 1. Using `Into<Vec<u8>>`, a Vec is allocated with capacity exactly equal to the string's input length. 2. By `v.push(0)`, the Vec is grown to twice capacity, since it was full. 3. By `v.into_boxed_slice()`, the Vec capacity is shrunk to fit the length again. If we use `.reserve_exact(1)` just before the push, then we avoid the capacity doubling that we're going to have to shrink anyway. Growing by just 1 byte means that the step (2) is less likely to have to move the memory to a larger allocation chunk, and that the step (3) does not have to reallocate.
(rust_highfive has picked a reviewer for you, use r? to override) |
The |
That can be addressed by specialization (I imagine, |
📌 Commit 876c02c has been approved by |
cstring: avoid excessive growth just to 0-terminate Based on following what happens in CString::new("string literal"): 1. Using `Into<Vec<u8>>`, a Vec is allocated with capacity exactly equal to the string's input length. 2. By `v.push(0)`, the Vec is grown to twice capacity, since it was full. 3. By `v.into_boxed_slice()`, the Vec capacity is shrunk to fit the length again. If we use `.reserve_exact(1)` just before the push, then we avoid the capacity doubling that we're going to have to shrink anyway. Growing by just 1 byte means that the step (2) is less likely to have to move the memory to a larger allocation chunk, and that the step (3) does not have to reallocate. Addresses part of rust-lang#35838
⌛ Testing commit 876c02c with merge 3c5a0fa... |
cstring: avoid excessive growth just to 0-terminate Based on following what happens in CString::new("string literal"): 1. Using `Into<Vec<u8>>`, a Vec is allocated with capacity exactly equal to the string's input length. 2. By `v.push(0)`, the Vec is grown to twice capacity, since it was full. 3. By `v.into_boxed_slice()`, the Vec capacity is shrunk to fit the length again. If we use `.reserve_exact(1)` just before the push, then we avoid the capacity doubling that we're going to have to shrink anyway. Growing by just 1 byte means that the step (2) is less likely to have to move the memory to a larger allocation chunk, and that the step (3) does not have to reallocate. Addresses part of #35838
Based on following what happens in CString::new("string literal"):
Into<Vec<u8>>
, a Vec is allocated with capacity exactly equalto the string's input length.
v.push(0)
, the Vec is grown to twice capacity, since it was full.v.into_boxed_slice()
, the Vec capacity is shrunk to fit the length again.If we use
.reserve_exact(1)
just before the push, then we avoid thecapacity doubling that we're going to have to shrink anyway.
Growing by just 1 byte means that the step (2) is less likely to have to
move the memory to a larger allocation chunk, and that the step (3) does
not have to reallocate.
Addresses part of #35838