Skip to content

Commit

Permalink
bench
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Nov 8, 2024
1 parent 429e4db commit fca4279
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 91 deletions.
149 changes: 75 additions & 74 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//! A simple benchmark for flatcontainer, adopted from `columnation`'s benchmark.
/*
use codspeed_bencher_compat::{benchmark_group, benchmark_main, Bencher};
use flatcontainer::impls::deduplicate::{CollapseSequence, ConsecutiveIndexPairs};
use flatcontainer::impls::index::IndexOptimized;
use flatcontainer::impls::tuple::{TupleABCRegion, TupleABRegion};
use flatcontainer::{
ColumnsRegion, FlatStack, MirrorRegion, OwnedRegion, Push, Region, RegionPreference,
ReserveItems, SliceRegion, StringRegion,
Clear, HeapSize, Index, OwnedRegion, Push, RegionPreference, ReserveItems, SliceRegion,
StringRegion,
};

fn empty_copy(bencher: &mut Bencher) {
Expand Down Expand Up @@ -64,50 +62,50 @@ fn str100_copy_region(bencher: &mut Bencher) {
fn string10_copy_region(bencher: &mut Bencher) {
_bench_copy_region::<SliceRegion<StringRegion>, _>(bencher, vec![format!("grawwwwrr!"); 1024]);
}
fn string10_copy_region_collapse(bencher: &mut Bencher) {
_bench_copy_region::<
SliceRegion<CollapseSequence<ConsecutiveIndexPairs<StringRegion>>, IndexOptimized>,
_,
>(bencher, vec![format!("grawwwwrr!"); 1024]);
}
// fn string10_copy_region_collapse(bencher: &mut Bencher) {
// _bench_copy_region::<
// SliceRegion<CollapseSequence<ConsecutiveIndexPairs<StringRegion>>, IndexOptimized>,
// _,
// >(bencher, vec![format!("grawwwwrr!"); 1024]);
// }
fn string20_copy_region(bencher: &mut Bencher) {
_bench_copy_region::<SliceRegion<StringRegion>, _>(
bencher,
vec![format!("grawwwwrr!!!!!!!!!!!"); 512],
);
}
fn vec_u_s_copy_region(bencher: &mut Bencher) {
_bench_copy_region::<SliceRegion<SliceRegion<TupleABRegion<MirrorRegion<_>, StringRegion>>>, _>(
_bench_copy_region::<SliceRegion<SliceRegion<TupleABRegion<Vec<u64>, StringRegion>>>, _>(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 32]; 32],
);
}
fn vec_u_vn_s_copy_region(bencher: &mut Bencher) {
_bench_copy_region::<
SliceRegion<SliceRegion<TupleABCRegion<MirrorRegion<_>, OwnedRegion<_>, StringRegion>>>,
_,
>(
bencher,
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}
fn vec_u_vn_s_copy_region_column(bencher: &mut Bencher) {
_bench_copy_region::<
SliceRegion<
ColumnsRegion<
TupleABCRegion<
MirrorRegion<_>,
CollapseSequence<OwnedRegion<_>>,
CollapseSequence<StringRegion>,
>,
>,
>,
SliceRegion<SliceRegion<TupleABCRegion<Vec<u64>, OwnedRegion<()>, StringRegion>>>,
_,
>(
bencher,
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}
// fn vec_u_vn_s_copy_region_column(bencher: &mut Bencher) {
// _bench_copy_region::<
// SliceRegion<
// ColumnsRegion<
// TupleABCRegion<
// MirrorRegion<_>,
// CollapseSequence<OwnedRegion<_>>,
// CollapseSequence<StringRegion>,
// >,
// >,
// >,
// _,
// >(
// bencher,
// vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
// );
// }

fn empty_clone(bencher: &mut Bencher) {
_bench_clone(bencher, vec![(); 1024]);
Expand Down Expand Up @@ -276,37 +274,37 @@ fn string20_copy_flat_region(bencher: &mut Bencher) {
);
}
fn vec_u_s_copy_flat_region(bencher: &mut Bencher) {
_bench_copy_flat::<SliceRegion<SliceRegion<TupleABRegion<MirrorRegion<_>, StringRegion>>>, _>(
_bench_copy_flat::<SliceRegion<SliceRegion<TupleABRegion<Vec<u64>, StringRegion>>>, _>(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 32]; 32],
);
}
fn vec_u_vn_s_copy_flat_region(bencher: &mut Bencher) {
_bench_copy_flat::<
SliceRegion<SliceRegion<TupleABCRegion<MirrorRegion<_>, OwnedRegion<_>, StringRegion>>>,
_,
>(
bencher,
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}
fn vec_u_vn_s_copy_flat_region_column(bencher: &mut Bencher) {
_bench_copy_flat::<
SliceRegion<
ColumnsRegion<
TupleABCRegion<
MirrorRegion<_>,
CollapseSequence<OwnedRegion<_>>,
CollapseSequence<StringRegion>,
>,
>,
>,
SliceRegion<SliceRegion<TupleABCRegion<Vec<u64>, OwnedRegion<()>, StringRegion>>>,
_,
>(
bencher,
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}
// fn vec_u_vn_s_copy_flat_region_column(bencher: &mut Bencher) {
// _bench_copy_flat::<
// SliceRegion<
// ColumnsRegion<
// TupleABCRegion<
// MirrorRegion<_>,
// CollapseSequence<OwnedRegion<_>>,
// CollapseSequence<StringRegion>,
// >,
// >,
// >,
// _,
// >(
// bencher,
// vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
// );
// }

fn set_bytes(target: &mut u64, bytes: usize) {
if std::env::var("BYTES").is_ok() {
Expand All @@ -316,15 +314,15 @@ fn set_bytes(target: &mut u64, bytes: usize) {

fn _bench_copy<T: RegionPreference + Eq>(bencher: &mut Bencher, record: T)
where
for<'a> <T as RegionPreference>::Region: Push<&'a T>,
for<'a> <T as RegionPreference>::Region: Default + HeapSize + Clear + Push<&'a T>,
{
// prepare encoded data for bencher.bytes
let mut arena = FlatStack::default_impl::<T>();
let mut arena = T::Region::default();

bencher.iter(|| {
arena.clear();
for _ in 0..1024 {
arena.copy(&record);
arena.push(&record);
}
});
let (mut siz, mut cap) = (0, 0);
Expand All @@ -335,17 +333,17 @@ where
set_bytes(&mut bencher.bytes, siz);
}

fn _bench_copy_region<R: Region, T>(bencher: &mut Bencher, record: T)
fn _bench_copy_region<R, T>(bencher: &mut Bencher, record: T)
where
for<'a> R: Push<&'a T>,
for<'a> R: Default + HeapSize + Clear + Push<&'a T>,
{
// prepare encoded data for bencher.bytes
let mut arena = FlatStack::<R>::default();
let mut arena = R::default();

bencher.iter(|| {
arena.clear();
for _ in 0..1024 {
arena.copy(&record);
arena.push(&record);
}
});
let (mut siz, mut cap) = (0, 0);
Expand All @@ -370,14 +368,14 @@ fn _bench_clone<T: RegionPreference + Eq + Clone>(bencher: &mut Bencher, record:

fn _bench_realloc<T: RegionPreference + Eq>(bencher: &mut Bencher, record: T)
where
for<'a> <T as RegionPreference>::Region: Push<&'a T>,
for<'a> <T as RegionPreference>::Region: Default + HeapSize + Push<&'a T>,
{
let mut arena = FlatStack::default_impl::<T>();
let mut arena = T::Region::default();
bencher.iter(|| {
// prepare encoded data for bencher.bytes
arena = FlatStack::default_impl::<T>();
arena = T::Region::default();
for _ in 0..1024 {
arena.copy(&record);
arena.push(&record);
}
});
let (mut siz, mut cap) = (0, 0);
Expand All @@ -389,15 +387,15 @@ where

fn _bench_prealloc<T: RegionPreference + Eq>(bencher: &mut Bencher, record: T)
where
for<'a> <T as RegionPreference>::Region: ReserveItems<&'a T> + Push<&'a T>,
for<'a> <T as RegionPreference>::Region: Default + HeapSize + ReserveItems<&'a T> + Push<&'a T>,
{
let mut arena = FlatStack::default_impl::<T>();
let mut arena = T::Region::default();
bencher.iter(|| {
arena = FlatStack::default_impl::<T>();
arena = T::Region::default();
// prepare encoded data for bencher.bytes
arena.reserve_items(std::iter::repeat(&record).take(1024));
for _ in 0..1024 {
arena.copy(&record);
arena.push(&record);
}
});
let (mut siz, mut cap) = (0, 0);
Expand All @@ -411,22 +409,26 @@ where
fn _bench_copy_flat_preference<T>(bencher: &mut Bencher, record: T)
where
T: RegionPreference,
for<'a> <T as RegionPreference>::Region:
Push<&'a T> + Push<<<T as RegionPreference>::Region as Region>::ReadItem<'a>> + Clone,
for<'a> <T as RegionPreference>::Region: Default
+ Index
+ HeapSize
+ Push<&'a T>
+ Push<<<T as RegionPreference>::Region as Index>::ReadItem<'a>>
+ Clone,
{
_bench_copy_flat::<T::Region, T>(bencher, record)
}

fn _bench_copy_flat<R, T>(bencher: &mut Bencher, record: T)
where
for<'a> R: Region + Push<&'a T> + Push<<R as Region>::ReadItem<'a>> + Clone,
for<'a> R: Default + Index + HeapSize + Push<&'a T> + Push<<R as Index>::ReadItem<'a>> + Clone,
{
// prepare encoded data for bencher.bytes
let mut arena = FlatStack::<R>::default();
let mut arena = R::default();
for _ in 0..1024 {
arena.copy(&record);
arena.push(&record);
}
let mut target = FlatStack::<R>::default();
let mut target = R::default();

bencher.iter(|| {
target.clone_from(&arena);
Expand Down Expand Up @@ -486,7 +488,7 @@ benchmark_group!(
str10_copy_region,
string10_copy_flat_region,
string10_copy_region,
string10_copy_region_collapse,
// string10_copy_region_collapse,
string20_copy_flat_region,
string20_copy_region,
u32x2_copy_flat_region,
Expand All @@ -498,9 +500,9 @@ benchmark_group!(
vec_u_s_copy_flat_region,
vec_u_s_copy_region,
vec_u_vn_s_copy_flat_region,
vec_u_vn_s_copy_flat_region_column,
// vec_u_vn_s_copy_flat_region_column,
vec_u_vn_s_copy_region,
vec_u_vn_s_copy_region_column,
// vec_u_vn_s_copy_region_column,
);
benchmark_group!(
alloc,
Expand All @@ -524,4 +526,3 @@ benchmark_group!(
vec_u_vn_s_realloc,
);
benchmark_main!(clone, copy, copy_flat, copy_region, alloc);
*/
1 change: 0 additions & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// pub mod deduplicate;
// pub mod huffman_container;
// pub mod index;
// pub mod mirror;
pub mod option;
pub mod result;
pub mod slice;
Expand Down
4 changes: 1 addition & 3 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,7 @@ where
{
#[inline]
fn push(&mut self, items: &'a [T]) {
for item in items.iter() {
self.inner.push(item);
}
self.inner.push_extend(items);
self.bounds
.push(self.inner.len().try_into().expect("must fit"));
}
Expand Down
14 changes: 6 additions & 8 deletions src/impls/slice_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::marker::PhantomData;
use serde::{Deserialize, Serialize};

// use crate::impls::storage::{PushStorage, Storage};
use crate::{Clear, HeapSize, Index, IndexAs, Len, Push, PushIter, Region, Reserve, ReserveItems};
use crate::{
Clear, HeapSize, Index, IndexAs, Len, Push, PushIter, PushSlice, Region, Reserve, ReserveItems,
};

type Idx = u64;

Expand Down Expand Up @@ -174,9 +176,7 @@ where
{
#[inline]
fn push(&mut self, items: [T; N]) {
for item in items {
self.slices.push(item);
}
self.slices.push_extend(items);
self.bounds
.push(self.slices.len().try_into().expect("must fit"));
}
Expand Down Expand Up @@ -220,14 +220,12 @@ where
impl<T, S, B> Push<&[T]> for OwnedRegion<T, S, B>
where
T: Clone,
S: for<'a> Push<&'a T> + Len,
S: PushSlice<T> + Len,
B: Push<Idx>,
{
#[inline]
fn push(&mut self, items: &[T]) {
for item in items {
self.slices.push(item);
}
self.slices.push_slice(items);
self.bounds
.push(self.slices.len().try_into().expect("must fit"));
}
Expand Down
22 changes: 17 additions & 5 deletions src/impls/vec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Definitions to use `Vec<T>` as a region.
use crate::{Clear, HeapSize, Index, IndexAs, Len, Push, Region, Reserve, ReserveItems};
use crate::{Clear, HeapSize, Index, IndexAs, Len, Push, PushSlice, Region, Reserve, ReserveItems};

impl<T> Region for Vec<T> {
#[inline(always)]
Expand Down Expand Up @@ -49,18 +49,30 @@ impl<T: Clone> Push<T> for Vec<T> {
fn push(&mut self, item: T) {
self.push(item);
}

#[inline(always)]
fn push_extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
Extend::extend(self, iter);
}
}

impl<T: Clone> PushSlice<T> for Vec<T> {
#[inline(always)]
fn push_slice(&mut self, slice: &[T]) {
self.extend_from_slice(slice);
}
}

impl<T: Clone> Push<&T> for Vec<T> {
impl<'a, T: Clone> Push<&'a T> for Vec<T> {
#[inline(always)]
fn push(&mut self, item: &T) {
fn push(&mut self, item: &'a T) {
self.push(item.clone());
}
}

impl<T: Clone> Push<&&T> for Vec<T> {
impl<'a, 'b, T: Clone> Push<&'a &'b T> for Vec<T> {
#[inline(always)]
fn push(&mut self, item: &&T) {
fn push(&mut self, item: &'a &'b T) {
self.push((*item).clone());
}
}
Expand Down
Loading

0 comments on commit fca4279

Please sign in to comment.