Skip to content

Commit

Permalink
Progress on v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Firstyear committed Apr 9, 2021
1 parent 3142724 commit 5aed2f7
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 130 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ license = "MPL-2.0"
# time = "0.2"
serde = "1.0"
serde_derive = "1.0"
smallvec = { version = "1.4", features = ["serde"], optional = true }
smallvec = { version = "1.6", features = ["serde"] }

[dev-dependencies]
criterion = "0.3"
Expand All @@ -27,5 +27,3 @@ name = "bench_idl"
harness = false

[features]
use_smallvec = ["smallvec"]
tracksize = []
60 changes: 60 additions & 0 deletions benches/bench_idl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};

use idl_simple::IDLSimple;
use idlset::IDLBitRange;
use idlset::v2::IDLBitRange as IDLBitRangeV2;
use std::iter::FromIterator;

// Trying to make these work with trait bounds is literally too hard
Expand All @@ -30,6 +31,15 @@ impl std::fmt::Display for RDuplex {
}
}

struct V2Duplex(IDLBitRangeV2, IDLBitRangeV2);

impl std::fmt::Display for V2Duplex {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {} - {} {}", self.0.len(), self.0.is_compressed(), self.1.len(), self.1.is_compressed())
}
}


struct Triplex(Vec<u64>, Vec<u64>, Vec<u64>);

struct STriplex(IDLSimple, IDLSimple, IDLSimple);
Expand Down Expand Up @@ -102,6 +112,11 @@ fn do_bench_duplex(c: &mut Criterion, label: &str, i: Duplex) {
IDLBitRange::from_iter(i.1.clone()),
);

let mut v2i = V2Duplex(
IDLBitRangeV2::from_iter(i.0.clone()),
IDLBitRangeV2::from_iter(i.1.clone()),
);

group.bench_with_input(BenchmarkId::new("Simple", &si), &si, |t, SDuplex(a, b)| {
t.iter(|| { b | a }.sum())
});
Expand All @@ -110,8 +125,32 @@ fn do_bench_duplex(c: &mut Criterion, label: &str, i: Duplex) {
&ri,
|t, RDuplex(a, b)| t.iter(|| { b | a }.sum()),
);
group.bench_with_input(
BenchmarkId::new("Compressed V2 Sparse Sparse", &ri),
&ri,
|t, V2Duplex(a, b)| t.iter(|| { b | a }.sum()),
);

v2i.0.compress();
group.bench_with_input(
BenchmarkId::new("Compressed V2 Sparse Compressed", &ri),
&ri,
|t, V2Duplex(a, b)| t.iter(|| { b | a }.sum()),
);

v2i.1.compress();
group.bench_with_input(
BenchmarkId::new("Compressed V2 Compressed Compressed", &ri),
&ri,
|t, V2Duplex(a, b)| t.iter(|| { b | a }.sum()),
);
group.finish();

let mut v2i = V2Duplex(
IDLBitRangeV2::from_iter(i.0.clone()),
IDLBitRangeV2::from_iter(i.1.clone()),
);

let mut group = c.benchmark_group(&format!("{}_intersection", label));
group.bench_with_input(BenchmarkId::new("Simple", &si), &si, |t, SDuplex(a, b)| {
t.iter(|| { b & a }.sum())
Expand All @@ -121,6 +160,27 @@ fn do_bench_duplex(c: &mut Criterion, label: &str, i: Duplex) {
&ri,
|t, RDuplex(a, b)| t.iter(|| { b & a }.sum()),
);

group.bench_with_input(
BenchmarkId::new("Compressed V2 Sparse Sparse", &ri),
&ri,
|t, V2Duplex(a, b)| t.iter(|| { b & a }.sum()),
);

v2i.0.compress();
group.bench_with_input(
BenchmarkId::new("Compressed V2 Sparse Compressed", &ri),
&ri,
|t, V2Duplex(a, b)| t.iter(|| { b & a }.sum()),
);

v2i.1.compress();
group.bench_with_input(
BenchmarkId::new("Compressed V2 Compressed Compressed", &ri),
&ri,
|t, V2Duplex(a, b)| t.iter(|| { b & a }.sum()),
);

group.finish();
}

Expand Down
18 changes: 1 addition & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,12 @@

#[macro_use]
extern crate serde_derive;

#[cfg(feature = "use_smallvec")]
#[macro_use]
extern crate smallvec;

pub mod v2;

#[cfg(feature = "use_smallvec")]
use smallvec::SmallVec;

use std::cmp::Ordering;
use std::iter::FromIterator;
use std::ops::{BitAnd, BitOr};
Expand All @@ -37,7 +34,6 @@ use std::{fmt, slice};
/// Default number of IDL ranges to keep in stack before we spill into heap. As many
/// operations in a system like kanidm are either single item indexes (think equality)
/// or very large indexes (think pres, class), we can keep this small.
#[cfg(feature = "use_smallvec")]
const DEFAULT_STACK_ALLOC: usize = 1;

/// Bit trait representing the equivalent of a & (!b). This allows set operations
Expand Down Expand Up @@ -161,28 +157,19 @@ impl IDLRange {
/// ```
#[derive(Serialize, Deserialize, PartialEq, Clone)]
pub struct IDLBitRange {
#[cfg(not(feature = "use_smallvec"))]
list: Vec<IDLRange>,
#[cfg(feature = "use_smallvec")]
list: SmallVec<[IDLRange; DEFAULT_STACK_ALLOC]>,
}

impl IDLBitRange {
/// Construct a new, empty set.
pub fn new() -> Self {
IDLBitRange {
#[cfg(not(feature = "use_smallvec"))]
list: Vec::new(),
#[cfg(feature = "use_smallvec")]
list: SmallVec::new(),
}
}

fn with_capacity(cap: usize) -> Self {
IDLBitRange {
#[cfg(not(feature = "use_smallvec"))]
list: Vec::with_capacity(cap),
#[cfg(feature = "use_smallvec")]
list: SmallVec::with_capacity(cap),
}
}
Expand Down Expand Up @@ -530,10 +517,7 @@ impl FromIterator<u64> for IDLBitRange {

let (lower_bound, _) = iter.size_hint();
let mut new = IDLBitRange {
#[cfg(feature = "use_smallvec")]
list: SmallVec::with_capacity(lower_bound),
#[cfg(not(feature = "use_smallvec"))]
list: Vec::with_capacity(lower_bound),
};

let mut max_seen = 0;
Expand Down
Loading

0 comments on commit 5aed2f7

Please sign in to comment.