about summary refs log tree commit diff
diff options
context:
space:
mode:
authorThom Chiovoloni <tchiovoloni@mozilla.com>2020-07-05 10:20:01 -0700
committerThom Chiovoloni <tchiovoloni@mozilla.com>2020-07-05 10:23:50 -0700
commit63e2e2e32674ebe662b927c139931ffd8a352313 (patch)
treefa4dad65301fc45c537b4d48ff54c14fca2f2847
parent980d8e1a0b18e89129cce23bb5a46c6a498dc5d2 (diff)
downloadrust-63e2e2e32674ebe662b927c139931ffd8a352313.tar.gz
rust-63e2e2e32674ebe662b927c139931ffd8a352313.zip
Avoid `vec!` allocation in `is_ascii_slice_*` benches
-rw-r--r--src/libcore/benches/ascii.rs45
1 files changed, 36 insertions, 9 deletions
diff --git a/src/libcore/benches/ascii.rs b/src/libcore/benches/ascii.rs
index 142f3950eec..96f2b9a90c6 100644
--- a/src/libcore/benches/ascii.rs
+++ b/src/libcore/benches/ascii.rs
@@ -58,7 +58,31 @@ macro_rules! benches {
                 }
             )+
         }
-    }
+    };
+
+    // For some tests the vec allocation tends to dominate, so it can be avoided.
+    (@readonly $( fn $name: ident($arg: ident: &[u8]) $body: block )+) => {
+        benches!(@ro mod short_readonly SHORT $($name $arg $body)+);
+        benches!(@ro mod medium_readonly MEDIUM $($name $arg $body)+);
+        benches!(@ro mod long_readonly LONG $($name $arg $body)+);
+    };
+    (@ro mod $mod_name: ident $input: ident $($name: ident $arg: ident $body: block)+) => {
+        mod $mod_name {
+            use super::*;
+
+            $(
+                #[bench]
+                fn $name(bencher: &mut Bencher) {
+                    bencher.bytes = $input.len() as u64;
+                    let vec = $input.as_bytes().to_vec();
+                    bencher.iter(|| {
+                        let $arg = black_box(&vec[..]);
+                        black_box($body)
+                    })
+                }
+            )+
+        }
+    };
 }
 
 use test::black_box;
@@ -230,14 +254,6 @@ benches! {
         }
     }
 
-    fn is_ascii_slice_libcore(bytes: &mut [u8]) {
-        bytes.is_ascii()
-    }
-
-    fn is_ascii_slice_iter_all(bytes: &mut [u8]) {
-        bytes.iter().all(|b| b.is_ascii())
-    }
-
     @iter
 
     is_ascii,
@@ -253,6 +269,17 @@ benches! {
     is_ascii_control,
 }
 
+benches! {
+    @readonly
+    fn is_ascii_slice_libcore(bytes: &[u8]) {
+        bytes.is_ascii()
+    }
+
+    fn is_ascii_slice_iter_all(bytes: &[u8]) {
+        bytes.iter().all(|b| b.is_ascii())
+    }
+}
+
 macro_rules! repeat {
     ($s: expr) => {
         concat!($s, $s, $s, $s, $s, $s, $s, $s, $s, $s)