case study · systems · C++17 · dependency-free
columnar-engine
A vector-at-a-time analytical engine. The useful benchmark is not where vectorization wins. It's where the crossover happens, and why.
- rows per second on TPC-H Q1, 1,024-row batches, median of five
- 53.1M
- differential assertions, vector against an independent scalar reference
- 6,288
- best speedup on the chained filter + group-by pipeline
- 1.94×
- at 1,000 rows the vector path is slower: the mask costs more than it saves
- 0.87×
Mask, then compact. Watch NULL never match.
A JavaScript rendition of the engine's two-phase filter: compare a contiguous column into a byte mask, respecting the validity bitmap, then compact true lanes into row IDs. The second predicate walks only the surviving IDs. An independently written row-at-a-time loop checks every result, like the repo's differential oracle.
Hover or tap a configuration.
Selection vectors, not copied rows.
Filters return 32-bit physical row IDs. The next operator walks only those rows, input order is preserved, and every incoming ID is bounds-checked: a corrupt selection is an error, not an out-of-bounds read.
Validity bitmaps
Nullness lives beside the values, one bit per row packed into 64-bit words, so value arrays stay dense and typed. A comparison with NULL is unknown, and unknown is never selected. Inner joins never match null keys, not even null against null.
Why the vector path can lose
Each batch writes a byte mask, then rereads it to compact. That gives the compiler a straight numeric loop to vectorize, and it's also a cost a scalar loop never pays. At small inputs or very selective scans, the mask costs more than it saves. Both sides are reported.
TPC-H Q1, fused
The vector executor builds one validity/predicate mask per batch, then updates all six aggregate states in a single pass, without materializing an intermediate table.
| executor | time | rows/s |
|---|---|---|
| tuple-at-a-time | 23.51 ms | 42.5M |
| vector, 1,024 rows/batch | 18.85 ms | 53.1M |
That's 1.25×, smaller than the filter pipeline's best. Q1 spends more time on arithmetic and hash-table work per surviving row, so scan dispatch is a smaller share of the total.
| check | result |
|---|---|
| correctness assertions | 6,288 |
| random filter/aggregate trials | 120 |
| random join trials | 100 |
| batch sizes per filter trial | 1, 7, 64, 1,024 |
| scalar/vector disagreements | 0 |
Vectorization is a trade, and the table shows both sides.
- Small inputs loseAt 1,000 rows the vector path runs at 0.87× and at 10,000 rows with 7% survival at 0.93×. The mask and compaction pass cost more than they save.
- Selective scans cross over lateAt a million rows with 7.4% survival the gain is only 1.10×. Few surviving rows leave little work to amortize the mask over.
- Q1 gains less than filters1.25× on TPC-H Q1 against 1.94× on the filter pipeline, because arithmetic and hashing dominate once rows survive.
- One core, one machineSingle-threaded medians on an M2 Pro. No parallel scan, no SIMD intrinsics written by hand: what the compiler auto-vectorizes is what you get.
git clone https://github.com/asp53826/columnar-engine cd columnar-engine make test # 6,288 assertions make benchmark # CSV, then the Q1 result