]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/string.rs
rollup merge of #20079: SimonSapin/string_push_ascii_fast_path
[rust.git] / src / libcollections / string.rs
index 3837dd703beaeff7b9464fcf929590675ef52916..76f69dbdb744bdd2c322fb461e7a4cff3023be4c 100644 (file)
@@ -513,6 +513,11 @@ pub fn shrink_to_fit(&mut self) {
     #[inline]
     #[stable = "function just renamed from push_char"]
     pub fn push(&mut self, ch: char) {
+        if (ch as u32) < 0x80 {
+            self.vec.push(ch as u8);
+            return;
+        }
+
         let cur_len = self.len();
         // This may use up to 4 bytes.
         self.vec.reserve(4);
@@ -1401,6 +1406,41 @@ fn bench_push_str(b: &mut Bencher) {
         });
     }
 
+    const REPETITIONS: u64 = 10_000;
+
+    #[bench]
+    fn bench_push_str_one_byte(b: &mut Bencher) {
+        b.bytes = REPETITIONS;
+        b.iter(|| {
+            let mut r = String::new();
+            for _ in range(0, REPETITIONS) {
+                r.push_str("a")
+            }
+        });
+    }
+
+    #[bench]
+    fn bench_push_char_one_byte(b: &mut Bencher) {
+        b.bytes = REPETITIONS;
+        b.iter(|| {
+            let mut r = String::new();
+            for _ in range(0, REPETITIONS) {
+                r.push('a')
+            }
+        });
+    }
+
+    #[bench]
+    fn bench_push_char_two_bytes(b: &mut Bencher) {
+        b.bytes = REPETITIONS * 2;
+        b.iter(|| {
+            let mut r = String::new();
+            for _ in range(0, REPETITIONS) {
+                r.push('รข')
+            }
+        });
+    }
+
     #[bench]
     fn from_utf8_lossy_100_ascii(b: &mut Bencher) {
         let s = b"Hello there, the quick brown fox jumped over the lazy dog! \