Why BQN Wins
All BQN solutions outperformed everything else in this perf wars video, this file explains the optimizations BQN has which led it there. Comments
Here is a rewritten account of the story based on the provided source material:
The benchmark results demonstrate that CBQN achieves significantly faster performance compared to C++ and Rust when sorting and rotating boolean arrays. The key advantage lies in how CBQN represents booleans as packed bit arrays, using only 1 bit per element instead of 1 byte per boolean value. This allows for a 64x parallelism multiplier during operations, without the need for SIMD instructions.
In particular, the sort+rotate operation on a 1000-element boolean array in CBQN takes only 159ns, whereas similar operations in C++ and Rust take significantly longer (617-668ns). This is because CBQN avoids the overhead of comparing and swapping individual bytes. Instead, it counts the number of 1s using hardware POPC instructions, then fills the sorted array by directly setting 64-bit words.
For the direct solution of sorting a boolean array and then rotating it, CBQN stores the array as a packed 1-byte representation. When sorting, it utilizes a counting sort algorithm that leverages SIMD instructions to build a histogram in parallel. At a size of 1000 booleans, this results in a fast O(n) sorting operation with a small constant factor.
Other approaches in the benchmark, such as sorting the boolean array directly and then rotating, have a higher complexity of O(n log n). CBQN's implementation of the count solution achieves a sorting time of 151ns, which is nearly as fast as a single memcpy operation. The counting step and replication steps are optimized using SIMD instructions and constant replication, minimizing the number of instructions and memory accesses required.
Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.