Skip to content

Commit

Permalink
Auto merge of rust-lang#82576 - gilescope:to_string, r=Amanieu
Browse files Browse the repository at this point in the history
i8 and u8::to_string() specialisation (far less asm).

Take 2. Around 1/6th of the assembly to without specialisation.

https://godbolt.org/z/bzz8Mq

(partially fixes rust-lang#73533 )
  • Loading branch information
bors committed May 2, 2021
2 parents 6b5de7a + 05330aa commit 8a8ed07
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2288,6 +2288,47 @@ impl ToString for char {
}
}

#[stable(feature = "u8_to_string_specialization", since = "1.999.0")]
impl ToString for u8 {
#[inline]
fn to_string(&self) -> String {
let mut buf = String::with_capacity(3);
let mut n = *self;
if n >= 10 {
if n >= 100 {
buf.push((b'0' + n / 100) as char);
n %= 100;
}
buf.push((b'0' + n / 10) as char);
n %= 10;
}
buf.push((b'0' + n) as char);
buf
}
}

#[stable(feature = "i8_to_string_specialization", since = "1.999.0")]
impl ToString for i8 {
#[inline]
fn to_string(&self) -> String {
let mut buf = String::with_capacity(4);
if self.is_negative() {
buf.push('-');
}
let mut n = self.unsigned_abs();
if n >= 10 {
if n >= 100 {
buf.push('1');
n -= 100;
}
buf.push((b'0' + n / 10) as char);
n %= 10;
}
buf.push((b'0' + n) as char);
buf
}
}

#[stable(feature = "str_to_string_specialization", since = "1.9.0")]
impl ToString for str {
#[inline]
Expand Down

0 comments on commit 8a8ed07

Please sign in to comment.