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

Use SmallVec for SmallCStr #53644

Merged
merged 1 commit into from
Aug 24, 2018
Merged

Conversation

llogiq
Copy link
Contributor

@llogiq llogiq commented Aug 23, 2018

This reuses the awesome optimizations from Servo's SmallVec to speed up SmallCStr.

@rust-highfive
Copy link
Collaborator

r? @estebank

(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 Aug 23, 2018
const SIZE: usize = 38;
use smallvec::SmallVec;

const SIZE: usize = 36;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for the change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Array isn't implemented for [T; 38], and due to the union optimization, the on-stack version can take more content per byte.

SmallVec::from_vec(data)
};
if let Err(e) = ffi::CStr::from_bytes_with_nul(&data) {
panic!("The string \"{}\" cannot be converted into a CStr: {}", s, e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you're not introducing this, but should we be returning Result here instead? Probably ok given the usage, but still...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was already discussed during the introduction of the SmallCStr type, the consensus was that ergonomics are more important as long as it's possible to get a stack trace.

data: ffi::CString,
}
pub struct SmallCStr {
data: SmallVec<[u8; SIZE]>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Now I see what you're doing. Why is this an improvement over the OnHeap version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absent the union optimization, this should generate the same code. However with the union optimization, the type is reduced to (usize, [T; _] / *mut T), which saves the discriminant + padding.

Apart from the memory savings, the code has seen a good deal of perf work already.

@estebank
Copy link
Contributor

r=me, but I wan't to know the response for the comments

}

#[inline]
pub fn new_with_zero(s: &str) -> SmallCStr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be called new_with_nul , or from_str_with_nul.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changed.

@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Aug 23, 2018

📌 Commit 25a83e3 has been approved by estebank

@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 Aug 23, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Aug 24, 2018
…estebank

Use SmallVec for SmallCStr

This reuses the awesome optimizations from Servo's `SmallVec` to speed up `SmallCStr`.
bors added a commit that referenced this pull request Aug 24, 2018
Rollup of 16 pull requests

Successful merges:

 - #53311 (Window Mutex: Document that we properly initialize the SRWLock)
 - #53503 (Discourage overuse of mem::forget)
 - #53545 (Fix #50865: ICE on impl-trait returning functions reaching private items)
 - #53559 (add macro check for lint)
 - #53562 (Lament the invincibility of the Turbofish)
 - #53563 (use String::new() instead of String::from(""), "".to_string(), "".to_owned() or "".into())
 - #53592 (docs: minor stylistic changes to str/string docs)
 - #53594 (Update RELEASES.md to include clippy-preview)
 - #53600 (Fix a grammatical mistake in "expected generic arguments" errors)
 - #53614 (update nomicon and book)
 - #53617 (tidy: Stop requiring a license header)
 - #53618 (Add missing fmt examples)
 - #53636 (Prefer `.nth(n)` over `.skip(n).next()`.)
 - #53644 (Use SmallVec for SmallCStr)
 - #53664 (Remove unnecessary closure in rustc_mir/build/mod.rs)
 - #53666 (Added rustc_codegen_llvm to compiler documentation.)
@bors bors merged commit 25a83e3 into rust-lang:master Aug 24, 2018
@llogiq llogiq deleted the smallvec-for-small-c-str branch August 24, 2018 21:05
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.

5 participants