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

clone smallest bitmap, adjust naive/workshy and thresholds in FastAggregation.and #612

Merged
merged 1 commit into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 14 additions & 5 deletions RoaringBitmap/src/main/java/org/roaringbitmap/FastAggregation.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static RoaringBitmap and(Iterator<? extends RoaringBitmap> bitmaps) {
* @return aggregated bitmap
*/
public static RoaringBitmap and(RoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optionally '10' could be made a variable with a comment indicating that it is based on a heuristic (so we know it is a rule of thumb).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can already call the underlying methods if they want to and I think the parameter would be confusing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richardstartin This is a fine answer.

return workShyAnd(new long[1024], bitmaps);
}
return naive_and(bitmaps);
Expand All @@ -49,7 +49,7 @@ public static RoaringBitmap and(RoaringBitmap... bitmaps) {
* @return aggregated bitmap
*/
public static RoaringBitmap and(long[] aggregationBuffer, RoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
if(aggregationBuffer.length < 1024) {
throw new IllegalArgumentException("buffer should have at least 1024 elements.");
}
Expand Down Expand Up @@ -329,9 +329,18 @@ public static RoaringBitmap naive_and(RoaringBitmap... bitmaps) {
if (bitmaps.length == 0) {
return new RoaringBitmap();
}
RoaringBitmap answer = bitmaps[0].clone();
for (int k = 1; k < bitmaps.length && !answer.isEmpty(); ++k) {
answer.and(bitmaps[k]);
RoaringBitmap smallest = bitmaps[0];
for (int i = 1; i < bitmaps.length; i++) {
RoaringBitmap bitmap = bitmaps[i];
if (bitmap.highLowContainer.size() < smallest.highLowContainer.size()) {
smallest = bitmap;
}
}
RoaringBitmap answer = smallest.clone();
for (int k = 0; k < bitmaps.length && !answer.isEmpty(); ++k) {
if (bitmaps[k] != smallest) {
answer.and(bitmaps[k]);
}
}
return answer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class BufferFastAggregation {
* @return aggregated bitmap
*/
public static MutableRoaringBitmap and(ImmutableRoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
return workShyAnd(new long[1024], bitmaps);
}
return naive_and(bitmaps);
Expand All @@ -42,7 +42,7 @@ public static MutableRoaringBitmap and(ImmutableRoaringBitmap... bitmaps) {
*/
public static MutableRoaringBitmap and(long[] aggregationBuffer,
ImmutableRoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
if(aggregationBuffer.length < 1024) {
throw new IllegalArgumentException("buffer should have at least 1024 elements.");
}
Expand Down Expand Up @@ -348,9 +348,18 @@ public static MutableRoaringBitmap naive_and(ImmutableRoaringBitmap... bitmaps)
MutableRoaringBitmap answer;

if (bitmaps.length > 0) {
answer = (bitmaps[0]).toMutableRoaringBitmap();
for (int k = 1; k < bitmaps.length; ++k) {
answer = ImmutableRoaringBitmap.and(answer, bitmaps[k]);
ImmutableRoaringBitmap smallest = bitmaps[0];
for (int i = 1; i < bitmaps.length; i++) {
ImmutableRoaringBitmap bitmap = bitmaps[i];
if (bitmap.highLowContainer.size() < smallest.highLowContainer.size()) {
smallest = bitmap;
}
}
answer = smallest.toMutableRoaringBitmap();
for (ImmutableRoaringBitmap bitmap : bitmaps) {
if (bitmap != smallest) {
answer.and(bitmap);
}
}
} else {
answer = new MutableRoaringBitmap();
Expand Down