Skip to content

Commit

Permalink
wip clone impls
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Jun 22, 2024
1 parent 1d5e426 commit 6efb01e
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 19 deletions.
73 changes: 71 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
extern crate test;

Check warning on line 5 in benches/bench.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/flatcontainer/flatcontainer/benches/bench.rs

use flatcontainer::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs};
use flatcontainer::impls::offsets::OffsetOptimized;
use flatcontainer::impls::offsets::{OffsetStride};
use flatcontainer::impls::tuple::{TupleABCRegion, TupleABRegion};
use flatcontainer::{
ColumnsRegion, Containerized, FlatStack, MirrorRegion, OwnedRegion, Push, Region, ReserveItems,
Expand Down Expand Up @@ -87,7 +87,7 @@ fn string10_copy_region(bencher: &mut Bencher) {
#[bench]
fn string10_copy_region_collapse(bencher: &mut Bencher) {
_bench_copy_region::<
SliceRegion<CollapseSequence<ConsecutiveOffsetPairs<StringRegion>>, OffsetOptimized>,
SliceRegion<CollapseSequence<ConsecutiveOffsetPairs<StringRegion>>, OffsetStride>,
_,
>(bencher, vec![format!("grawwwwrr!"); 1024]);
}
Expand Down Expand Up @@ -263,6 +263,49 @@ fn vec_u_vn_s_prealloc(bencher: &mut Bencher) {
);
}

#[bench]
fn empty_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec![(); 1024]);
}
#[bench]
fn u64_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec![0u64; 1024]);
}
#[bench]
fn u32x2_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec![(0u32, 0u32); 1024]);
}
#[bench]
fn u8_u64_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec![(0u8, 0u64); 512]);
}
#[bench]
fn str10_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec!["grawwwwrr!"; 1024]);
}
#[bench]
fn string10_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec![format!("grawwwwrr!"); 1024]);
}
#[bench]
fn string20_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(bencher, vec![format!("grawwwwrr!!!!!!!!!!!"); 512]);
}
#[bench]
fn vec_u_s_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(
bencher,
vec![vec![(0u64, "grawwwwrr!".to_string()); 32]; 32],
);
}
#[bench]
fn vec_u_vn_s_copy_flat(bencher: &mut Bencher) {
_bench_copy_flat(
bencher,
vec![vec![(0u64, vec![(); 1 << 40], "grawwwwrr!".to_string()); 32]; 32],
);
}

fn _bench_copy<T: Containerized + Eq>(bencher: &mut Bencher, record: T)
where
for<'a> <T as Containerized>::Region: Push<&'a T>,
Expand Down Expand Up @@ -343,3 +386,29 @@ where
}
});
}

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

bencher.iter(|| {
target.clone_from(&arena);
// for item in &arena {
// target.copy(item);
// }
});
let (mut siz, mut cap) = (0, 0);
arena.heap_size(|this_siz, this_cap| {
siz += this_siz;
cap += this_cap
});
println!("{siz} {cap}");
}
10 changes: 9 additions & 1 deletion src/impls/offsets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub trait OffsetContainer<T>: Default {
/// Allocate with space for `capacity` elements.
fn with_capacity(capacity: usize) -> Self;

/// Allocate storage large enough to absorb `regions`'s contents.
fn merge_regions<'a>(regions: impl Iterator<Item = &'a Self> + Clone) -> Self
where
Self: 'a,
{
Self::with_capacity(regions.map(Self::len).sum())
}

/// Accepts a newly pushed element.
fn push(&mut self, item: T);

Expand Down Expand Up @@ -285,7 +293,7 @@ where
L: OffsetContainer<u64>,
{
fn with_capacity(_capacity: usize) -> Self {
// `self.strided` doesn't have any capacity and we don't know the structure of the data.
// `self.strided` doesn't have any capacity, and we don't know the structure of the data.
Self::default()
}

Expand Down
21 changes: 20 additions & 1 deletion src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<T: Containerized, const N: usize> Containerized for [T; N] {
///
/// assert_eq!(r.index(de_index).get(2), "jagen");
/// ```
#[derive(Debug, Clone)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SliceRegion<C: Region, O: OffsetContainer<C::Index> = Vec<<C as Region>::Index>> {
/// Container of slices.
Expand All @@ -60,6 +60,25 @@ pub struct SliceRegion<C: Region, O: OffsetContainer<C::Index> = Vec<<C as Regio
inner: C,
}

impl<R, O> Clone for SliceRegion<R, O>
where
R: Region + Clone,
O: OffsetContainer<R::Index> + Clone,
{
fn clone(&self) -> Self {
Self {
slices: self.slices.clone(),
inner: self.inner.clone(),
}
}

fn clone_from(&mut self, source: &Self) {
self.slices.clone_from(&source.slices);
self.inner.clone_from(&source.inner);
}

Check warning on line 78 in src/impls/slice.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/flatcontainer/flatcontainer/src/impls/slice.rs
}


impl<C: Region, O: OffsetContainer<C::Index>> Region for SliceRegion<C, O> {
type Owned = Vec<C::Owned>;
type ReadItem<'a> = ReadSlice<'a, C, O> where Self: 'a;
Expand Down
18 changes: 17 additions & 1 deletion src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@ use crate::{CopyIter, Push, Region, ReserveItems};
/// assert_eq!(panagram_de.as_bytes(), r.index(de_index));
/// assert_eq!(panagram_en.as_bytes(), r.index(en_index));
/// ```
#[derive(Debug, Clone)]
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct OwnedRegion<T, S: SliceStorage<T> = Vec<T>> {
slices: S,
_marker: PhantomData<T>,
}

impl<T, S> Clone for OwnedRegion<T, S>
where
S: SliceStorage<T> + Clone,
{
fn clone(&self) -> Self {
Self {
slices: self.slices.clone(),
_marker: PhantomData,
}
}

fn clone_from(&mut self, source: &Self) {
self.slices.clone_from(&source.slices);
}
}

impl<T, S> Region for OwnedRegion<T, S>
where
[T]: ToOwned,
Expand Down
17 changes: 16 additions & 1 deletion src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::{Containerized, Push, Region, ReserveItems};
/// assert_eq!(panagram_de, r.index(de_index));
/// assert_eq!(panagram_en, r.index(en_index));
/// ```
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct StringRegion<R = OwnedRegion<u8>>
where
Expand All @@ -39,6 +39,21 @@ where
inner: R,
}

Check warning on line 41 in src/impls/string.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/flatcontainer/flatcontainer/src/impls/string.rs
impl<R> Clone for StringRegion<R>
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + Clone + 'a,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}

fn clone_from(&mut self, source: &Self) {
self.inner.clone_from(&source.inner);
}
}

impl<R> Region for StringRegion<R>
where
for<'a> R: Region<ReadItem<'a> = &'a [u8]> + 'a,
Expand Down
18 changes: 17 additions & 1 deletion src/impls/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,28 @@ macro_rules! tuple_flatcontainer {

/// A region for a tuple.
#[allow(non_snake_case)]
#[derive(Default, Clone, Debug)]
#[derive(Default, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct [<Tuple $($name)* Region >]<$($name),*> {
$([<container $name>]: $name),*
}

#[allow(non_snake_case)]
impl<$($name: Region + Clone),*> Clone for [<Tuple $($name)* Region>]<$($name),*>
where
$(<$name as Region>::Index: crate::Index),*
{
fn clone(&self) -> Self {
Self {
$([<container $name>]: self.[<container $name>].clone(),)*
}
}

fn clone_from(&mut self, source: &Self) {
$(self.[<container $name>].clone_from(&source.[<container $name>]);)*
}
}

#[allow(non_snake_case)]
impl<$($name: Region),*> Region for [<Tuple $($name)* Region>]<$($name),*>
where
Expand Down
Loading

0 comments on commit 6efb01e

Please sign in to comment.