Skip to content

Add benchmarks for push, insert, extend, and pushpop #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ documentation = "http://doc.servo.org/smallvec/"
name = "smallvec"
path = "lib.rs"
doctest = false

[features]
benchmarks = []
52 changes: 52 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

#![cfg_attr(feature = "benchmarks", feature(test))]

use std::borrow::{Borrow, BorrowMut};
use std::cmp;
use std::fmt;
Expand Down Expand Up @@ -1001,3 +1003,53 @@ pub mod tests {
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) {
b.iter(|| {
let mut vec: SmallVec<[u64; 16]> = SmallVec::new();
for x in 0..100 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think the compiler will be pretty much able to optimize these loops out. IIRC there was some builtin support for black box testing for this kind of things. I think we should be able to enforce that using it, or at least confirm it's not being optimized out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe if the closure returns a value then the optimizer will not remove it.

I was empirically able to determine that the optimizer was not removing the code either way (for now), but I will include the vec return value to ensure it.

vec.push(x);
}
vec
});
}

#[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);
}
vec
});
}

#[bench]
fn bench_extend(b: &mut Bencher) {
b.iter(|| {
let mut vec: SmallVec<[u64; 16]> = SmallVec::new();
vec.extend(0..100);
vec
});
}

#[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();
}
vec
});
}
}