benchmarking

legacy
Avril 4 years ago
parent 68d2fae46a
commit 53e9230aa7
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -6,6 +6,12 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["experimental_inserter"]
# Use non-allocating array inserter
experimental_inserter = []
[dependencies] [dependencies]
tokio = {version = "0.2", features=["full"]} tokio = {version = "0.2", features=["full"]}
async-trait = "0.1.40" async-trait = "0.1.40"

@ -3,18 +3,26 @@ use super::*;
pub trait VecExt<T> pub trait VecExt<T>
{ {
/// Insert many elements /// Insert many elements with exact size iterator
fn move_insert<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I) fn insert_exact<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe; where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe;
/// Insert many elements /// Insert many elements
fn splice_insert<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I); fn insert_many<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I);
} }
impl<T> VecExt<T> for Vec<T> impl<T> VecExt<T> for Vec<T>
{ {
fn move_insert<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I) #[cfg(not(feature="experimental_inserter"))]
#[inline(always)]
fn insert_exact<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe
{
self.insert_many(location, slice)
}
#[cfg(feature="experimental_inserter")]
fn insert_exact<Ex, I: IntoIterator<Item = T, IntoIter = Ex>>(&mut self, location: usize, slice: I)
where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe, where Ex: ExactSizeIterator<Item = T> + std::panic::UnwindSafe,
{ {
#[inline(never)] #[inline(never)]
@ -79,7 +87,7 @@ impl<T> VecExt<T> for Vec<T>
self.set_len(len + sent); self.set_len(len + sent);
} }
} }
fn splice_insert<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I) fn insert_many<I: IntoIterator<Item =T>>(&mut self, location: usize, slice: I)
{ {
let slice = slice.into_iter(); let slice = slice.into_iter();
match slice.size_hint() { match slice.size_hint() {
@ -98,17 +106,17 @@ mod tests
{ {
use super::*; use super::*;
#[test] #[test]
fn vec_move_insert() fn vec_insert_exact()
{ {
let mut vec = vec![0,1,2,8,9,10]; let mut vec = vec![0,1,2,8,9,10];
vec.move_insert(3, [3,4,5,6, 7].iter().copied()); vec.insert_exact(3, [3,4,5,6, 7].iter().copied());
assert_eq!(&vec[..], assert_eq!(&vec[..],
&[0,1,2,3,4,5,6,7,8,9,10] &[0,1,2,3,4,5,6,7,8,9,10]
); );
} }
#[test] #[test]
fn vec_move_insert_nt() fn vec_insert_exact_nt()
{ {
macro_rules! string { macro_rules! string {
($str:literal) => (String::from($str)); ($str:literal) => (String::from($str));
@ -128,7 +136,7 @@ mod tests
string!("uhh"), string!("uhh"),
]; ];
vec.move_insert(2, vec![string!("hi"), string!("hello")]); vec.insert_exact(2, vec![string!("hi"), string!("hello")]);
assert_eq!(&vec[..], &vec2[..]); assert_eq!(&vec[..], &vec2[..]);
} }
@ -140,6 +148,16 @@ mod tests
use test::{ use test::{
Bencher, black_box, Bencher, black_box,
}; };
#[cfg(not(feature="experimental_inserter"))]
#[bench]
fn move_exact(b: &mut Bencher)
{
let mut vec = vec![0,10,11,12];
let span = [0,1,2,3];
b.iter(|| {
black_box(vec.insert_exact(2, span.iter().copied()));
});
}
#[bench] #[bench]
fn move_via_splice(b: &mut Bencher) fn move_via_splice(b: &mut Bencher)
{ {
@ -147,18 +165,18 @@ mod tests
let span = [0,1,2,3]; let span = [0,1,2,3];
b.iter(|| { b.iter(|| {
black_box(vec.splice_insert(2, span.iter().copied())); black_box(vec.insert_many(2, span.iter().copied()));
}); });
} }
#[cfg(feature="experimental_inserter")]
#[bench] #[bench]
fn move_via_unsafe(b: &mut Bencher) fn move_via_unsafe(b: &mut Bencher)
{ {
let mut vec = vec![0,10,11,12]; let mut vec = vec![0,10,11,12];
let span = [0,1,2,3]; let span = [0,1,2,3];
b.iter(|| { b.iter(|| {
black_box(vec.move_insert(2, span.iter().copied())); black_box(vec.insert_exact(2, span.iter().copied()));
}); });
} }
} }
} }

@ -26,5 +26,5 @@ mod post;
mod state; mod state;
fn main() { fn main() {
println!("Hello, world!");
} }

Loading…
Cancel
Save