The first two posts were about “is it right.” This one is about “is it fast” — and not a 10% difference, but 5x.
1. Baseline: 128 vs 634 QPS
1M docs, luceneutil task set, Top-10:
| Scale | C++ | Java |
|---|---|---|
| 250K docs | 483 QPS | 1,886 QPS |
| 1M docs | 128 QPS | 634 QPS |
The gap isn’t constant — it grows with data size, which means the bottleneck is the per-document processing path, not startup overhead.
2. Where Lucene gets its speed: three layers of pruning
Lucene doesn’t iterate every candidate and sort; it stacks three pruning mechanisms:
| Mechanism | Role |
|---|---|
| impacts (level-0/level-1 skip data) | Each block records its max score, enabling range skipping |
| WAND | tail/lead/head: tail is a max-score heap, lead is a candidate chain, head is a doc-id heap; prune with leadScore + tailMaxScore < minCompetitiveScore |
| minCompetitiveScore | The Top-N collector propagates “the current Nth score” back to the scorer; candidates below it are dropped |
Two more details matter: integer score scaling (scale float scores to integers by 24 mantissa bits — faster and more stable comparisons), and advanceShallow must route through the full doAdvanceShallow (otherwise block metadata desyncs).
3. Implementation traps
| Trap | Symptom | Fix |
|---|---|---|
Empty head conflated with NO_MORE_DOCS | Query ends early (1,254 → 950 diffs) | Separate sentinel |
target = upTo + 1 overflow | Infinite loop past NO_MORE_DOCS | Clamp at the boundary |
| Stale cached block bounds | matches() misjudges | upTo_ >= doc_ check + invalidate on insert |
advanceShallow bypassing the wrapper | Bit-set block exhausted early | Go through doAdvanceShallow |
Wrong leadCost | Under-pruning | Confirm Lucene’s semantics: for a disjunction, leadCost is its own cost() |
After the fixes: 592 QPS at 1M (Java 644), p99 18.3 ms — better than Java’s 22.7 ms; 1,909 at 250K (Java 2,155).
4. Measurement discipline: more important than the optimization
Performance work is easy to fool via measurement. We hit three classes:
- Stray processes: two leftover benchmark processes from an earlier round were still running, biasing the whole run by ~9%. After killing them C++ went 542→589 and Java 599→627 — ratios barely moved, which shows how much absolute-number noise there was.
- Isolated family runs mislead: running one query family alone disagreed with the mixed full-set run by up to 17x. Final rule: always attribute per family over the full task set (
KIND_SECONDS). - Measure both scales: 250K caught bugs 1M didn’t (a bound error at one block boundary), and 1M exposes things 250K can’t hit.
Two more lessons:
- A sudden speedup should make you suspect the fixture: a postings read once got 10x faster, and it turned out the test used an empty index;
- Experiments must be revertible: changing the in-block scan from a linear walk to bisection/hybrid made Or queries 21% slower — reverted immediately. Before git, backups; after git, commits.
5. What’s still behind
Per-family ratios vs Java after the work (< 1 means C++ is faster):
| Family | Ratio |
|---|---|
| Phrase (63% of the workload) | 0.76–0.86x |
| And | 0.95–1.34x |
| Or | 1.38–2.25x |
| IntNRQ | 4.93x |
| Wildcard | 2.92x |
| Prefix3 | 4.16x |
| Fuzzy1 / Fuzzy2 | 3.01x / 3.74x |
An interesting finding: Lucene 10.4 uses MaxScoreBulkScorer (not WAND) for TOP_SCORES && minShouldMatch <= 1. We tried porting MAXSCORE, but it dropped documents on 3 fuzzy queries; after checking, the drop couldn’t be explained (a self-check reported 7 unjustified drops), so we reverted and documented it, enabling it only for ≥3 clauses.
6. Summary
- The performance gap is a per-document cost problem; all three pruning layers are needed;
- Boundary conditions in pruning (sentinels, overflow, stale caches) are bug hotspots;
- Measurement itself needs discipline: exclude stray processes, attribute over the full set, measure both scales;
- An honest result beats a pretty but untrustworthy number.
Next: the writer and the coverage long tail — how 7GB of memory became 398MB, and how coverage went from 59% to 100%.
Related
- Rewriting Lucene in C++ (Part 1): Byte-Level Compatibility — When Java Reads a C++-Written Index
- Rewriting Lucene in C++ (Part 2): Bit-Exact Scoring — The Floating-Point Traps of BM25
- Implementing Git for Data on DuckDB (Part 3): Branches, diff, pick, merge — The Version Primitives of Data
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.