From b24c6fde477dd7a8734a783dc0f4eeb9a1ea4f18 Mon Sep 17 00:00:00 2001 From: Alexis Date: Tue, 10 Feb 2015 13:41:05 -0500 Subject: [PATCH] fix and macro-ify map benches, fixes #22134 --- src/libcollections/bench.rs | 180 +++++++++++++++++--------------- src/libcollections/btree/map.rs | 72 ++----------- src/libcollections/lib.rs | 2 +- src/libcollections/vec_map.rs | 73 ++----------- 4 files changed, 112 insertions(+), 215 deletions(-) diff --git a/src/libcollections/bench.rs b/src/libcollections/bench.rs index b0a5911720a..b59799b4997 100644 --- a/src/libcollections/bench.rs +++ b/src/libcollections/bench.rs @@ -8,103 +8,113 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use prelude::*; -use std::rand; -use std::rand::Rng; -use test::{Bencher, black_box}; +macro_rules! map_insert_rand_bench { + ($name: ident, $n: expr, $map: ident) => ( + #[bench] + pub fn $name(b: &mut ::test::Bencher) { + use std::rand; + use std::rand::Rng; + use test::black_box; -pub fn insert_rand_n(n: usize, - map: &mut M, - b: &mut Bencher, - mut insert: I, - mut remove: R) where - I: FnMut(&mut M, usize), - R: FnMut(&mut M, usize), -{ - // setup - let mut rng = rand::weak_rng(); + let n: usize = $n; + let mut map = $map::new(); + // setup + let mut rng = rand::weak_rng(); - for _ in 0..n { - insert(map, rng.gen::() % n); - } + for _ in 0..n { + let i = rng.gen() % n; + map.insert(i, i); + } - // measure - b.iter(|| { - let k = rng.gen::() % n; - insert(map, k); - remove(map, k); - }); - black_box(map); + // measure + b.iter(|| { + let k = rng.gen() % n; + map.insert(k, k); + map.remove(&k); + }); + black_box(map); + } + ) } -pub fn insert_seq_n(n: usize, - map: &mut M, - b: &mut Bencher, - mut insert: I, - mut remove: R) where - I: FnMut(&mut M, usize), - R: FnMut(&mut M, usize), -{ - // setup - for i in 0..n { - insert(map, i * 2); - } +macro_rules! map_insert_seq_bench { + ($name: ident, $n: expr, $map: ident) => ( + #[bench] + pub fn $name(b: &mut ::test::Bencher) { + use test::black_box; - // measure - let mut i = 1; - b.iter(|| { - insert(map, i); - remove(map, i); - i = (i + 2) % n; - }); - black_box(map); + let mut map = $map::new(); + let n: usize = $n; + // setup + for i in 0..n { + map.insert(i * 2, i * 2); + } + + // measure + let mut i = 1; + b.iter(|| { + map.insert(i, i); + map.remove(&i); + i = (i + 2) % n; + }); + black_box(map); + } + ) } -pub fn find_rand_n(n: usize, - map: &mut M, - b: &mut Bencher, - mut insert: I, - mut find: F) where - I: FnMut(&mut M, usize), - F: FnMut(&M, usize) -> T, -{ - // setup - let mut rng = rand::weak_rng(); - let mut keys: Vec<_> = (0..n).map(|_| rng.gen::() % n).collect(); +macro_rules! map_find_rand_bench { + ($name: ident, $n: expr, $map: ident) => ( + #[bench] + pub fn $name(b: &mut ::test::Bencher) { + use std::rand; + use std::rand::Rng; + use test::black_box; - for k in &keys { - insert(map, *k); - } + let mut map = $map::new(); + let n: usize = $n; - rng.shuffle(&mut keys); + // setup + let mut rng = rand::weak_rng(); + let mut keys: Vec<_> = (0..n).map(|_| rng.gen() % n).collect(); - // measure - let mut i = 0; - b.iter(|| { - let t = find(map, keys[i]); - i = (i + 1) % n; - black_box(t); - }) + for &k in &keys { + map.insert(k, k); + } + + rng.shuffle(&mut keys); + + // measure + let mut i = 0; + b.iter(|| { + let t = map.get(&keys[i]); + i = (i + 1) % n; + black_box(t); + }) + } + ) } -pub fn find_seq_n(n: usize, - map: &mut M, - b: &mut Bencher, - mut insert: I, - mut find: F) where - I: FnMut(&mut M, usize), - F: FnMut(&M, usize) -> T, -{ - // setup - for i in 0..n { - insert(map, i); - } +macro_rules! map_find_seq_bench { + ($name: ident, $n: expr, $map: ident) => ( + #[bench] + pub fn $name(b: &mut ::test::Bencher) { + use test::black_box; - // measure - let mut i = 0; - b.iter(|| { - let x = find(map, i); - i = (i + 1) % n; - black_box(x); - }) + let mut map = $map::new(); + let n: usize = $n; + + // setup + for i in 0..n { + map.insert(i, i); + } + + // measure + let mut i = 0; + b.iter(|| { + let x = map.get(&i); + i = (i + 1) % n; + black_box(x); + }) + } + ) } diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index aec50d53808..31604bcd5b9 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -1843,74 +1843,18 @@ mod bench { use test::{Bencher, black_box}; use super::BTreeMap; - use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n}; - #[bench] - pub fn insert_rand_100(b: &mut Bencher) { - let mut m = BTreeMap::new(); - insert_rand_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } + map_insert_rand_bench!{insert_rand_100, 100, BTreeMap} + map_insert_rand_bench!{insert_rand_10_000, 10_000, BTreeMap} - #[bench] - pub fn insert_rand_10_000(b: &mut Bencher) { - let mut m = BTreeMap::new(); - insert_rand_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } + map_insert_seq_bench!{insert_seq_100, 100, BTreeMap} + map_insert_seq_bench!{insert_seq_10_000, 10_000, BTreeMap} - // Insert seq - #[bench] - pub fn insert_seq_100(b: &mut Bencher) { - let mut m = BTreeMap::new(); - insert_seq_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } + map_find_rand_bench!{find_rand_100, 100, BTreeMap} + map_find_rand_bench!{find_rand_10_000, 10_000, BTreeMap} - #[bench] - pub fn insert_seq_10_000(b: &mut Bencher) { - let mut m = BTreeMap::new(); - insert_seq_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } - - // Find rand - #[bench] - pub fn find_rand_100(b: &mut Bencher) { - let mut m = BTreeMap::new(); - find_rand_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } - - #[bench] - pub fn find_rand_10_000(b: &mut Bencher) { - let mut m = BTreeMap::new(); - find_rand_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } - - // Find seq - #[bench] - pub fn find_seq_100(b: &mut Bencher) { - let mut m = BTreeMap::new(); - find_seq_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } - - #[bench] - pub fn find_seq_10_000(b: &mut Bencher) { - let mut m = BTreeMap::new(); - find_seq_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } + map_find_seq_bench!{find_seq_100, 100, BTreeMap} + map_find_seq_bench!{find_seq_10_000, 10_000, BTreeMap} fn bench_iter(b: &mut Bencher, size: i32) { let mut map = BTreeMap::::new(); diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index f220724c42e..6b7aa2546cc 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -101,7 +101,7 @@ pub mod btree_set { } -#[cfg(test)] mod bench; +#[cfg(test)] #[macro_use] mod bench; // FIXME(#14344) this shouldn't be necessary #[doc(hidden)] diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 739b8d8ce19..ddd4713e4cc 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -1321,74 +1321,17 @@ mod test_map { #[cfg(test)] mod bench { - use test::Bencher; use super::VecMap; - use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n}; - #[bench] - pub fn insert_rand_100(b: &mut Bencher) { - let mut m = VecMap::new(); - insert_rand_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } + map_insert_rand_bench!{insert_rand_100, 100, VecMap} + map_insert_rand_bench!{insert_rand_10_000, 10_000, VecMap} - #[bench] - pub fn insert_rand_10_000(b: &mut Bencher) { - let mut m = VecMap::new(); - insert_rand_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } + map_insert_seq_bench!{insert_seq_100, 100, VecMap} + map_insert_seq_bench!{insert_seq_10_000, 10_000, VecMap} - // Insert seq - #[bench] - pub fn insert_seq_100(b: &mut Bencher) { - let mut m = VecMap::new(); - insert_seq_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } + map_find_rand_bench!{find_rand_100, 100, VecMap} + map_find_rand_bench!{find_rand_10_000, 10_000, VecMap} - #[bench] - pub fn insert_seq_10_000(b: &mut Bencher) { - let mut m = VecMap::new(); - insert_seq_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.remove(&i); }); - } - - // Find rand - #[bench] - pub fn find_rand_100(b: &mut Bencher) { - let mut m = VecMap::new(); - find_rand_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } - - #[bench] - pub fn find_rand_10_000(b: &mut Bencher) { - let mut m = VecMap::new(); - find_rand_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } - - // Find seq - #[bench] - pub fn find_seq_100(b: &mut Bencher) { - let mut m = VecMap::new(); - find_seq_n(100, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } - - #[bench] - pub fn find_seq_10_000(b: &mut Bencher) { - let mut m = VecMap::new(); - find_seq_n(10_000, &mut m, b, - |m, i| { m.insert(i, 1); }, - |m, i| { m.get(&i); }); - } + map_find_seq_bench!{find_seq_100, 100, VecMap} + map_find_seq_bench!{find_seq_10_000, 10_000, VecMap} }