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

collections: Move optimized String::from_str to String::from #24517

Merged
merged 1 commit into from
Apr 19, 2015

Conversation

erickt
Copy link
Contributor

@erickt erickt commented Apr 17, 2015

This implementation is currently about 3-4 times faster than using the .to_string() based approach.

I would also suggest we deprecate String::from_str since it's redundant with the stable String::from method, but I'll leave that for a future PR.

@rust-highfive
Copy link
Collaborator

r? @pcwalton

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

@@ -1015,7 +1015,7 @@ impl AsRef<str> for String {
impl<'a> From<&'a str> for String {
#[inline]
fn from(s: &'a str) -> String {
s.to_string()
String { vec: <[_]>::to_vec(string.as_bytes()) }
Copy link
Contributor

Choose a reason for hiding this comment

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

As an onlooker, I'm a bit confused here. Where is the string variable coming from, or do you mean to have s?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shame on me! you're right. Let me fix that.

let s = "Hello there, the quick brown fox jumped over the lazy dog! \
Lorem ipsum dolor sit amet, consectetur. ";
b.iter(|| {
test::black_box(String::from_str(s));
Copy link
Member

Choose a reason for hiding this comment

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

It's ok to actually omit these calls to test::black_box, whatever is returned from the closure will be passed to black_box automatically (so these could all just return the string)

@alexcrichton
Copy link
Member

Nice! r=me with the benchmark tweaks

@erickt
Copy link
Contributor Author

erickt commented Apr 17, 2015

@bors: r=alexcrichton c8841d2

@frewsxcv
Copy link
Member

Is it possible to impl ToString for str and add this logic there so we can get this same speedup in both places?

@frewsxcv
Copy link
Member

Nevermind, I don't think my previous idea works.

@bors
Copy link
Contributor

bors commented Apr 18, 2015

⌛ Testing commit c8841d2 with merge fb01e4b...

@bors
Copy link
Contributor

bors commented Apr 18, 2015

💔 Test failed - auto-linux-64-nopt-t

This implementation is currently about 3-4 times faster than using
the `.to_string()` based approach.
@erickt
Copy link
Contributor Author

erickt commented Apr 19, 2015

@bors r=alexcrichton f055054

@bors
Copy link
Contributor

bors commented Apr 19, 2015

⌛ Testing commit f055054 with merge da355ef...

bors added a commit that referenced this pull request Apr 19, 2015
This implementation is currently about 3-4 times faster than using the `.to_string()` based approach.

I would also suggest we deprecate `String::from_str` since it's redundant with the stable `String::from` method, but I'll leave that for a future PR.
@bors bors merged commit f055054 into rust-lang:master Apr 19, 2015
Kintaro pushed a commit to Kintaro/wtftw that referenced this pull request Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants