Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Feb 2, 2024
1 parent 23f1802 commit 030607d
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 87 deletions.
44 changes: 24 additions & 20 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate test;

use test::Bencher;
use flatcontainer::Containerized;
use flatcontainer::{Containerized, Region, CopyOnto};

#[bench] fn empty_copy(bencher: &mut Bencher) { _bench_copy(bencher, vec![(); 1024]); }
#[bench] fn u64_copy(bencher: &mut Bencher) { _bench_copy(bencher, vec![0u64; 1024]); }
Expand All @@ -14,14 +14,14 @@ use flatcontainer::Containerized;
#[bench] fn vec_u_s_copy(bencher: &mut Bencher) { _bench_copy(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }
#[bench] fn vec_u_vn_s_copy(bencher: &mut Bencher) { _bench_copy(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); }

#[bench] fn empty_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![(); 1024]); }
#[bench] fn u64_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![0u64; 1024]); }
#[bench] fn u32x2_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![(0u32,0u32); 1024]); }
#[bench] fn u8_u64_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![(0u8, 0u64); 512]); }
#[bench] fn string10_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![format!("grawwwwrr!"); 1024]); }
#[bench] fn string20_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); }
#[bench] fn vec_u_s_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }
#[bench] fn vec_u_vn_s_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); }
//#[bench] fn empty_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![(); 1024]); }
//#[bench] fn u64_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![0u64; 1024]); }
//#[bench] fn u32x2_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![(0u32,0u32); 1024]); }
//#[bench] fn u8_u64_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![(0u8, 0u64); 512]); }
//#[bench] fn string10_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![format!("grawwwwrr!"); 1024]); }
//#[bench] fn string20_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]); }
//#[bench] fn vec_u_s_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }
//#[bench] fn vec_u_vn_s_clone(bencher: &mut Bencher) { _bench_clone(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); }

#[bench] fn empty_realloc(bencher: &mut Bencher) { _bench_realloc(bencher, vec![(); 1024]); }
#[bench] fn u64_realloc(bencher: &mut Bencher) { _bench_realloc(bencher, vec![0u64; 1024]); }
Expand All @@ -41,7 +41,11 @@ use flatcontainer::Containerized;
#[bench] fn vec_u_s_prealloc(bencher: &mut Bencher) { _bench_prealloc(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]); }
#[bench] fn vec_u_vn_s_prealloc(bencher: &mut Bencher) { _bench_prealloc(bencher, vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32]); }

fn _bench_copy<T: Containerized+Eq>(bencher: &mut Bencher, record: T) {
fn _bench_copy<T: Containerized+Eq + >(bencher: &mut Bencher, record: T)
where

for<'a> &'a T: CopyOnto<<T as Containerized>::Container>
{

// prepare encoded data for bencher.bytes
let mut arena = T::Container::default();
Expand Down Expand Up @@ -70,22 +74,22 @@ fn _bench_clone<T: Containerized+Eq+Clone>(bencher: &mut Bencher, record: T) {
fn _bench_realloc<T: Containerized+Eq>(bencher: &mut Bencher, record: T) {

bencher.iter(|| {
// prepare encoded data for bencher.bytes
let mut arena = ColumnStack::<T>::default();
for _ in 0 .. 1024 {
arena.copy(&record);
}
// // prepare encoded data for bencher.bytes
// let mut arena = ColumnStack::<T>::default();
// for _ in 0 .. 1024 {
// arena.copy(&record);
// }
});
}

fn _bench_prealloc<T: Containerized+Eq>(bencher: &mut Bencher, record: T) {

bencher.iter(|| {
// prepare encoded data for bencher.bytes
let mut arena = ColumnStack::<T>::default();
arena.reserve_items(std::iter::repeat(&record).take(1024));
for _ in 0 .. 1024 {
arena.copy(&record);
}
// let mut arena = ColumnStack::<T>::default();
// arena.reserve_items(std::iter::repeat(&record).take(1024));
// for _ in 0 .. 1024 {
// arena.copy(&record);
// }
});
}
177 changes: 110 additions & 67 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ where
C: Region,
&'a T: CopyOnto<C>,
{
#[inline]
fn copy_onto(self, target: &mut SliceContainer<C>) -> usize {
target
.slices
Expand All @@ -197,6 +198,7 @@ where
C: Region,
&'a [T]: CopyOnto<SliceContainer<C>>,
{
#[inline]
fn copy_onto(self, target: &mut SliceContainer<C>) -> usize {
self.as_slice().copy_onto(target)
}
Expand All @@ -209,10 +211,29 @@ where
}
}

impl<C, T> CopyOnto<SliceContainer<C>> for Vec<T>
where
C: Region,
for<'a> &'a [T]: CopyOnto<SliceContainer<C>>,
{
#[inline]
fn copy_onto(self, target: &mut SliceContainer<C>) -> usize {
self.as_slice().copy_onto(target)
}

fn reserve_items< I>(target: &mut SliceContainer<C>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
//CopyOnto::reserve_items(target, items.map(Deref::deref))
}
}

impl<'a, C: Region + 'a> CopyOnto<SliceContainer<C>> for &'a (&'a C, &'a [C::Index])
where
C::ReadItem<'a>: CopyOnto<C>,
{
#[inline]
fn copy_onto(self, target: &mut SliceContainer<C>) -> <SliceContainer<C> as Region>::Index {
let (container, indexes) = self;
target.slices.extend(
Expand Down Expand Up @@ -272,6 +293,7 @@ impl Region for StringContainer {
self.inner.reserve_regions(regions.map(|r| &r.inner));
}

#[inline]
fn clear(&mut self) {
self.inner.clear();
}
Expand All @@ -282,6 +304,7 @@ impl Containerized for String {
}

impl<'a> CopyOnto<StringContainer> for &'a String {
#[inline]
fn copy_onto(self, target: &mut StringContainer) -> usize {
self.as_str().copy_onto(target)
}
Expand All @@ -295,6 +318,7 @@ impl<'a> CopyOnto<StringContainer> for &'a String {
}

impl<'a> CopyOnto<StringContainer> for &'a str {
#[inline]
fn copy_onto(self, target: &mut StringContainer) -> usize {
self.as_bytes().copy_onto(&mut target.inner)
}
Expand Down Expand Up @@ -356,10 +380,12 @@ mod implementations {
}

impl CopyOnto<MirrorContainer<Self>> for $index_type {
#[inline(always)]
fn copy_onto(self, _target: &mut MirrorContainer<Self>) -> $index_type {
self
}

#[inline(always)]
fn reserve_items<I>(_target: &mut MirrorContainer<Self>, _items: I)
where
I: Iterator<Item = Self> + Clone,
Expand All @@ -368,10 +394,12 @@ mod implementations {
}

impl<'a> CopyOnto<MirrorContainer<$index_type>> for &'a $index_type {
#[inline(always)]
fn copy_onto(self, _target: &mut MirrorContainer<$index_type>) -> $index_type {
*self
}

#[inline(always)]
fn reserve_items<I>(_target: &mut MirrorContainer<$index_type>, _items: I)
where
I: Iterator<Item = Self> + Clone,
Expand Down Expand Up @@ -468,6 +496,7 @@ mod result {
T: CopyOnto<TC>,
E: CopyOnto<EC>,
{
#[inline]
fn copy_onto(
self,
target: &mut ResultContainer<TC, EC>,
Expand All @@ -491,6 +520,7 @@ mod result {
&'a T: CopyOnto<TC>,
&'a E: CopyOnto<EC>,
{
#[inline]
fn copy_onto(
self,
target: &mut ResultContainer<TC, EC>,
Expand Down Expand Up @@ -518,45 +548,22 @@ pub mod tuple {

use paste::paste;

macro_rules! tuple_columnation_inner1 {
([$name0:tt $($name:tt)*], [($index0:tt) $(($index:tt))*], $self:tt, $items:tt) => ( paste! {
$self.[<region $name0>].reserve_items($items.clone().map(|item| {
&item.$index0
}));
tuple_columnation_inner1!([$($name)*], [$(($index))*], $self, $items);
}
);
([], [$(($index:tt))*], $self:ident, $items:ident) => ( );
}

// This macro is copied from the above macro, but could probably be simpler as it does not need indexes.
macro_rules! tuple_columnation_inner2 {
([$name0:tt $($name:tt)*], [($index0:tt) $(($index:tt))*], $self:tt, $regions:tt) => ( paste! {
$self.[<region $name0>].reserve_regions($regions.clone().map(|region| {
&region.[<region $name0>]
}));
tuple_columnation_inner2!([$($name)*], [$(($index))*], $self, $regions);
}
);
([], [$(($index:tt))*], $self:ident, $regions:ident) => ( );
}

/// The macro creates the region implementation for tuples
macro_rules! tuple_flatcontainer {
( $($name:ident)+) => (
paste! {
impl<$($name: Containerized),*> Containerized for ($($name,)*) {
type Container = [<Tuple $($name)* Container >]<$($name::Container,)*>;
type Container = [<Tuple $($name)* Region >]<$($name::Container,)*>;
}

#[allow(non_snake_case)]
#[derive(Default)]
pub struct [<Tuple $($name)* Container >]<$($name),*> {
pub struct [<Tuple $($name)* Region >]<$($name),*> {
$([<container $name>]: $name),*
}

#[allow(non_snake_case)]
impl<$($name: Container),*> Container for [<Tuple $($name)* Container>]<$($name),*> {
impl<$($name: Region),*> Region for [<Tuple $($name)* Region>]<$($name),*> {
type ReadItem<'a> = ($($name::ReadItem<'a>,)*) where Self: 'a;
type Index = ($($name::Index,)*);
#[inline] fn index(&self, index: Self::Index) -> Self::ReadItem<'_> {
Expand All @@ -565,79 +572,115 @@ pub mod tuple {
$(self.[<container $name>].index($name),)*
)
}
#[allow(unreachable_code)]
#[inline(always)]
fn len(&self) -> usize {
$( return self.[<container $name>].len();)*
}
#[inline(always)]
fn is_empty(&self) -> bool {
[$(self.[<container $name>].is_empty() ,)*].into_iter().all(std::convert::identity)
std::iter::empty()$(.chain(std::iter::once(self.[<container $name>].is_empty())))*.all(std::convert::identity)
}

#[inline(always)]
fn reserve_regions<'a, It>(&mut self, regions: It)
where
Self: 'a,
It: Iterator<Item = &'a Self> + Clone {
$(self.[<container $name>].reserve_regions(regions.clone().map(|r| &r.[<container $name>]));)*
}

#[inline(always)]
fn clear(&mut self) {
$(self.[<container $name>].clear();)*
}
}

/*
#[allow(non_snake_case)]
impl<$($name, [<$name C>]: Container ),*>
CopyOnto<[<Tuple $($name)* Container>]<$([<$name C>]),*>>
impl<$($name, [<$name C>]: Region ),*>
CopyOnto<[<Tuple $($name)* Region>]<$([<$name C>]),*>>
for ($($name,)*)
where
$($name: CopyOnto<[<$name C>]>),*
{
fn copy_onto(self, target: &mut [<Tuple $($name)* Container>]<$([<$name C>]),*>)
-> <[<Tuple $($name)* Container>]<$([<$name C>]),*> as Container>::Index {
fn copy_onto(self, target: &mut [<Tuple $($name)* Region>]<$([<$name C>]),*>)
-> <[<Tuple $($name)* Region>]<$([<$name C>]),*> as Region>::Index {
let ($($name,)*) = self;
($($name.copy_onto(&mut target.[<container $name>]),)*)
}
}
fn reserve_items<I>(target: &mut [<Tuple $($name)* Region>]<$([<$name C>]),*>, items: I)
where
I: Iterator<Item = Self> + Clone {
}
}*/

#[allow(non_snake_case)]
impl<'a, $($name, [<$name C>]: Container ),*>
CopyOnto<[<Tuple $($name)* Container>]<$([<$name C>]),*>>
#[allow(non_camel_case_types)]
impl<'a, $($name, [<$name _C>]: Region ),*>
CopyOnto<[<Tuple $($name)* Region>]<$([<$name _C>]),*>>
for &'a ($($name,)*)
where
$(&'a $name: CopyOnto<[<$name C>]>),*
$(&'a $name: CopyOnto<[<$name _C>]>),*
{
fn copy_onto(self, target: &mut [<Tuple $($name)* Container>]<$([<$name C>]),*>)
-> <[<Tuple $($name)* Container>]<$([<$name C>]),*> as Container>::Index {
#[inline(always)]
fn copy_onto(self, target: &mut [<Tuple $($name)* Region>]<$([<$name _C>]),*>)
-> <[<Tuple $($name)* Region>]<$([<$name _C>]),*> as Region>::Index {
let ($($name,)*) = self;
($($name.copy_onto(&mut target.[<container $name>]),)*)
}
fn reserve_items<It>(target: &mut [<Tuple $($name)* Region>]<$([<$name _C>]),*>, items: It)
where
It: Iterator<Item = Self> + Clone {
tuple_flatcontainer!(reserve_items target items $($name)* @ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31);
}
}
}
);
(reserve_items $target:ident $items:ident $name0:ident $($name:ident)* @ $num0:tt $($num:tt)*) => {
paste! {
CopyOnto::reserve_items(&mut $target.[<container $name0>], $items.clone().map(|i| {
&i.$num0
}));
tuple_flatcontainer!(reserve_items $target $items $($name)* @ $($num)*);
}
};
(reserve_items $target:ident $items:ident @ $($num:tt)*) => {};
}

tuple_flatcontainer!(A);
tuple_flatcontainer!(A B);
// tuple_flatcontainer!(A B C);
// tuple_flatcontainer!(A B C D);
// tuple_flatcontainer!(A B C D E);
// tuple_flatcontainer!(A B C D E F);
// tuple_flatcontainer!(A B C D E F G);
// tuple_flatcontainer!(A B C D E F G H);
// tuple_flatcontainer!(A B C D E F G H I);
// tuple_flatcontainer!(A B C D E F G H I J);
// tuple_flatcontainer!(A B C D E F G H I J K);
// tuple_flatcontainer!(A B C D E F G H I J K L);
// tuple_flatcontainer!(A B C D E F G H I J K L M);
// tuple_flatcontainer!(A B C D E F G H I J K L M N);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE);
// tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF);
tuple_flatcontainer!(A B C);
tuple_flatcontainer!(A B C D);
tuple_flatcontainer!(A B C D E);
tuple_flatcontainer!(A B C D E F);
tuple_flatcontainer!(A B C D E F G);
tuple_flatcontainer!(A B C D E F G H);
tuple_flatcontainer!(A B C D E F G H I);
tuple_flatcontainer!(A B C D E F G H I J);
tuple_flatcontainer!(A B C D E F G H I J K);
tuple_flatcontainer!(A B C D E F G H I J K L);
tuple_flatcontainer!(A B C D E F G H I J K L M);
tuple_flatcontainer!(A B C D E F G H I J K L M N);
tuple_flatcontainer!(A B C D E F G H I J K L M N O);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE);
tuple_flatcontainer!(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF);
}

#[cfg(test)]
Expand Down

0 comments on commit 030607d

Please sign in to comment.