Skip to content

Collect sparse constant-score iterators one doc at a time - #17029

Open
uchiha-asha wants to merge 5 commits into
apache:mainfrom
uchiha-asha:lucene-perf-sparse-constant-score
Open

uchiha-asha wants to merge 5 commits into
apache:mainfrom
uchiha-asha:lucene-perf-sparse-constant-score

Conversation

@uchiha-asha

@uchiha-asha uchiha-asha commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Since #16141, ConstantScoreBulkScorer loads matches of plain iterators into a 4096-doc bit-set window via intoBitSet and collects the window as a DocIdStream. Processing each window incurs fixed overhead even when it contains few matches. So, selective filters (a term or range matching 1 doc in 1000) pay the full window cost for a handful of docs.

This change checks the iterator's cost() up front. Plain iterators that are expected to match fewer than 1 doc in 128 (32 matches per window) are collected one doc at a time with DefaultBulkScorer, both in ConstantScoreScorerSupplier and in the no-score filter path of BooleanScorerSupplier.

Benchmark - Times are µs/op, lower is better. JDK 25.0.5, 2 GB heap, 3 forks × 5 measured iterations on a shared VM.

Workload Baseline Upfront cost()
Term, 1 in 1,000, read a doc value per hit 280.6 ± 58.4 69.5 ± 21.6
Term, 1 in 10,000, same collector 46.9 ± 6.4 9.6 ± 3.7
Term, 1 in 256, count 151.6 ± 82.8 73.6 ± 14.3
Point range, 1 in 1,000, read a doc value per hit 368.0 ± 135.8 120.7 ± 52.6
64 adjacent hits, then 1 every 4,096 docs, count 65.0 ± 6.4 6.4 ± 0.9
Term, 1 in 50, read a doc value per hit 1,975 ± 181 1,957 ± 344

@uchiha-asha

Copy link
Copy Markdown
Contributor Author

@romseygeek @costin could you please review this when available. Thanks!

@romseygeek

Copy link
Copy Markdown
Contributor

Thanks for opening @uchiha-asha!

I wonder if we can get similar performance by checking the cost() of the filter upfront, rather than trying to switch between collection modes? I think I'd also like to see an end-to-end https://github.com/mikemccand/luceneutil benchmark.

@uchiha-asha

Copy link
Copy Markdown
Contributor Author

Thanks @romseygeek. I reran the same JMH benchmarks again after choosing DefaultBulkScorer up front for plain iterators with cost() < maxDoc / 128. The upfront check had a lower mean time than this PR on five of the six workloads, though several differences were within run-to-run variation.

Times are µs/op, lower is better. JDK 25.0.5, 2 GB heap, 3 forks × 5 measured iterations on a shared VM.

Workload Baseline This PR Upfront cost()
Term, 1 in 1,000, read a doc value per hit 280.6 ± 58.4 76.9 ± 49.3 69.5 ± 21.6
Term, 1 in 10,000, same collector 46.9 ± 6.4 10.3 ± 6.9 9.6 ± 3.7
Term, 1 in 256, count 151.6 ± 82.8 89.4 ± 11.3 73.6 ± 14.3
Point range, 1 in 1,000, read a doc value per hit 368.0 ± 135.8 85.7 ± 12.2 120.7 ± 52.6
64 adjacent hits, then 1 every 4,096 docs, count 65.0 ± 6.4 9.6 ± 1.0 6.4 ± 0.9
Term, 1 in 50, read a doc value per hit 1,975 ± 181 2,545 ± 1,289 1,957 ± 344

I also ran luceneutil’s SearchPerfTest. On wikimedium10m, neither version showed a clear improvement over baseline on countOnly.tasks or the additional prefix and filtered-disjunction counts.

Because of the simpler implementation, I plan to switch this PR to the upfront cost() check. Let me know what you think.

@jainankitk jainankitk left a comment

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.

Thanks @uchiha-asha for iterating.

I looked at whether we could do better than cost(), e.g. estimating density from the doc ID span of the matches. The first match is cheap to get, but DocIdSetIterator can't give us the last one without new API. docIDRunEnd() only helps for fully contiguous runs. Given your numbers, I agree with @romseygeek that the upfront cost() check is the best trade-off: simpler, and as fast or faster on most workloads. +1 to switching to it.

@uchiha-asha uchiha-asha changed the title Collect sparse matches one by one in ConstantScoreBulkScorer Collect sparse constant-score iterators one doc at a time Nov 20, 2026
@uchiha-asha

uchiha-asha commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @jainankitk @romseygeek, I have made the change for cost() check, updated description and title.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants