-
Notifications
You must be signed in to change notification settings - Fork 84
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
Unions on multiple bitmaps at a time #58
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0d806a8
First draft of the Muple type
Kerollmops 0a03dfb
Rewite the Muple with an heap and interior mutability
Kerollmops 0a99b3f
Introduce the multi_union operator
Kerollmops 60138bb
Introduce the multi_ops module
Kerollmops 0dde158
Change the multi union function to become in-place
Kerollmops df3311b
Add criterion benchmarks
Kerollmops 64441bf
Change the benchmarks to involve more bitmaps
Kerollmops f3fc45f
Do the union operation on the stores not the containers
Kerollmops 611ea75
Change the function into union_of
Kerollmops 7bab470
Use a custom next method to cache buffer
Kerollmops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ mod util; | |
mod cmp; | ||
mod inherent; | ||
mod iter; | ||
mod multi_ops; | ||
mod ops; | ||
mod serialization; | ||
|
||
|
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,163 @@ | ||
use std::cell::RefCell; | ||
use std::cmp::{Ordering, Reverse}; | ||
use std::collections::BinaryHeap; | ||
use std::iter::Peekable; | ||
use std::slice; | ||
|
||
use super::container::Container; | ||
use crate::RoaringBitmap; | ||
|
||
// This struct is here to bypass the `Ord::cmp` limitation | ||
// where it is not possible to mutate self to get or compute a value. | ||
struct InteriorMutable<'a>(RefCell<Peekable<slice::Iter<'a, Container>>>); | ||
|
||
struct Muple<'a> { | ||
heap: BinaryHeap<Reverse<InteriorMutable<'a>>>, | ||
buffer: Vec<&'a Container>, | ||
} | ||
|
||
impl RoaringBitmap { | ||
/// Unions in-place with the specified others bitmaps. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// use roaring::RoaringBitmap; | ||
/// | ||
/// let rb1 = (0..5).collect(); | ||
/// let rb2 = (5..10).collect(); | ||
/// let rb3 = (10..15).collect(); | ||
/// let rb4 = (0..4).collect(); | ||
/// | ||
/// let out = RoaringBitmap::union_of(&[rb1, rb2, rb3, rb4]); | ||
/// | ||
/// assert_eq!(out, (0..15).collect()); | ||
/// ``` | ||
pub fn union_of<'a>(bitmaps: impl IntoIterator<Item = &'a Self>) -> Self { | ||
let iter = bitmaps.into_iter().map(|b| b.containers.iter().peekable()); | ||
let mut muple = Muple::new(iter); | ||
|
||
let mut stores = Vec::new(); | ||
while let Some(cs) = muple.next() { | ||
let a = cs[0].clone(); // safe | ||
let mut store = a.store; | ||
cs[1..].iter().for_each(|c| store.union_with(&c.store)); | ||
stores.push((a.key, store)); | ||
} | ||
|
||
// We reconstruct the containers from the stores | ||
let containers = stores | ||
.into_iter() | ||
.map(|(key, store)| { | ||
let mut container = Container { | ||
key, | ||
len: store.len(), | ||
store, | ||
}; | ||
container.ensure_correct_store(); | ||
container | ||
}) | ||
.collect(); | ||
|
||
RoaringBitmap { containers } | ||
} | ||
} | ||
|
||
impl Ord for InteriorMutable<'_> { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
let mut c1 = self.0.borrow_mut(); | ||
let mut c2 = other.0.borrow_mut(); | ||
|
||
match (c1.peek(), c2.peek()) { | ||
(None, None) => Ordering::Equal, | ||
(Some(_), None) => Ordering::Less, // move Nones to the back | ||
(None, Some(_)) => Ordering::Greater, | ||
(Some(c1), Some(c2)) => match (c1.key, c2.key) { | ||
(key1, key2) if key1 == key2 => Ordering::Equal, | ||
(key1, key2) if key1 < key2 => Ordering::Less, | ||
(key1, key2) if key1 > key2 => Ordering::Greater, | ||
(_, _) => unreachable!(), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> InteriorMutable<'a> { | ||
fn new(iter: Peekable<slice::Iter<'a, Container>>) -> Self { | ||
InteriorMutable(RefCell::new(iter)) | ||
} | ||
} | ||
|
||
impl PartialOrd for InteriorMutable<'_> { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl PartialEq for InteriorMutable<'_> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.cmp(other) == Ordering::Equal | ||
} | ||
} | ||
|
||
impl Eq for InteriorMutable<'_> {} | ||
|
||
impl<'a> Muple<'a> { | ||
fn new<I>(iters: I) -> Muple<'a> | ||
where | ||
I: IntoIterator<Item = Peekable<slice::Iter<'a, Container>>>, | ||
{ | ||
let mut heap = BinaryHeap::new(); | ||
|
||
iters.into_iter().for_each(|iter| { | ||
heap.push(Reverse(InteriorMutable::new(iter))); | ||
}); | ||
|
||
let buffer = Vec::with_capacity(heap.len()); | ||
|
||
Muple { heap, buffer } | ||
} | ||
} | ||
|
||
impl<'a> Muple<'a> { | ||
fn next(&mut self) -> Option<&[&'a Container]> { | ||
// We retrieve the lowest key that we must return containers for. | ||
let key = match self.heap.peek_mut() { | ||
Some(mut iter) => { | ||
match (iter.0).0.get_mut().peek() { | ||
Some(c) => c.key, | ||
// Nones are moved to the back, | ||
// it means that we only have empty iterators. | ||
None => return None, | ||
} | ||
} | ||
None => return None, | ||
}; | ||
|
||
self.buffer.clear(); | ||
|
||
while let Some(mut iter) = self.heap.peek_mut() { | ||
let containers = (iter.0).0.get_mut(); | ||
match containers.peek() { | ||
// This iterator gives us a key that is corresponding | ||
// to the lowest one, we must return this container | ||
Some(c) if c.key == key => { | ||
let container = containers.next().unwrap(); | ||
self.buffer.push(container); | ||
} | ||
// Keys are no more equal to the lowest one, we must stop. | ||
Some(_) => break, | ||
// This iterator is exhauted we must stop here as empty iterators | ||
// are pushed to the back of the heap. This means that we will | ||
// continue to see this empty iterator if we continue peeking. | ||
None => break, | ||
} | ||
} | ||
|
||
if !self.buffer.is_empty() { | ||
Some(&self.buffer) | ||
} else { | ||
None | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that it measures the time it takes to aggregate a few deterministic tiny sets... Tiny sets in a branchy setting lead to incorrect benchmarks because of branch prediction... see https://www.infoq.com/articles/making-code-faster-taming-branches/
I suggest using realistic data sets. See for example https://github.com/RoaringBitmap/RoaringBitmap/tree/master/real-roaring-dataset/src/main/resources/real-roaring-dataset