Skip to content

Commit

Permalink
Specialize StepBy<Range<{integer}>>
Browse files Browse the repository at this point in the history
For ranges < usize we determine the number of items
StepBy would yield and then store that in the range.end
instead of the actual end. This significantly
simplifies calculation of the loop induction variable
especially in cases where StepBy::step (an usize)
could overflow the Range's item type
  • Loading branch information
the8472 committed Jun 22, 2023
1 parent 4051305 commit 070ce23
Show file tree
Hide file tree
Showing 3 changed files with 428 additions and 36 deletions.
52 changes: 52 additions & 0 deletions library/core/benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 070ce23

Please sign in to comment.