From abf82ff53315a483ada9b506c0c6ed34cc20a238 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Sat, 27 Aug 2016 13:51:37 -0700 Subject: [PATCH 1/2] Add benchmarks for push, insert, extend, and pushpop --- lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib.rs b/lib.rs index e2587a8..6e82e6e 100644 --- a/lib.rs +++ b/lib.rs @@ -5,6 +5,8 @@ //! Small vectors in various sizes. These store a certain number of elements inline and fall back //! to the heap for larger allocations. +#![feature(test)] + use std::borrow::{Borrow, BorrowMut}; use std::cmp; use std::fmt; @@ -647,8 +649,10 @@ impl_array!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, #[cfg(test)] pub mod tests { + extern crate test; use SmallVec; use std::borrow::ToOwned; + use self::test::Bencher; // We heap allocate all these strings so that double frees will show up under valgrind. @@ -1000,4 +1004,43 @@ pub mod tests { assert_eq!(vec.clone().into_iter().len(), 3); assert_eq!(vec.drain().len(), 3); } + + #[bench] + fn bench_push(b: &mut Bencher) { + b.iter(|| { + let mut vec: SmallVec<[u64; 16]> = SmallVec::new(); + for x in 0..100 { + vec.push(x); + } + }); + } + + #[bench] + fn bench_insert(b: &mut Bencher) { + b.iter(|| { + let mut vec: SmallVec<[u64; 16]> = SmallVec::new(); + for x in 0..100 { + vec.insert(0, x); + } + }); + } + + #[bench] + fn bench_extend(b: &mut Bencher) { + b.iter(|| { + let mut vec: SmallVec<[u64; 16]> = SmallVec::new(); + vec.extend(0..100); + }); + } + + #[bench] + fn bench_pushpop(b: &mut Bencher) { + b.iter(|| { + let mut vec: SmallVec<[u64; 16]> = SmallVec::new(); + for x in 0..100 { + vec.push(x); + vec.pop(); + } + }); + } } From 951f1d0bea60b3eb0db665de54c9f426ddb5e264 Mon Sep 17 00:00:00 2001 From: Nipunn Koorapati Date: Sat, 27 Aug 2016 19:45:49 -0700 Subject: [PATCH 2/2] Add .travis.yml. Bench closures return values. Feature gate. --- .travis.yml | 5 +++++ Cargo.toml | 3 +++ lib.rs | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index fd1c19d..87270d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,5 +3,10 @@ rust: - nightly - beta - stable +script: | + cargo build --verbose && + cargo test --verbose && + ([ $TRAVIS_RUST_VERSION != nightly ] || cargo test --verbose --features benchmarks) && + ([ $TRAVIS_RUST_VERSION != nightly ] || cargo bench --verbose --features benchmarks bench) notifications: webhooks: http://build.servo.org:54856/travis diff --git a/Cargo.toml b/Cargo.toml index df4aa31..bf4880b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ documentation = "http://doc.servo.org/smallvec/" name = "smallvec" path = "lib.rs" doctest = false + +[features] +benchmarks = [] diff --git a/lib.rs b/lib.rs index 6e82e6e..c4bae36 100644 --- a/lib.rs +++ b/lib.rs @@ -5,7 +5,7 @@ //! Small vectors in various sizes. These store a certain number of elements inline and fall back //! to the heap for larger allocations. -#![feature(test)] +#![cfg_attr(feature = "benchmarks", feature(test))] use std::borrow::{Borrow, BorrowMut}; use std::cmp; @@ -649,10 +649,8 @@ impl_array!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 24, 32, #[cfg(test)] pub mod tests { - extern crate test; use SmallVec; use std::borrow::ToOwned; - use self::test::Bencher; // We heap allocate all these strings so that double frees will show up under valgrind. @@ -1004,6 +1002,13 @@ pub mod tests { assert_eq!(vec.clone().into_iter().len(), 3); assert_eq!(vec.drain().len(), 3); } +} + +#[cfg(all(feature = "benchmarks", test))] +mod bench { + extern crate test; + use SmallVec; + use self::test::Bencher; #[bench] fn bench_push(b: &mut Bencher) { @@ -1012,6 +1017,7 @@ pub mod tests { for x in 0..100 { vec.push(x); } + vec }); } @@ -1022,6 +1028,7 @@ pub mod tests { for x in 0..100 { vec.insert(0, x); } + vec }); } @@ -1030,6 +1037,7 @@ pub mod tests { b.iter(|| { let mut vec: SmallVec<[u64; 16]> = SmallVec::new(); vec.extend(0..100); + vec }); } @@ -1041,6 +1049,7 @@ pub mod tests { vec.push(x); vec.pop(); } + vec }); } }