Skip to content

Commit b00666e

Browse files
committed
add benchmark for iter::ArrayChunks::fold specialization
This also updates the existing iter::Copied::next_chunk benchmark so that the thing it benches doesn't get masked by the ArrayChunks specialization
1 parent d69c33a commit b00666e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

library/core/benches/iter.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::borrow::Borrow;
12
use core::iter::*;
23
use core::mem;
34
use core::num::Wrapping;
@@ -403,13 +404,31 @@ fn bench_trusted_random_access_adapters(b: &mut Bencher) {
403404

404405
/// Exercises the iter::Copied specialization for slice::Iter
405406
#[bench]
406-
fn bench_copied_array_chunks(b: &mut Bencher) {
407+
fn bench_copied_chunks(b: &mut Bencher) {
408+
let v = vec![1u8; 1024];
409+
410+
b.iter(|| {
411+
let mut iter = black_box(&v).iter().copied();
412+
let mut acc = Wrapping(0);
413+
// This uses a while-let loop to side-step the TRA specialization in ArrayChunks
414+
while let Ok(chunk) = iter.next_chunk::<{ mem::size_of::<u64>() }>() {
415+
let d = u64::from_ne_bytes(chunk);
416+
acc += Wrapping(d.rotate_left(7).wrapping_add(1));
417+
}
418+
acc
419+
})
420+
}
421+
422+
/// Exercises the TrustedRandomAccess specialization in ArrayChunks
423+
#[bench]
424+
fn bench_trusted_random_access_chunks(b: &mut Bencher) {
407425
let v = vec![1u8; 1024];
408426

409427
b.iter(|| {
410428
black_box(&v)
411429
.iter()
412-
.copied()
430+
// this shows that we're not relying on the slice::Iter specialization in Copied
431+
.map(|b| *b.borrow())
413432
.array_chunks::<{ mem::size_of::<u64>() }>()
414433
.map(|ary| {
415434
let d = u64::from_ne_bytes(ary);

library/core/benches/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(test)]
66
#![feature(trusted_random_access)]
77
#![feature(iter_array_chunks)]
8+
#![feature(iter_next_chunk)]
89

910
extern crate test;
1011

0 commit comments

Comments
 (0)