Skip to content

Commit

Permalink
More improvements
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 c6e95b0 commit a13cb45
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 29 deletions.
58 changes: 37 additions & 21 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

extern crate test;

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

#[bench]
Expand Down Expand Up @@ -33,13 +33,16 @@ fn string20_copy(bencher: &mut Bencher) {
}
#[bench]
fn vec_u_s_copy(bencher: &mut Bencher) {
_bench_copy(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]);
_bench_copy(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 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],
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}

Expand Down Expand Up @@ -69,13 +72,16 @@ fn string20_clone(bencher: &mut Bencher) {
}
#[bench]
fn vec_u_s_clone(bencher: &mut Bencher) {
_bench_clone(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]);
_bench_clone(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 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],
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}

Expand Down Expand Up @@ -105,13 +111,16 @@ fn string20_realloc(bencher: &mut Bencher) {
}
#[bench]
fn vec_u_s_realloc(bencher: &mut Bencher) {
_bench_realloc(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]);
_bench_realloc(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 32]; 32],
);
}
#[bench]
fn vec_u_vn_s_realloc(bencher: &mut Bencher) {
_bench_realloc(
bencher,
vec![vec![(0u64, vec![(); 1 << 40], format!("grawwwwrr!")); 32]; 32],
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}

Expand Down Expand Up @@ -141,13 +150,16 @@ fn string20_prealloc(bencher: &mut Bencher) {
}
#[bench]
fn vec_u_s_prealloc(bencher: &mut Bencher) {
_bench_prealloc(bencher, vec![vec![(0u64, format!("grawwwwrr!")); 32]; 32]);
_bench_prealloc(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 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],
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}

Expand Down Expand Up @@ -178,23 +190,27 @@ fn _bench_clone<T: Containerized + Eq + Clone>(bencher: &mut Bencher, record: T)
});
}

fn _bench_realloc<T: Containerized + Eq>(bencher: &mut Bencher, record: T) {
fn _bench_realloc<T: Containerized + Eq>(bencher: &mut Bencher, record: T)
where for<'a> &'a T: CopyOnto<<T as Containerized>::Region>
{
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 = FlatStack::default_impl::<T>();
for _ in 0..1024 {
arena.copy(&record);
}
});
}

fn _bench_prealloc<T: Containerized + Eq>(bencher: &mut Bencher, record: T) {
fn _bench_prealloc<T: Containerized + Eq>(bencher: &mut Bencher, record: T)
where for<'a> &'a T: ReserveItems<<T as Containerized>::Region> + CopyOnto<<T as Containerized>::Region>
{
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 = FlatStack::default_impl::<T>();
arena.reserve_items(std::iter::repeat(&record).take(1024));
for _ in 0..1024 {
arena.copy(&record);
}
});
}
4 changes: 2 additions & 2 deletions src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<T: Copy> Default for CopyRegion<T> {
}
}

impl<'a, T> CopyOnto<CopyRegion<T>> for &'a [T]
impl<T> CopyOnto<CopyRegion<T>> for &[T]
where
T: Copy,
{
Expand All @@ -55,7 +55,7 @@ where
}
}

impl<'a, T: Copy> ReserveItems<CopyRegion<T>> for &'a [T] {
impl<T: Copy> ReserveItems<CopyRegion<T>> for &[T] {
fn reserve_items<I>(target: &mut CopyRegion<T>, items: I)
where
I: Iterator<Item = Self> + Clone,
Expand Down
24 changes: 19 additions & 5 deletions src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct StringRegion {
}

impl Region for StringRegion {
type ReadItem<'a>= &'a str where Self: 'a ;
type ReadItem<'a> = &'a str where Self: 'a ;
type Index = <SliceRegion<CopyRegion<u8>> as Region>::Index;

#[inline]
Expand All @@ -34,7 +34,7 @@ impl Containerized for String {
type Region = StringRegion;
}

impl<'a> CopyOnto<StringRegion> for &'a String {
impl CopyOnto<StringRegion> for &String {
#[inline]
fn copy_onto(self, target: &mut StringRegion) -> <StringRegion as Region>::Index {
self.as_str().copy_onto(target)
Expand All @@ -48,7 +48,7 @@ impl<'a> CopyOnto<StringRegion> for &'a String {
}
}

impl<'a> ReserveItems<StringRegion> for &'a String {
impl ReserveItems<StringRegion> for &String {
fn reserve_items<I>(target: &mut StringRegion, items: I)
where
I: Iterator<Item = Self> + Clone,
Expand All @@ -57,7 +57,7 @@ impl<'a> ReserveItems<StringRegion> for &'a String {
}
}

impl<'a> CopyOnto<StringRegion> for &'a str {
impl CopyOnto<StringRegion> for &str {
#[inline]
fn copy_onto(self, target: &mut StringRegion) -> <StringRegion as Region>::Index {
self.as_bytes().copy_onto(&mut target.inner)
Expand All @@ -71,7 +71,21 @@ impl<'a> CopyOnto<StringRegion> for &'a str {
}
}

impl<'a> ReserveItems<StringRegion> for &'a str {
impl CopyOnto<StringRegion> for &&str {
#[inline]
fn copy_onto(self, target: &mut StringRegion) -> <StringRegion as Region>::Index {
self.as_bytes().copy_onto(&mut target.inner)
}

fn reserve_items<I>(target: &mut StringRegion, items: I)
where
I: Iterator<Item = Self> + Clone,
{
CopyOnto::reserve_items(&mut target.inner, items.map(|s| s.as_bytes()))
}
}

impl ReserveItems<StringRegion> for &str {
fn reserve_items<I>(target: &mut StringRegion, items: I)
where
I: Iterator<Item = Self> + Clone,
Expand Down
91 changes: 90 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ impl<R: Region> FlatStack<R> {
self.indices.clear();
self.region.clear();
}

pub fn reserve_items<T>(&mut self, items: impl Iterator<Item=T> + Clone)
where T: ReserveItems<R>,
{
ReserveItems::reserve_items(&mut self.region, items);
}

pub fn reserve_regions<'a>(&mut self, regions: impl Iterator<Item=&'a R> + Clone)
where R: 'a
{
self.region.reserve_regions(regions)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -300,5 +312,82 @@ mod tests {
}

#[test]
fn all_types() {}
fn all_types() {
fn test_copy<T, C: Region>(t: T)
where
T: CopyOnto<C>,
{
let mut c = FlatStack::default();
c.copy(t);
}

test_copy::<_, StringRegion>(&"a".to_string());
test_copy::<_, StringRegion>("a");

test_copy::<_, MirrorRegion<_>>(());
test_copy::<_, MirrorRegion<_>>(&());
test_copy::<_, MirrorRegion<_>>(true);
test_copy::<_, MirrorRegion<_>>(&true);
test_copy::<_, MirrorRegion<_>>(' ');
test_copy::<_, MirrorRegion<_>>(&' ');
test_copy::<_, MirrorRegion<_>>(0u8);
test_copy::<_, MirrorRegion<_>>(&0u8);
test_copy::<_, MirrorRegion<_>>(0u16);
test_copy::<_, MirrorRegion<_>>(&0u16);
test_copy::<_, MirrorRegion<_>>(0u32);
test_copy::<_, MirrorRegion<_>>(&0u32);
test_copy::<_, MirrorRegion<_>>(0u64);
test_copy::<_, MirrorRegion<_>>(&0u64);
test_copy::<_, MirrorRegion<_>>(0u128);
test_copy::<_, MirrorRegion<_>>(&0u128);
test_copy::<_, MirrorRegion<_>>(0usize);
test_copy::<_, MirrorRegion<_>>(&0usize);
test_copy::<_, MirrorRegion<_>>(0i8);
test_copy::<_, MirrorRegion<_>>(&0i8);
test_copy::<_, MirrorRegion<_>>(0i16);
test_copy::<_, MirrorRegion<_>>(&0i16);
test_copy::<_, MirrorRegion<_>>(0i32);
test_copy::<_, MirrorRegion<_>>(&0i32);
test_copy::<_, MirrorRegion<_>>(0i64);
test_copy::<_, MirrorRegion<_>>(&0i64);
test_copy::<_, MirrorRegion<_>>(0i128);
test_copy::<_, MirrorRegion<_>>(&0i128);
test_copy::<_, MirrorRegion<_>>(0isize);
test_copy::<_, MirrorRegion<_>>(&0isize);
test_copy::<_, MirrorRegion<_>>(0f32);
test_copy::<_, MirrorRegion<_>>(&0f32);
test_copy::<_, MirrorRegion<_>>(0f64);
test_copy::<_, MirrorRegion<_>>(&0f64);
test_copy::<_, MirrorRegion<_>>(std::num::Wrapping(0i8));
test_copy::<_, MirrorRegion<_>>(&std::num::Wrapping(0i8));
test_copy::<_, MirrorRegion<_>>(std::num::Wrapping(0i16));
test_copy::<_, MirrorRegion<_>>(&std::num::Wrapping(0i16));
test_copy::<_, MirrorRegion<_>>(std::num::Wrapping(0i32));
test_copy::<_, MirrorRegion<_>>(&std::num::Wrapping(0i32));
test_copy::<_, MirrorRegion<_>>(std::num::Wrapping(0i64));
test_copy::<_, MirrorRegion<_>>(&std::num::Wrapping(0i64));
test_copy::<_, MirrorRegion<_>>(std::num::Wrapping(0i128));
test_copy::<_, MirrorRegion<_>>(&std::num::Wrapping(0i128));
test_copy::<_, MirrorRegion<_>>(std::num::Wrapping(0isize));
test_copy::<_, MirrorRegion<_>>(&std::num::Wrapping(0isize));

test_copy::<_, ResultRegion<_, _>>(Result::<u8, u8>::Ok(0));
test_copy::<_, ResultRegion<_, _>>(&Result::<u8, u8>::Ok(0));

test_copy::<_, SliceRegion<_>>([0u8].as_slice());
test_copy::<_, SliceRegion<_>>(vec![0u8]);
test_copy::<_, SliceRegion<_>>(&vec![0u8]);

test_copy::<_, SliceRegion<_>>(["a"].as_slice());
test_copy::<_, SliceRegion<_>>(vec!["a"]);
test_copy::<_, SliceRegion<_>>(&vec!["a"]);

test_copy::<_, SliceRegion<_>>([("a",)].as_slice());
test_copy::<_, SliceRegion<_>>(vec![("a",)]);
test_copy::<_, SliceRegion<_>>(&vec![("a",)]);

test_copy::<_, CopyRegion<_>>([0u8].as_slice());

test_copy::<_, <(u8, u8) as Containerized>::Region>((1, 2));
}
}

0 comments on commit a13cb45

Please sign in to comment.