-
Notifications
You must be signed in to change notification settings - Fork 147
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
Add a push_light method and use likely/unlikely intrinsics #134
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,12 @@ use smallvec::{ExtendFromSlice, SmallVec}; | |
|
||
const VEC_SIZE: usize = 16; | ||
const SPILLED_SIZE: usize = 100; | ||
const LARGE_SIZE: usize = 10000; | ||
|
||
trait Vector<T>: for<'a> From<&'a [T]> + Extend<T> + ExtendFromSlice<T> { | ||
fn new() -> Self; | ||
fn push(&mut self, val: T); | ||
fn push_light(&mut self, val: T); | ||
fn pop(&mut self) -> Option<T>; | ||
fn remove(&mut self, p: usize) -> T; | ||
fn insert(&mut self, n: usize, val: T); | ||
|
@@ -29,6 +31,8 @@ impl<T: Copy> Vector<T> for Vec<T> { | |
self.push(val) | ||
} | ||
|
||
fn push_light(&mut self, val: T) {} | ||
|
||
fn pop(&mut self) -> Option<T> { | ||
self.pop() | ||
} | ||
|
@@ -59,6 +63,14 @@ impl<T: Copy> Vector<T> for SmallVec<[T; VEC_SIZE]> { | |
self.push(val) | ||
} | ||
|
||
#[cfg(feature = "push_light")] | ||
fn push_light(&mut self, val: T) { | ||
self.push_light(val) | ||
} | ||
|
||
#[cfg(not(feature = "push_light"))] | ||
fn push_light(&mut self, _: T) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why this is defined if the feature is missing, and why it does nothing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's to ensure things still compile There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It compiles sure, but it doesn't do the right thing, why should it be a noop if the feature is missing? Sounds like a way to have silent bugs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not meaningful to benchmark a feature if it's disabled There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh ok sorry I didn't notice this was an implementation for benchmarks. |
||
|
||
fn pop(&mut self) -> Option<T> { | ||
self.pop() | ||
} | ||
|
@@ -93,8 +105,12 @@ macro_rules! make_benches { | |
|
||
make_benches! { | ||
SmallVec<[u64; VEC_SIZE]> { | ||
bench_push => gen_push(SPILLED_SIZE as _), | ||
bench_push_light_small => gen_push_light(VEC_SIZE as _), | ||
bench_push_light => gen_push_light(SPILLED_SIZE as _), | ||
bench_push_light_large => gen_push_light(LARGE_SIZE as _), | ||
bench_push_small => gen_push(VEC_SIZE as _), | ||
bench_push => gen_push(SPILLED_SIZE as _), | ||
bench_push_large => gen_push(LARGE_SIZE as _), | ||
bench_insert => gen_insert(SPILLED_SIZE as _), | ||
bench_insert_small => gen_insert(VEC_SIZE as _), | ||
bench_remove => gen_remove(SPILLED_SIZE as _), | ||
|
@@ -150,6 +166,21 @@ fn gen_push<V: Vector<u64>>(n: u64, b: &mut Bencher) { | |
}); | ||
} | ||
|
||
fn gen_push_light<V: Vector<u64>>(n: u64, b: &mut Bencher) { | ||
#[inline(never)] | ||
fn push_light_noinline<V: Vector<u64>>(vec: &mut V, x: u64) { | ||
vec.push_light(x); | ||
} | ||
|
||
b.iter(|| { | ||
let mut vec = V::new(); | ||
for x in 0..n { | ||
push_light_noinline(&mut vec, x); | ||
} | ||
vec | ||
}); | ||
} | ||
|
||
fn gen_insert<V: Vector<u64>>(n: u64, b: &mut Bencher) { | ||
#[inline(never)] | ||
fn insert_noinline<V: Vector<u64>>(vec: &mut V, p: usize, x: u64) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "testing" | ||
version = "0.1.0" | ||
authors = ["John Kåre Alsaker <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["dylib"] | ||
|
||
[dependencies] | ||
smallvec = { path = ".." } | ||
|
||
[profile.release] | ||
debug = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#![feature(core_intrinsics)] | ||
|
||
use smallvec::{SmallVec, Array}; | ||
use std::ptr; | ||
|
||
#[cfg(not(feature = "likely"))] | ||
macro_rules! likely { | ||
($e:expr) => { $e } | ||
} | ||
|
||
#[cfg(feature = "likely")] | ||
macro_rules! likely { | ||
($e:expr) => { #[allow(unused_unsafe)] { unsafe { std::intrinsics::likely($e) }} } | ||
} | ||
|
||
#[cfg(not(feature = "likely"))] | ||
macro_rules! unlikely { | ||
($e:expr) => { $e } | ||
} | ||
|
||
#[cfg(feature = "likely")] | ||
macro_rules! unlikely { | ||
($e:expr) => { #[allow(unused_unsafe)] { unsafe { std::intrinsics::unlikely($e) }} } | ||
} | ||
|
||
pub trait Test<A: Array> { | ||
fn push_light(&mut self, v: A::Item); | ||
} | ||
|
||
#[inline(never)] | ||
#[cold] | ||
fn push_light_cold<A: Array>(vec: &mut SmallVec<A>, v: A::Item) { | ||
if likely!(vec.spilled()) { | ||
let len = vec.len(); | ||
if likely!(len < vec.capacity()) { | ||
unsafe { | ||
ptr::write(vec.as_mut_ptr().offset(len as isize), v); | ||
vec.set_len(len + 1); | ||
} | ||
} else { | ||
vec.push_old(v) | ||
} | ||
} else { | ||
unsafe { | ||
std::intrinsics::assume(vec.capacity() == A::size()); | ||
} | ||
vec.push_old(v) | ||
} | ||
} | ||
|
||
impl<A: Array> Test<A> for SmallVec<A> { | ||
#[inline(always)] | ||
fn push_light(&mut self, v: A::Item) { | ||
let (free, len) = if !self.spilled() { | ||
let len = self.len(); | ||
(len < A::size(), len) | ||
} else { | ||
(false, 0) | ||
}; | ||
if likely!(free) { | ||
unsafe { | ||
ptr::write(self.as_mut_ptr().offset(len as isize), v); | ||
std::intrinsics::assume(!self.spilled()); | ||
self.set_len(self.len() + 1); | ||
} | ||
} else { | ||
push_light_cold(self, v); | ||
} | ||
} | ||
} | ||
|
||
// FIXME: Remove | ||
#[no_mangle] | ||
pub fn test1(a: &mut SmallVec<[u32; 8]>) { | ||
a.push(5); | ||
} | ||
|
||
#[no_mangle] | ||
pub fn test2(a: &mut SmallVec<[u32; 8]>) { | ||
a.push_light(5); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this should either not exist when the feature is not enabled, or delegate to
push
in the default implementation.