-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from orxfun/recursive-growth-strategy
recursive growth strategy is introduced
- Loading branch information
Showing
27 changed files
with
697 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "orx-split-vec" | ||
version = "1.2.1" | ||
version = "1.3.0" | ||
edition = "2021" | ||
authors = ["orxfun <[email protected]>"] | ||
description = "An efficient constant access time vector with dynamic capacity and pinned elements." | ||
|
@@ -13,7 +13,7 @@ categories = ["data-structures", "rust-patterns"] | |
orx-pinned-vec = "1.0" | ||
|
||
[[bench]] | ||
name = "serial_access" | ||
name = "append" | ||
harness = false | ||
|
||
[dev-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion}; | ||
use orx_split_vec::prelude::*; | ||
use rand::prelude::*; | ||
use rand_chacha::ChaCha8Rng; | ||
|
||
const NUM_APPEND_OPS: usize = 32; | ||
|
||
struct Vectors(Vec<Vec<usize>>); | ||
impl Clone for Vectors { | ||
fn clone(&self) -> Self { | ||
let vecs = (0..self.0.len()).map(|i| self.0[i].clone()).collect(); | ||
Self(vecs) | ||
} | ||
} | ||
|
||
fn get_vectors(n: usize) -> Vectors { | ||
let mut rng = ChaCha8Rng::seed_from_u64(685412); | ||
Vectors( | ||
(0..NUM_APPEND_OPS) | ||
.map(|_| (0..n).map(|_| rng.gen_range(0..n)).collect()) | ||
.collect(), | ||
) | ||
} | ||
|
||
fn calc_std_vec(mut vec: Vec<usize>, mut vectors: Vectors) -> Vec<usize> { | ||
for x in &mut vectors.0 { | ||
vec.append(x); | ||
} | ||
vec | ||
} | ||
|
||
fn calc_split_vec_extend<G: Growth>( | ||
mut vec: SplitVec<usize, G>, | ||
mut vectors: Vectors, | ||
) -> SplitVec<usize, G> { | ||
for x in &mut vectors.0 { | ||
vec.extend_from_slice(&x); | ||
x.clear(); | ||
} | ||
vec | ||
} | ||
|
||
fn calc_split_vec_append( | ||
mut vec: SplitVec<usize, Recursive>, | ||
vectors: Vectors, | ||
) -> SplitVec<usize, Recursive> { | ||
let vectors = vectors.0; | ||
for x in vectors { | ||
vec.append(x) | ||
} | ||
vec | ||
} | ||
|
||
fn bench(c: &mut Criterion) { | ||
let treatments = vec![1_024, 16_384, 262_144, 4_194_304]; | ||
|
||
let mut group = c.benchmark_group("append"); | ||
|
||
for n in treatments { | ||
let treatment = format!("n={}]", n); | ||
let vectors = get_vectors(n); | ||
|
||
group.bench_with_input(BenchmarkId::new("std_vec_new", &treatment), &n, |b, _| { | ||
b.iter_batched( | ||
|| get_vectors(n), | ||
|vectors| calc_std_vec(black_box(Vec::new()), black_box(vectors)), | ||
BatchSize::LargeInput, | ||
) | ||
}); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("std_vec_with_exact_capacity", &treatment), | ||
&n, | ||
|b, _| { | ||
let capacity: usize = vectors.0.iter().map(|x| x.len()).sum(); | ||
b.iter_batched( | ||
|| get_vectors(n), | ||
|vectors| { | ||
calc_std_vec(black_box(Vec::with_capacity(capacity)), black_box(vectors)) | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}, | ||
); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("split_vec_linear - 2^10", &treatment), | ||
&n, | ||
|b, _| { | ||
b.iter_batched( | ||
|| get_vectors(n), | ||
|vectors| { | ||
calc_split_vec_extend( | ||
black_box(SplitVec::with_linear_growth(10)), | ||
black_box(vectors), | ||
) | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}, | ||
); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("split_vec_doubling", &treatment), | ||
&n, | ||
|b, _| { | ||
b.iter_batched( | ||
|| get_vectors(n), | ||
|vectors| { | ||
calc_split_vec_extend( | ||
black_box(SplitVec::with_doubling_growth()), | ||
black_box(vectors), | ||
) | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}, | ||
); | ||
|
||
group.bench_with_input( | ||
BenchmarkId::new("split_vec_recursive", &treatment), | ||
&n, | ||
|b, _| { | ||
b.iter_batched( | ||
|| get_vectors(n), | ||
|vectors| { | ||
calc_split_vec_append( | ||
black_box(SplitVec::with_recursive_growth()), | ||
black_box(vectors), | ||
) | ||
}, | ||
BatchSize::LargeInput, | ||
) | ||
}, | ||
); | ||
} | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use crate::{Fragment, Growth, SplitVec}; | ||
|
||
/// Converts self into a collection of [`Fragment`]s. | ||
pub trait IntoFragments<T> { | ||
/// Converts self into a collection of [`Fragment`]s. | ||
fn into_fragments(self) -> impl Iterator<Item = Fragment<T>>; | ||
} | ||
|
||
impl<T> IntoFragments<T> for Vec<T> { | ||
fn into_fragments(self) -> impl Iterator<Item = Fragment<T>> { | ||
[Fragment::from(self)].into_iter() | ||
} | ||
} | ||
|
||
impl<T, const N: usize> IntoFragments<T> for [Vec<T>; N] { | ||
fn into_fragments(self) -> impl Iterator<Item = Fragment<T>> { | ||
self.into_iter().map(Fragment::from) | ||
} | ||
} | ||
|
||
impl<T> IntoFragments<T> for Vec<Vec<T>> { | ||
fn into_fragments(self) -> impl Iterator<Item = Fragment<T>> { | ||
self.into_iter().map(Fragment::from) | ||
} | ||
} | ||
|
||
impl<T, G: Growth> IntoFragments<T> for SplitVec<T, G> { | ||
fn into_fragments(self) -> impl Iterator<Item = Fragment<T>> { | ||
self.fragments.into_iter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ mod deref; | |
mod eq; | ||
pub(crate) mod fragment_struct; | ||
mod from; | ||
pub(crate) mod into_fragments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
mod constants; | ||
mod doubling_growth; | ||
mod from; | ||
mod impl_growth; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use impl_growth::Doubling; | ||
pub use doubling_growth::Doubling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
mod constants; | ||
mod from; | ||
mod impl_growth; | ||
mod linear_growth; | ||
|
||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use impl_growth::Linear; | ||
pub use linear_growth::Linear; |
Oops, something went wrong.