Skip to content

Commit

Permalink
Optimize String::push_byte()
Browse files Browse the repository at this point in the history
```
test new_push_byte ... bench:      6985 ns/iter (+/- 487) = 17 MB/s
test old_push_byte ... bench:     19335 ns/iter (+/- 1368) = 6 MB/s
```

```rust
extern crate test;
use test::Bencher;

static TEXT: &'static str = "\
    Unicode est un standard informatique qui permet des échanges \
    de textes dans différentes langues, à un niveau mondial.";

#[bench]
fn old_push_byte(bencher: &mut Bencher) {
    bencher.bytes = TEXT.len() as u64;
    bencher.iter(|| {
        let mut new = String::new();
        for b in TEXT.bytes() {
            unsafe { new.as_mut_vec().push_all([b]) }
        }
    })
}

#[bench]
fn new_push_byte(bencher: &mut Bencher) {
    bencher.bytes = TEXT.len() as u64;
    bencher.iter(|| {
        let mut new = String::new();
        for b in TEXT.bytes() {
            unsafe { new.as_mut_vec().push(b) }
        }
    })
}
```
  • Loading branch information
SimonSapin committed Jul 6, 2014
1 parent b8ef5cf commit ed3eee2
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/libcollections/string.rs
Expand Up @@ -208,7 +208,7 @@ impl String {
/// Appends a byte to this string buffer. The caller must preserve the valid UTF-8 property.
#[inline]
pub unsafe fn push_byte(&mut self, byte: u8) {
self.push_bytes([byte])
self.vec.push(byte)
}

/// Removes the last byte from the string buffer and returns it. Returns `None` if this string
Expand Down

5 comments on commit ed3eee2

@bors
Copy link
Contributor

@bors bors commented on ed3eee2 Jul 6, 2014

Choose a reason for hiding this comment

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

saw approval from alexcrichton
at SimonSapin@ed3eee2

@bors
Copy link
Contributor

@bors bors commented on ed3eee2 Jul 6, 2014

Choose a reason for hiding this comment

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

merging SimonSapin/rust/patch-4 = ed3eee2 into auto

@bors
Copy link
Contributor

@bors bors commented on ed3eee2 Jul 6, 2014

Choose a reason for hiding this comment

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

SimonSapin/rust/patch-4 = ed3eee2 merged ok, testing candidate = f601c3e

@bors
Copy link
Contributor

@bors bors commented on ed3eee2 Jul 6, 2014

Choose a reason for hiding this comment

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

fast-forwarding master to auto = f601c3e

Please sign in to comment.