One underscore and a few hundred lines of Rust: raggio's hybrid search tail

■
One underscore and a few hundred lines of Rust: raggio's hybrid search tail
Table of Contents

Hybrid search was raggio’s widest loss in the first benchmark: a median of 104 ms against Weaviate’s 36, and a p99 near 600. The first post blamed FTS5’s missing WAND and the Python rescoring that makes up for it. That was half right.

A hybrid query runs a vector leg and a text leg and fuses the two. The text leg has two stages (the first post explains why): stage 1 asks SQLite’s FTS5 for candidates, with the query pruned to its rarest words so a common word can’t make FTS5 rank most of the corpus, and stage 2 rescores those candidates in Python with the full query.

One underscore

24 of the 500 benchmark titles contain a token with an underscore, like _2 or _x. The pruning step split the query with the regex \w+, which keeps underscores, and looked up each token’s document frequency to decide what to keep. _2 appears in zero documents as a token, so it was always kept, as the rarest word of all. Then FTS5 got the query, and its tokenizer splits on underscores: _2 became 2, which appears in about 2.5 million documents. Stage 1 then ranked up to 738,000 rows, against a budget of about 51,000, and took 400 to 665 ms. The median query matches 30,000 rows in 28 ms.

The fix is the regex [^\W_]+, word characters minus the underscore, which is what FTS5 does. Warm hybrid p99 went from 486 ms to 118, and the top 10 changed for 5 of the 500 queries. One character, 4x on the tail. The desktop research had blamed the tail on stage 2; the DGX probe showed the tail survived a 21x faster stage 2 untouched.

Fig. 1 One underscore

Three made-up titles with illustrative document frequencies; the budget is the real one, 2% of 2,549,119 rows, and the time scale is fitted to the two measured points. Pick a title and toggle the fix to see what each tokenizer keeps and how many rows FTS5 ends up ranking.

A few hundred lines of Rust

Stage 2 was the one real piece of Python work in the hot path, and the first recommendation from part 1’s midnight answer: move it to native code. The agents wrote the scorer and the tokenizer in Rust, as a small extension module with an environment switch back to Python. The requirement was parity, bit for bit: same ids in the same order and the same score bits as the Python scorer, on 500 queries. Zero mismatches. Python’s and Rust’s Unicode tables disagree on about 5,000 code points, and every one of them is unassigned, so no real text tokenizes differently.

Stage 2 went from 31 ms to 1.6 at the median, 19x. Warm hybrid p50 went from 77 ms to 46, and with 8 queries in flight throughput doubled, from 17.7 to 36.7 queries per second, with the p99 down from 1,047 ms to 441.

Fig. 2 Where a text query's time goes

The warm median of one text query by stage. The desktop estimate is what the research brief assumed; the two DGX bars are measured, with the Python scorer in the probe and the Rust one in plan E’s A/B. Hover a segment for its share.

The mutex inside SQLite

That doubling almost didn’t happen. The DGX probe that tested a prototype of the native scorer, on the program’s second day, ran on the image’s Debian Python with its SQLite 3.40.1, and a 21x faster stage 2 changed concurrent hybrid throughput by nothing: 0.99x, with a p99 of 7 seconds. The desktop brief had predicted the GIL would be the limit. The probe found SQLite instead: with no Python work at all, just the SQL, throughput fell from 36.5 queries per second at 2 threads to 16.8 at 8, with 89 seconds of system time and 4 million context switches. SQLite keeps memory statistics, and every allocation takes one global mutex to update them. Eight threads running FTS5 queries spent their time queuing on it. Turning the statistics off with a ctypes hack gave 3.6 to 4.1x, but there’s no supported way to turn them off in Python’s bundled SQLite.

So before this plan, plan A moved the image from Debian’s Python to a python-build-standalone one, which bundles SQLite 3.53.1. That alone took concurrent hybrid on the IVF data from 12.7 to 17.9 queries per second with the old Python scorer. And on 3.53.1 the native scorer gave its 2.07x. That build keeps the statistics too, and we never pinned down why it doesn’t queue the same way. The SQLite version goes on every row of every A/B now, because it turned out to be a performance variable on its own.

Fig. 3 The mutex inside SQLite

Top: hybrid queries per second with 8 in flight, by SQLite build and scorer. The dashed bar is a ctypes hack that switches the statistics off, not something you can ship. Bottom: the same SQL with no Python work at all, by thread count, on Debian’s build.

The loop at work

Two catches from this plan. The pre-flight read the plan’s first task, which keyed the pruning cache by each token as written, with this justification: folding isn’t idempotent, math bold 𝐀 folds to A, and A folds again to a. The fact is right, and it argues for the opposite choice. FTS5 lowercases query terms, so a lookup for AB finds a document frequency of 0, the token is always kept, and the underscore bug is back through another door. The agent reproduced it in a sandbox before ruling. Every step of the plan had been replayed green before that, because none of its tests used a character whose fold changes twice.

The second: an implementer reported that one of the plan’s test strings was wrong. Korean 한국어 should fold to decomposed jamo, it said, and the plan “carried precomposed syllables”. It didn’t. The plan held the jamo; an editing tool had recomposed the characters somewhere between the plan and the file on disk, so the “correction” was the plan’s own value. The coordinating agent then compared every added line with the brief, code point by code point, and found a second change no test could catch: one backslash dropped from a fuzz test’s character pool, so where the plan had a tab, the test had the letter t. The fuzz still passed, because it compares the Rust tokenizer with the Python one on whatever input it gets. It had just stopped testing the tab. Both lines were restored, and the rule is now: compare test literals with their source by code point, never by eye.

Part 5 in numbers

From the final A/B, the original code against the final one, flat index, 2.55 million vectors in a 4 GiB container:

MetricBeforeAfter
Hybrid throughput, serial10.0 q/s20.5 q/s
Hybrid throughput, 8 in flight14.0 q/s36.1 q/s
Hybrid p99, serial570 ms100 ms
Hybrid p99, 8 in flight3,545 ms471 ms

Next Monday

Part 6, November 16: the IVF fan-out. Sixteen shards, one thread, twenty OpenBLAS threads doing nothing, and a box with two kinds of cores.

Related Articles