Skip to content
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internals = []
bytes = { version = "1", optional = true, default-features = false }
serde_core = { version = "1.0.221", optional = true, default-features = false }
malloc_size_of = { version = "0.1.1", optional = true, default-features = false }
add-syntax = "0.1.0"

[dev-dependencies]
serde_test = "1.0"
Expand Down
133 changes: 60 additions & 73 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#![allow(deprecated)]

use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use smallvec::{smallvec, SmallVec};
use std::hint::black_box;
use std::time::Duration;
use {
criterion::{
Bencher,
Criterion,
criterion_group,
criterion_main
},
smallvec::{
SmallVec,
smallvec
},
std::{
hint::black_box,
time::Duration
}
};

const VEC_SIZE: usize = 16;
const SPILLED_SIZE: usize = 100;
Expand All @@ -18,72 +30,51 @@ trait Vector<T>: for<'a> From<&'a [T]> + Extend<T> {
fn from_elems(val: &[T]) -> Self;
fn extend_from_slice(&mut self, other: &[T]);
fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut T) -> bool;
where F: FnMut(&mut T) -> bool;
}

impl<T: Copy> Vector<T> for Vec<T> {
fn new() -> Self {
Self::with_capacity(VEC_SIZE)
}
fn push(&mut self, val: T) {
self.push(val)
}
fn pop(&mut self) -> Option<T> {
self.pop()
}
fn remove(&mut self, p: usize) -> T {
self.remove(p)
}
fn insert(&mut self, n: usize, val: T) {
self.insert(n, val)
}
fn from_elem(val: T, n: usize) -> Self {
vec![val; n]
}
fn from_elems(val: &[T]) -> Self {
val.to_owned()
}
fn extend_from_slice(&mut self, other: &[T]) {
Vec::extend_from_slice(self, other)
}
fn new() -> Self { Self::with_capacity(VEC_SIZE) }

fn push(&mut self, val: T) { self.push(val) }

fn pop(&mut self) -> Option<T> { self.pop() }

fn remove(&mut self, p: usize) -> T { self.remove(p) }

fn insert(&mut self, n: usize, val: T) { self.insert(n, val) }

fn from_elem(val: T, n: usize) -> Self { vec![val; n] }

fn from_elems(val: &[T]) -> Self { val.to_owned() }

fn extend_from_slice(&mut self, other: &[T]) { Vec::extend_from_slice(self, other) }

fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut T) -> bool,
{
where F: FnMut(&mut T) -> bool {
self.retain_mut(f)
}
}

impl<T: Copy> Vector<T> for SmallVec<T, VEC_SIZE> {
fn new() -> Self {
Self::new()
}
fn push(&mut self, val: T) {
self.push(val)
}
fn pop(&mut self) -> Option<T> {
self.pop()
}
fn remove(&mut self, p: usize) -> T {
self.remove(p)
}
fn insert(&mut self, n: usize, val: T) {
self.insert(n, val)
}
fn from_elem(val: T, n: usize) -> Self {
smallvec![val; n]
}
fn from_elems(val: &[T]) -> Self {
SmallVec::from(val)
}
fn extend_from_slice(&mut self, other: &[T]) {
SmallVec::extend_from_slice(self, other)
}
fn new() -> Self { Self::new() }

fn push(&mut self, val: T) { self.push(val) }

fn pop(&mut self) -> Option<T> { self.pop() }

fn remove(&mut self, p: usize) -> T { self.remove(p) }

fn insert(&mut self, n: usize, val: T) { self.insert(n, val) }

fn from_elem(val: T, n: usize) -> Self { smallvec![val; n] }

fn from_elems(val: &[T]) -> Self { SmallVec::from(val) }

fn extend_from_slice(&mut self, other: &[T]) { SmallVec::extend_from_slice(self, other) }

fn retain_mut<F>(&mut self, f: F)
where
F: FnMut(&mut T) -> bool,
{
where F: FnMut(&mut T) -> bool {
self.retain_mut(f)
}
}
Expand All @@ -100,8 +91,8 @@ macro_rules! make_benches {
}
}

/* ---------- Bench generation (same list, just using the new macro)
* ---------- */
// ---------- Bench generation (same list, just using the new macro)
// ----------
make_benches! {
SmallVec<u64, VEC_SIZE> {
bench_push => gen_push(SPILLED_SIZE as _),
Expand Down Expand Up @@ -168,9 +159,7 @@ make_benches! {

fn gen_push<V: Vector<u64>>(n: u64, b: &mut Bencher) {
#[inline(never)]
fn push_noinline<V: Vector<u64>>(vec: &mut V, x: u64) {
vec.push(black_box(x));
}
fn push_noinline<V: Vector<u64>>(vec: &mut V, x: u64) { vec.push(black_box(x)); }

b.iter(|| {
let n = black_box(n);
Expand Down Expand Up @@ -216,15 +205,13 @@ fn gen_insert<V: Vector<u64>>(n: u64, b: &mut Bencher) {
insert_noinline(&mut vec, 0, x);
}
vec
},
}
);
}

fn gen_remove<V: Vector<u64>>(n: usize, b: &mut Bencher) {
#[inline(never)]
fn remove_noinline<V: Vector<u64>>(vec: &mut V, p: usize) -> u64 {
vec.remove(black_box(p))
}
fn remove_noinline<V: Vector<u64>>(vec: &mut V, p: usize) -> u64 { vec.remove(black_box(p)) }

b.iter_with_setup(
|| V::from_elem(0, black_box(n)),
Expand All @@ -233,7 +220,7 @@ fn gen_remove<V: Vector<u64>>(n: usize, b: &mut Bencher) {
black_box(remove_noinline(&mut vec, 0));
}
vec
},
}
);
}

Expand Down Expand Up @@ -309,7 +296,7 @@ fn gen_retain_mut_half<V: Vector<u64>>(n: usize, b: &mut Bencher) {
|mut vec| {
vec.retain_mut(|x| black_box(*x) % 2 == 0);
vec
},
}
);
}

Expand All @@ -319,7 +306,7 @@ fn gen_retain_mut_all<V: Vector<u64>>(n: usize, b: &mut Bencher) {
|mut vec| {
vec.retain_mut(|_| true);
vec
},
}
);
}

Expand All @@ -329,7 +316,7 @@ fn gen_retain_mut_none<V: Vector<u64>>(n: usize, b: &mut Bencher) {
|mut vec| {
vec.retain_mut(|_| false);
vec
},
}
);
}

Expand Down
18 changes: 16 additions & 2 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
wrap_comments = true
imports_granularity = "Preserve"
imports_granularity = "One"
group_imports = "One"
format_code_in_doc_comments = true
format_code_in_doc_comments = true
error_on_line_overflow = true
error_on_unformatted = true
blank_lines_lower_bound = 0
blank_lines_upper_bound = 1
float_literal_trailing_zero = "IfNoPostfix"
fn_single_line = true
imports_layout = "Vertical"
normalize_comments = true
reorder_impl_items = true
struct_lit_single_line = false
style_edition = "2024"
trailing_comma = "Never"
use_try_shorthand = true
where_single_line = true
64 changes: 64 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use {
super::SmallVec,
bytes::{
BufMut,
buf::UninitSlice
}
};

unsafe impl<const N: usize> BufMut for SmallVec<u8, N> {
fn remaining_mut(&self) -> usize {
// A vector can never have more than isize::MAX bytes
isize::MAX as usize - self.len()
}

unsafe fn advance_mut(&mut self, cnt: usize) {
let len = self.len();
let remaining = self.capacity() - len;

if remaining < cnt {
panic!("advance out of bounds: the len is {remaining} but advancing by {cnt}");
}

// Addition will not overflow since the sum is at most the capacity.
self.set_len(len + cnt);
}

fn chunk_mut(&mut self) -> &mut UninitSlice {
if self.capacity() == self.len() {
self.reserve(64); // Grow the smallvec
}

let cap = self.capacity();
let len = self.len();

let ptr = self.as_mut_ptr();
// SAFETY: Since `ptr` is valid for `cap` bytes, `ptr.add(len)` must be
// valid for `cap - len` bytes. The subtraction will not underflow since
// `len <= cap`.
unsafe { UninitSlice::from_raw_parts_mut(ptr.add(len), cap - len) }
}

// Specialize these methods so they can skip checking `remaining_mut`
// and `advance_mut`.
fn put<T: bytes::Buf>(&mut self, mut src: T)
where Self: Sized {
// In case the src isn't contiguous, reserve upfront.
self.reserve(src.remaining());

while src.has_remaining() {
let s = src.chunk();
let l = s.len();
self.extend_from_slice(s);
src.advance(l);
}
}

fn put_slice(&mut self, src: &[u8]) { self.extend_from_slice(src); }

fn put_bytes(&mut self, val: u8, cnt: usize) {
// If the addition overflows, then the `resize` will fail.
let new_len = self.len().saturating_add(cnt);
self.resize(new_len, val);
}
}
40 changes: 40 additions & 0 deletions src/comparisons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::SmallVec;

impl<T: PartialEq<U>, U, const N: usize, const M: usize> PartialEq<SmallVec<U, M>>
for SmallVec<T, N>
{
fn eq(&self, other: &SmallVec<U, M>) -> bool { self.as_slice().eq(other.as_slice()) }
}
impl<T, const N: usize> Eq for SmallVec<T, N> where T: Eq {}

impl<T: PartialEq<U>, U, const N: usize, const M: usize> PartialEq<[U; M]> for SmallVec<T, N> {
fn eq(&self, other: &[U; M]) -> bool { self[..] == other[..] }
}

impl<T: PartialEq<U>, U, const N: usize, const M: usize> PartialEq<&[U; M]> for SmallVec<T, N> {
fn eq(&self, other: &&[U; M]) -> bool { self[..] == other[..] }
}

impl<T: PartialEq<U>, U, const N: usize> PartialEq<[U]> for SmallVec<T, N> {
fn eq(&self, other: &[U]) -> bool { self[..] == other[..] }
}

impl<T: PartialEq<U>, U, const N: usize> PartialEq<&[U]> for SmallVec<T, N> {
fn eq(&self, other: &&[U]) -> bool { self[..] == other[..] }
}

impl<T: PartialEq<U>, U, const N: usize> PartialEq<&mut [U]> for SmallVec<T, N> {
fn eq(&self, other: &&mut [U]) -> bool { self[..] == other[..] }
}

impl<T: PartialOrd, const N: usize> PartialOrd for SmallVec<T, N> {
fn partial_cmp(&self, other: &SmallVec<T, N>) -> Option<core::cmp::Ordering> {
self.as_slice().partial_cmp(other.as_slice())
}
}

impl<T: Ord, const N: usize> Ord for SmallVec<T, N> {
fn cmp(&self, other: &SmallVec<T, N>) -> core::cmp::Ordering {
self.as_slice().cmp(other.as_slice())
}
}
Loading
Loading