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:

ScaleC++Java
250K docs483 QPS1,886 QPS
1M docs128 QPS634 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:

MechanismRole
impacts (level-0/level-1 skip data)Each block records its max score, enabling range skipping
WANDtail/lead/head: tail is a max-score heap, lead is a candidate chain, head is a doc-id heap; prune with leadScore + tailMaxScore < minCompetitiveScore
minCompetitiveScoreThe 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

TrapSymptomFix
Empty head conflated with NO_MORE_DOCSQuery ends early (1,254 → 950 diffs)Separate sentinel
target = upTo + 1 overflowInfinite loop past NO_MORE_DOCSClamp at the boundary
Stale cached block boundsmatches() misjudgesupTo_ >= doc_ check + invalidate on insert
advanceShallow bypassing the wrapperBit-set block exhausted earlyGo through doAdvanceShallow
Wrong leadCostUnder-pruningConfirm 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:

  1. 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.
  2. 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).
  3. 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):

FamilyRatio
Phrase (63% of the workload)0.76–0.86x
And0.95–1.34x
Or1.38–2.25x
IntNRQ4.93x
Wildcard2.92x
Prefix34.16x
Fuzzy1 / Fuzzy23.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%.

Get the next essay

New essays on products, business and tech — plus what the experiment's numbers actually say.