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 realloc in CString::new #65551

Merged
merged 1 commit into from
Oct 20, 2019
Merged

Avoid realloc in CString::new #65551

merged 1 commit into from
Oct 20, 2019

Conversation

sinkuu
Copy link
Contributor

@sinkuu sinkuu commented Oct 18, 2019

If &[u8] or &str is given, CString::new allocates a new Vec with the exact capacity, and then CString::from_vec_unchecked calls .reserve_exact(1) for nul byte. This PR avoids the reallocation by allocationg len + 1 bytes beforehand. In microbenchmark this PR speeds up CString::new(&[u8]) by 30%.

@rust-highfive
Copy link
Collaborator

r? @Kimundi

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 18, 2019
@sinkuu
Copy link
Contributor Author

sinkuu commented Oct 18, 2019

cc #35838

@tesuji
Copy link
Contributor

tesuji commented Oct 19, 2019

r? @eddyb

@eddyb
Copy link
Member

eddyb commented Oct 19, 2019

LGTM but this is a libs change, cc @alexcrichton @sfackler.

@sfackler
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Oct 19, 2019

📌 Commit 23cb1d5 has been approved by sfackler

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 19, 2019
Centril added a commit to Centril/rust that referenced this pull request Oct 19, 2019
Avoid realloc in `CString::new`

If `&[u8]` or `&str` is given, `CString::new` allocates a new `Vec` with the exact capacity, and then `CString::from_vec_unchecked` calls `.reserve_exact(1)` for nul byte. This PR avoids the reallocation by allocationg `len + 1` bytes beforehand. In microbenchmark this PR speeds up `CString::new(&[u8])` by 30%.
Centril added a commit to Centril/rust that referenced this pull request Oct 20, 2019
Avoid realloc in `CString::new`

If `&[u8]` or `&str` is given, `CString::new` allocates a new `Vec` with the exact capacity, and then `CString::from_vec_unchecked` calls `.reserve_exact(1)` for nul byte. This PR avoids the reallocation by allocationg `len + 1` bytes beforehand. In microbenchmark this PR speeds up `CString::new(&[u8])` by 30%.
bors added a commit that referenced this pull request Oct 20, 2019
Rollup of 6 pull requests

Successful merges:

 - #64996 (Inline `ptr::null(_mut)` even in debug builds)
 - #65551 (Avoid realloc in `CString::new`)
 - #65593 (add test for calling non-const fn)
 - #65595 (move `parse_cfgspecs` to `rustc_interface`)
 - #65600 (Remove unneeded `ref` from docs)
 - #65602 (Fix plural mistake in emitter.rs)

Failed merges:

r? @ghost
@bors bors merged commit 23cb1d5 into rust-lang:master Oct 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants