Tech
Branchless Filtering Speeds Up Number Slice Filtering

The author, greyblake, was optimizing a hot path in code that filters a slice of numbers greater than a threshold, a common database problem. Benchmarks on an Intel i7-10875H laptop with one million random f64 values showed that the 50% filter case was the slowest, despite copying only half the data, because unpredictable branches caused CPU pipeline mispredictions costing 15-20 cycles each. Sorting the input made the same code faster, but sorting is not a practical fix.
Instead, the branchless approach always writes the element and uses the comparison result as a number to decide where to place it, turning a control dependency into a data dependency. The worst case improved from 3.87 ms to about 1 ms, and performance became flat regardless of data distribution. The trade-off is that the branchless version always writes, potentially increasing memory traffic, but the speed gain is significant.
Source: Hacker News