Skip to content

Significantly speed up bitmap computation#1099

Open
magdalendobson wants to merge 30 commits into
mainfrom
users/magdalen/add_filter_utils
Open

Significantly speed up bitmap computation#1099
magdalendobson wants to merge 30 commits into
mainfrom
users/magdalen/add_filter_utils

Conversation

@magdalendobson
Copy link
Copy Markdown
Contributor

@magdalendobson magdalendobson commented May 21, 2026

Introduction

Bitmap computation in diskann-label-filter is unacceptably slow. Currently, with a 1 million size slice of yfcc and a 10k query set, computing the query bitmaps takes 43.10 seconds. With just a 100K slice of the caselaw dataset and a 10k query set, computing the bitmaps takes 6.03 seconds. This was making it hard to run experiments on filtered search algorithms for the full sizes of these datasets.

Speeding up the bitmap computation is conceptually simple. Instead of iterating over every base label for every query filter, we compute an inverted index for each label type, which maps the label value to the documents with the same value. Then, at query time, we query the inverted index for the relevant label values, and compose the resulting sets as necessary to find the documents satisfying the entire filter expression. At a high level, that is what this PR does.

Lower level details

The overall workflow of the main function, compute_query_bitmaps, is as follows:

  1. Check whether the query expression contains any ASTExpr::Not clauses. If so, default to the existing slow path. This is because we don't store the document universe for each label, and thus can't compute the complement of an arbitrary bitset.
  2. Otherwise, move to the fast path.
  3. Flatten the base labels so that nested values map to a single string (e.g. the JSON string {"car": {"color":"red", "make":Mazda"}} would be transformed to {"car.color":red, "car.make":"Mazda}), and re-organize as a hash map of labels to values.
  4. For each label, compute either an inverted index (strings and bools) or an B-tree (ints and floats) depending on its type.
  5. At query time, use either the inverted index or the B-tree to produce a bitset for each CompareOp in the clause, and then compose them with AND and OR as needed to produce the final bitset.

We also add a utility to diskann-label-filter for computing the specificity of a set of query filters with respect to a base set, outputting some statistics on it, and optionally outputting the individual specificity values to a file for further processing.

Inverted Index

The inverted index maps each label value, converted to a string, to a bitset containing the doc ids corresponding to that value.

B-Tree

For simplicity, the B-tree implementation converts integers to floats before inserting so that we don't have to deal with two different types of B-tree. The performance of this piece of code isn't sensitive enough that it makes sense to differentiate, but this could be changed in the future.

The B-tree maps collections of ids to vectors instead of bitsets, because concatenating vectors is much cheaper than extending bitsets, and potentially many vectors would be concatenated during a range query.

Timings

Returning to the earlier discussion of timings, for the 1 million size slice of yfcc and a 10k query set, computing the query bitmaps now takes .6 seconds. For the 100K slice of the caselaw dataset and a 10k query set, computing the bitmaps now takes 1.728 seconds.

@magdalendobson magdalendobson marked this pull request as ready for review May 21, 2026 22:13
@magdalendobson magdalendobson requested review from a team and Copilot May 21, 2026 22:13
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR targets a major performance improvement in diskann-label-filter by introducing a fast-path for computing per-query bitmaps using precomputed per-field accelerators (inverted-index style maps for equality and a numeric BTree for range queries), while falling back to the existing evaluator when NOT is present. It also adds an example utility for computing “specificity” statistics over query filters.

Changes:

  • Add utils::compute_bitmap::compute_query_bitmaps implementing an accelerated bitmap computation path (with a NOT-guarded slow fallback).
  • Export the new bitmap API from diskann-label-filter and add an example (compute_specificities) to compute stats/output.
  • Minor doc comment updates in flattening utilities and dependency updates for the new module.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
diskann-label-filter/src/utils/flatten_utils.rs Updates doc examples for configurable flattening (one example is currently inconsistent with behavior).
diskann-label-filter/src/utils/compute_bitmap.rs New accelerated bitmap computation implementation plus unit tests.
diskann-label-filter/src/lib.rs Exposes the new module and re-exports compute_query_bitmaps.
diskann-label-filter/examples/compute_specificities.rs New example for computing/saving specificity stats from computed bitmaps.
diskann-label-filter/Cargo.toml Adds dependencies needed by the new bitmap computation module.
Cargo.lock Locks new transitive deps (bit-set, rayon) for this crate.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread diskann-label-filter/src/utils/flatten_utils.rs Outdated
Comment thread diskann-label-filter/src/utils/compute_bitmap.rs Outdated
Comment thread diskann-label-filter/src/utils/compute_bitmap.rs Outdated
Comment thread diskann-label-filter/src/utils/compute_bitmap.rs Outdated
Comment thread diskann-label-filter/src/utils/compute_bitmap.rs Outdated
Comment thread diskann-tools/src/bin/compute_specificities.rs
Comment thread diskann-label-filter/examples/compute_specificities.rs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI and others added 2 commits May 22, 2026 14:11
Agent-Logs-Url: https://github.com/microsoft/DiskANN/sessions/f16c59eb-89cf-4480-b6fe-afe4be5e7c8e

Co-authored-by: magdalendobson <58752279+magdalendobson@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/DiskANN/sessions/727d3d88-3d0b-47bf-a023-9170d72fb87a

Co-authored-by: magdalendobson <58752279+magdalendobson@users.noreply.github.com>
Copilot AI and others added 2 commits May 22, 2026 14:17
Agent-Logs-Url: https://github.com/microsoft/DiskANN/sessions/47e2bd0f-cb8b-495f-8274-02a88596b0e6

Co-authored-by: magdalendobson <58752279+magdalendobson@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/DiskANN/sessions/1bc31d27-7a19-4c4c-9ecc-c10260b944a3

Co-authored-by: magdalendobson <58752279+magdalendobson@users.noreply.github.com>
Magdalen Manohar added 3 commits May 22, 2026 14:41
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 22, 2026

Codecov Report

❌ Patch coverage is 84.74149% with 121 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.44%. Comparing base (4f70a82) to head (c205a87).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
diskann-tools/src/bin/compute_specificities.rs 0.00% 72 Missing ⚠️
diskann-tools/src/utils/compute_bitmap.rs 93.56% 46 Missing ⚠️
diskann-tools/src/utils/ground_truth.rs 50.00% 3 Missing ⚠️

❌ Your patch status has failed because the patch coverage (84.74%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1099      +/-   ##
==========================================
- Coverage   89.49%   89.44%   -0.05%     
==========================================
  Files         474      481       +7     
  Lines       89761    91118    +1357     
==========================================
+ Hits        80332    81504    +1172     
- Misses       9429     9614     +185     
Flag Coverage Δ
miri 89.44% <84.74%> (-0.05%) ⬇️
unittests 89.09% <84.74%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
diskann-label-filter/src/utils/flatten_utils.rs 87.65% <ø> (ø)
diskann-tools/src/utils/ground_truth.rs 50.00% <50.00%> (-1.50%) ⬇️
diskann-tools/src/utils/compute_bitmap.rs 93.56% <93.56%> (ø)
diskann-tools/src/bin/compute_specificities.rs 0.00% <0.00%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@hildebrandmw hildebrandmw left a comment

Choose a reason for hiding this comment

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

Performance improvement of bitmap calculation is really needed to unblock serious work on the filtered algorithms, so I have a vested interest in seeing something like this merged ASAP.

That said, there is a lot of work needed to fully productionize it. As examples:

  1. The conversion of i64 to f64 is an imprecise conversion. Similarly u64 would have to be treated separately from i64, which is currently isn't in AttributeValue.
  2. I'm not convinced the handling of "." as a separator is robust: what happens if a user string contains a "."? For example, currently {"a.b": 1} and {"a": {"b": 1}} get lowered the same, creating ambiguity.
  3. Probably want to use RoaringBitSet instead of BitSet.
  4. This adds anyhow as a low-level library error type, which is not a great fit.
  5. This also unconditionally adds rayon as a dependency of diskann-label-filter and doesn't provide a caller with a clear ability to opt-out.
  6. Copy-paste in eval_query_using_accelerators that could be factored out.
  7. Many of the helper structs made public that don't necessarily need to be.
    This is probably fine for the datasets we are testing on where everything is "nice", but I have concerns about the overall correctness in the presence of corner cases.
  8. Minor, but the PR description says R-tree when the implementation uses a B-tree.

To unblock algorithmic work, though, what if we do the following:

  1. Put this behind an "experimental" feature flag and in an "experimental" module to clearly indicate that things can go wrong.
  2. Gate the new dependencies on this features to avoid unconditionally adding dependencies (in particularly, anyhow and rayon).
  3. Enable this feature in benchmarks to unblock filtered work.

Comment thread diskann-tools/src/utils/compute_bitmap.rs
@magdalendobson
Copy link
Copy Markdown
Contributor Author

Performance improvement of bitmap calculation is really needed to unblock serious work on the filtered algorithms, so I have a vested interest in seeing something like this merged ASAP.

That said, there is a lot of work needed to fully productionize it. As examples:

  1. The conversion of i64 to f64 is an imprecise conversion. Similarly u64 would have to be treated separately from i64, which is currently isn't in AttributeValue.
  2. I'm not convinced the handling of "." as a separator is robust: what happens if a user string contains a "."? For example, currently {"a.b": 1} and {"a": {"b": 1}} get lowered the same, creating ambiguity.
  3. Probably want to use RoaringBitSet instead of BitSet.
  4. This adds anyhow as a low-level library error type, which is not a great fit.
  5. This also unconditionally adds rayon as a dependency of diskann-label-filter and doesn't provide a caller with a clear ability to opt-out.
  6. Copy-paste in eval_query_using_accelerators that could be factored out.
  7. Many of the helper structs made public that don't necessarily need to be.
    This is probably fine for the datasets we are testing on where everything is "nice", but I have concerns about the overall correctness in the presence of corner cases.
  8. Minor, but the PR description says R-tree when the implementation uses a B-tree.

To unblock algorithmic work, though, what if we do the following:

  1. Put this behind an "experimental" feature flag and in an "experimental" module to clearly indicate that things can go wrong.
  2. Gate the new dependencies on this features to avoid unconditionally adding dependencies (in particularly, anyhow and rayon).
  3. Enable this feature in benchmarks to unblock filtered work.

I'll address your individual comments separately, but one thing this makes me wonder is if we should actually move this piece of code to diskann-tools. It's true that unlike most of the rest of the library, this is geared towards internal users who are computing groundtruth/benchmarking/etc. This would resolve your concern about adding rayon and anyhow to diskann-label-filter, which I agree has drawbacks. To me this would be significantly preferable for the user experience than an experimental feature.

@magdalendobson
Copy link
Copy Markdown
Contributor Author

Performance improvement of bitmap calculation is really needed to unblock serious work on the filtered algorithms, so I have a vested interest in seeing something like this merged ASAP.

That said, there is a lot of work needed to fully productionize it. As examples:

  1. The conversion of i64 to f64 is an imprecise conversion. Similarly u64 would have to be treated separately from i64, which is currently isn't in AttributeValue.

With the move to diskann-tools, is this still something you would like to see handled?

  1. I'm not convinced the handling of "." as a separator is robust: what happens if a user string contains a "."? For example, currently {"a.b": 1} and {"a": {"b": 1}} get lowered the same, creating ambiguity.

Doesn't our existing code already explicitly treat those two expressions as the same? E.g.

/// Helper to get a nested value from a label using dot notation (e.g., "specs.cpu")

  1. Probably want to use RoaringBitSet instead of BitSet.

After the move to diskann-tools, since the existing drivers use usize for vector ids, I don't think it makes sense to move to RoaringBitSet.

  1. This adds anyhow as a low-level library error type, which is not a great fit.

Resolved by the move to diskann-tools.

  1. This also unconditionally adds rayon as a dependency of diskann-label-filter and doesn't provide a caller with a clear ability to opt-out.

Resolved by the move to diskann-tools.

  1. Copy-paste in eval_query_using_accelerators that could be factored out.

Resolved in latest edits.

  1. Many of the helper structs made public that don't necessarily need to be.
    This is probably fine for the datasets we are testing on where everything is "nice", but I have concerns about the overall correctness in the presence of corner cases.

In latest edits I made the helper functions and structs private.

  1. Minor, but the PR description says R-tree when the implementation uses a B-tree.

Resolved.

To unblock algorithmic work, though, what if we do the following:

  1. Put this behind an "experimental" feature flag and in an "experimental" module to clearly indicate that things can go wrong.
  2. Gate the new dependencies on this features to avoid unconditionally adding dependencies (in particularly, anyhow and rayon).
  3. Enable this feature in benchmarks to unblock filtered work.

@hildebrandmw
Copy link
Copy Markdown
Contributor

Performance improvement of bitmap calculation is really needed to unblock serious work on the filtered algorithms, so I have a vested interest in seeing something like this merged ASAP.
That said, there is a lot of work needed to fully productionize it. As examples:

  1. The conversion of i64 to f64 is an imprecise conversion. Similarly u64 would have to be treated separately from i64, which is currently isn't in AttributeValue.

With the move to diskann-tools, is this still something you would like to see handled?

  1. I'm not convinced the handling of "." as a separator is robust: what happens if a user string contains a "."? For example, currently {"a.b": 1} and {"a": {"b": 1}} get lowered the same, creating ambiguity.

Doesn't our existing code already explicitly treat those two expressions as the same? E.g.

/// Helper to get a nested value from a label using dot notation (e.g., "specs.cpu")

  1. Probably want to use RoaringBitSet instead of BitSet.

After the move to diskann-tools, since the existing drivers use usize for vector ids, I don't think it makes sense to move to RoaringBitSet.

  1. This adds anyhow as a low-level library error type, which is not a great fit.

Resolved by the move to diskann-tools.

  1. This also unconditionally adds rayon as a dependency of diskann-label-filter and doesn't provide a caller with a clear ability to opt-out.

Resolved by the move to diskann-tools.

  1. Copy-paste in eval_query_using_accelerators that could be factored out.

Resolved in latest edits.

  1. Many of the helper structs made public that don't necessarily need to be.
    This is probably fine for the datasets we are testing on where everything is "nice", but I have concerns about the overall correctness in the presence of corner cases.

In latest edits I made the helper functions and structs private.

  1. Minor, but the PR description says R-tree when the implementation uses a B-tree.

Resolved.

To unblock algorithmic work, though, what if we do the following:

  1. Put this behind an "experimental" feature flag and in an "experimental" module to clearly indicate that things can go wrong.
  2. Gate the new dependencies on this features to avoid unconditionally adding dependencies (in particularly, anyhow and rayon).
  3. Enable this feature in benchmarks to unblock filtered work.

Thanks Magdalen, most of this gets resolved by moving it to diskann-tools, which is a better fit.

let args: Vec<String> = env::args().collect();
if args.len() != 3 && args.len() != 4 {
eprintln!(
"Usage: {} <base_label_file> <query_label_file> [specificity_output_file]",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would prefer input intake with argparse. This is error prone.

use std::mem::discriminant;
use std::ops::Bound::{Excluded, Included, Unbounded};

struct NotNonNan;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you provide some explanation for what a NotNonNan is?


fn check_for_disallowed_operators(query_expr: &ASTExpr) -> bool {
match query_expr {
ASTExpr::Not(_) => true,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should the check for disallowed operators be more central than here?(Such as where the syntax is parse and validated)

Ok(acc.unwrap_or_else(BitSet::new))
}
ASTExpr::Not(_) => Err(anyhow::anyhow!(
"NOT operator is not supported when using query accelerators"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not?

}
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Coudl this be designed with "query_accelerator" as a trait with multiple concrete implementations?

Copy link
Copy Markdown
Contributor

@harsha-simhadri harsha-simhadri left a comment

Choose a reason for hiding this comment

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

posted some questions inline. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants