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

Conversation

@sinkuu
Copy link
Contributor

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

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Oct 18, 2019

r? @Kimundi

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

@sinkuu

This comment has been minimized.

Copy link
Contributor Author

sinkuu commented Oct 18, 2019

cc #35838

@lzutao

This comment has been minimized.

Copy link
Contributor

lzutao commented Oct 19, 2019

r? @eddyb

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Oct 19, 2019

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

@sfackler

This comment has been minimized.

Copy link
Member

sfackler commented Oct 19, 2019

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Oct 19, 2019

📌 Commit 23cb1d5 has been approved by sfackler

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%.
bors added a commit that referenced this pull request Oct 19, 2019
Rollup of 5 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)

Failed merges:

r? @ghost
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
4 checks passed
4 checks passed
pr Build #20191018.20 succeeded
Details
pr (Linux mingw-check) Linux mingw-check succeeded
Details
pr (Linux x86_64-gnu-llvm-6.0) Linux x86_64-gnu-llvm-6.0 succeeded
Details
pr (LinuxTools) LinuxTools succeeded
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.