Skip to content
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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ union = []
default = ["std"]
specialization = []
may_dangle = []
likely = []
push_light = []

[lib]
name = "smallvec"
Expand Down
33 changes: 32 additions & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

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.

fn pop(&mut self) -> Option<T>;
fn remove(&mut self, p: usize) -> T;
fn insert(&mut self, n: usize, val: T);
Expand All @@ -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()
}
Expand Down Expand Up @@ -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) {}
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

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

It's to ensure things still compile

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

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

It's not meaningful to benchmark a feature if it's disabled

Copy link
Contributor

Choose a reason for hiding this comment

The 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()
}
Expand Down Expand Up @@ -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 _),
Expand Down Expand Up @@ -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) {
Expand Down
80 changes: 79 additions & 1 deletion lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]
#![cfg_attr(feature = "union", feature(untagged_unions))]
#![cfg_attr(feature = "likely", feature(stmt_expr_attributes))]
#![cfg_attr(feature = "push_light", feature(nll))]
#![cfg_attr(any(feature = "likely", feature = "push_light"), feature(core_intrinsics))]
#![cfg_attr(feature = "specialization", feature(specialization))]
#![cfg_attr(feature = "may_dangle", feature(dropck_eyepatch))]
#![deny(missing_docs)]
Expand Down Expand Up @@ -145,6 +148,26 @@ macro_rules! debug_unreachable {
}
}

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

/// Common operations implemented by both `Vec` and `SmallVec`.
///
/// This can be used to write generic code that works with both `Vec` and `SmallVec`.
Expand Down Expand Up @@ -611,7 +634,7 @@ impl<A: Array> SmallVec<A> {
pub fn push(&mut self, value: A::Item) {
unsafe {
let (_, &mut len, cap) = self.triple_mut();
if len == cap {
if unlikely!(len == cap) {
self.reserve(1);
}
let (ptr, len_ptr, _) = self.triple_mut();
Expand All @@ -620,6 +643,61 @@ impl<A: Array> SmallVec<A> {
}
}

/// Append an item to the vector. This is always inlined with a fast
/// path for when the vector doesn't need an heap allocation.
#[cfg(feature = "push_light")]
#[inline(always)]
pub fn push_light(&mut self, value: A::Item) {
unsafe {
if likely!(self.capacity < A::size()) {
let ptr = self.data.inline_mut().ptr_mut();
ptr::write(ptr.offset(self.capacity as isize), value);
self.capacity = self.capacity + 1;
} else {
self.push_light_cold(self.capacity, value);
}
}
}

// Slow path
#[cfg(feature = "push_light")]
#[inline(never)]
#[cold]
unsafe fn push_light_cold(&mut self, cap: usize, value: A::Item) {
std::intrinsics::assume(self.capacity == cap);
if likely!(cap != A::size()) {
debug_assert!(self.spilled());
let &mut (ptr, ref mut len_ptr) = self.data.heap_mut();

let len = *len_ptr;

if unlikely!(cap - len < 1) {
std::intrinsics::assume(self.capacity >= A::size());
if unlikely!(cap > (isize::max_value() >> 1) as usize) {
panic!("size overflow")
}
let new_cap = cap << 1;
self.grow(new_cap);
let &mut (ptr, ref mut len_ptr) = self.data.heap_mut();
*len_ptr = len + 1;
ptr::write(ptr.offset(len as isize), value);
} else {
*len_ptr = len + 1;
ptr::write(ptr.offset(len as isize), value);
}
} else {
debug_assert!(self.len() == A::size());
let new_cap = A::size().checked_add(1).
and_then(usize::checked_next_power_of_two).
unwrap_or(usize::max_value());
self.grow(new_cap);
debug_assert!(self.spilled());
let &mut (ptr, ref mut len_ptr) = self.data.heap_mut();
*len_ptr = A::size() + 1;
ptr::write(ptr.offset(A::size() as isize), value);
}
}

/// Remove an item from the end of the vector and return it, or None if empty.
#[inline]
pub fn pop(&mut self) -> Option<A::Item> {
Expand Down
14 changes: 14 additions & 0 deletions testing/Cargo.toml
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
81 changes: 81 additions & 0 deletions testing/src/lib.rs
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);
}