summaryrefslogtreecommitdiffstats
path: root/library/core/benches
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-30 03:57:31 +0000
commitdc0db358abe19481e475e10c32149b53370f1a1c (patch)
treeab8ce99c4b255ce46f99ef402c27916055b899ee /library/core/benches
parentReleasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff)
downloadrustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz
rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/core/benches')
-rw-r--r--library/core/benches/iter.rs52
-rw-r--r--library/core/benches/slice.rs9
2 files changed, 61 insertions, 0 deletions
diff --git a/library/core/benches/iter.rs b/library/core/benches/iter.rs
index 60ef83223..5ec22e514 100644
--- a/library/core/benches/iter.rs
+++ b/library/core/benches/iter.rs
@@ -2,6 +2,7 @@ use core::borrow::Borrow;
use core::iter::*;
use core::mem;
use core::num::Wrapping;
+use core::ops::Range;
use test::{black_box, Bencher};
#[bench]
@@ -69,6 +70,57 @@ fn bench_max(b: &mut Bencher) {
})
}
+#[bench]
+fn bench_range_step_by_sum_reducible(b: &mut Bencher) {
+ let r = 0u32..1024;
+ b.iter(|| {
+ let r = black_box(r.clone()).step_by(8);
+
+ let mut sum: u32 = 0;
+ for i in r {
+ sum += i;
+ }
+
+ sum
+ })
+}
+
+#[bench]
+fn bench_range_step_by_loop_u32(b: &mut Bencher) {
+ let r = 0..(u16::MAX as u32);
+ b.iter(|| {
+ let r = black_box(r.clone()).step_by(64);
+
+ let mut sum: u32 = 0;
+ for i in r {
+ let i = i ^ i.wrapping_sub(1);
+ sum = sum.wrapping_add(i);
+ }
+
+ sum
+ })
+}
+
+#[bench]
+fn bench_range_step_by_fold_usize(b: &mut Bencher) {
+ let r: Range<usize> = 0..(u16::MAX as usize);
+ b.iter(|| {
+ let r = black_box(r.clone());
+ r.step_by(64)
+ .map(|x: usize| x ^ (x.wrapping_sub(1)))
+ .fold(0usize, |acc, i| acc.wrapping_add(i))
+ })
+}
+
+#[bench]
+fn bench_range_step_by_fold_u16(b: &mut Bencher) {
+ let r: Range<u16> = 0..u16::MAX;
+ b.iter(|| {
+ let r = black_box(r.clone());
+ r.step_by(64).map(|x: u16| x ^ (x.wrapping_sub(1))).fold(0u16, |acc, i| acc.wrapping_add(i))
+ })
+}
+
pub fn copy_zip(xs: &[u8], ys: &mut [u8]) {
for (a, b) in ys.iter_mut().zip(xs) {
*a = *b;
diff --git a/library/core/benches/slice.rs b/library/core/benches/slice.rs
index 9b86a0ca9..3bfb35e68 100644
--- a/library/core/benches/slice.rs
+++ b/library/core/benches/slice.rs
@@ -1,3 +1,4 @@
+use core::ptr::NonNull;
use test::black_box;
use test::Bencher;
@@ -162,3 +163,11 @@ fn fill_byte_sized(b: &mut Bencher) {
black_box(slice.fill(black_box(NewType(42))));
});
}
+
+// Tests the ability of the compiler to recognize that only the last slice item is needed
+// based on issue #106288
+#[bench]
+fn fold_to_last(b: &mut Bencher) {
+ let slice: &[i32] = &[0; 1024];
+ b.iter(|| black_box(slice).iter().fold(None, |_, r| Some(NonNull::from(r))));
+}