Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(WIP) optimizing push and append using vec #23

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions benches/lib-criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ fn append(criterion: &mut Criterion) {
BatchSize::SmallInput,
);
},
vec![100, 500, 1000, 5000, 10000, 50000, 100000, 200000, 500000],
vec![8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192],
)
.with_function("im-rs", |bencher, n| {
bencher.iter_batched(
Expand Down Expand Up @@ -717,9 +717,9 @@ criterion_group!(
index_sequentially,
iterator,
index_randomly,
append,
split_off,
append_clone,
append_push,
split_off
append
);
criterion_main!(benches);
86 changes: 42 additions & 44 deletions src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
use super::Flavor;
use super::PVec;
use rrbtree::iter::RrbTreeIter;
use rrbtree::BRANCH_FACTOR;
use rrbvec::RrbVec;
use std::fmt::Debug;

#[cfg(test)]
use rrbtree::BRANCH_FACTOR;

use rrbvec::iter::RrbVecIter;
use sharedptr::Take;
use std::vec::IntoIter as VecIter;

#[derive(Debug, Clone)]
pub struct PVecIter<T> {
// - you should avoid heap allocation in iterators
tree_iter: RrbTreeIter<T>,
tree_len: usize,
tail: [Option<T>; BRANCH_FACTOR],
tail_len: usize,
index: usize,
chunk: Option<([Option<T>; BRANCH_FACTOR], usize)>,
chunk_index: usize,
iter_vec: Option<VecIter<T>>,
iter_rrbvec: Option<RrbVecIter<T>>,
}

impl<T: Clone + Debug> PVecIter<T> {
fn from_vec(vec: Vec<T>) -> Self {
PVecIter {
iter_vec: Some(vec.into_iter()),
iter_rrbvec: None,
}
}

fn from_rrbvec(rrbvec: RrbVec<T>) -> Self {
PVecIter {
iter_vec: None,
iter_rrbvec: Some(rrbvec.into_iter()),
}
}
}

impl<T: Clone + Debug> Iterator for PVecIter<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
if self.index < self.tree_len {
if self.chunk.is_none() {
self.chunk = self.tree_iter.next();
}

let chunk = self.chunk.as_mut().unwrap();
if self.chunk_index >= chunk.1 {
self.chunk_index = 0;
self.chunk = self.tree_iter.next();
}

let chunk = self.chunk.as_mut().unwrap();
let value = chunk.0[self.chunk_index].take();

self.chunk_index += 1;
self.index += 1;

value
} else if self.index < self.tree_len + self.tail_len {
let index = self.index - self.tree_len;

self.index += 1;
self.tail[index].take()
if let Some(iter_vec) = self.iter_vec.as_mut() {
iter_vec.next()
} else if let Some(iter_rrbvec) = self.iter_rrbvec.as_mut() {
iter_rrbvec.next()
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.tree_len + self.tail_len;
(len, Some(len))
if let Some(iter_vec) = self.iter_vec.as_ref() {
iter_vec.size_hint()
} else if let Some(iter_rrbvec) = self.iter_rrbvec.as_ref() {
iter_rrbvec.size_hint()
} else {
(0, None)
}
}
}

Expand All @@ -58,15 +61,10 @@ impl<T: Clone + Debug> IntoIterator for PVec<T> {
type IntoIter = PVecIter<T>;

fn into_iter(self) -> Self::IntoIter {
PVecIter {
tree_len: self.tree.len(),
tree_iter: self.tree.into_iter(),
tail_len: self.tail_len,
tail: self.tail,
index: 0,
chunk: None,
chunk_index: 0,
}
return match self.0 {
Flavor::Standard(vec_arc) => PVecIter::from_vec(vec_arc.take()),
Flavor::Persistent(pvec) => PVecIter::from_rrbvec(pvec),
};
}
}

Expand Down
Loading