about summary refs log tree commit diff
path: root/src/rustllvm/RustWrapper.cpp
diff options
context:
space:
mode:
authorUlrik Sverdrup <bluss@users.noreply.github.com>2016-08-21 13:33:28 +0200
committerUlrik Sverdrup <bluss@users.noreply.github.com>2016-08-21 13:37:33 +0200
commit876c02cc1ab537703c4d6f19693dd9a42b131442 (patch)
treeee270c5f75895f0282d61db82ebf34ded55d2c96 /src/rustllvm/RustWrapper.cpp
parente25542cb02d946959517ebb68e2f7b1318817237 (diff)
downloadrust-876c02cc1ab537703c4d6f19693dd9a42b131442.tar.gz
rust-876c02cc1ab537703c4d6f19693dd9a42b131442.zip
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.
Diffstat (limited to 'src/rustllvm/RustWrapper.cpp')
0 files changed, 0 insertions, 0 deletions