Engineering posts
A cosmetic C rewrite makes a quicksort 6x faster by triggering branchless codegen
- Category: Engineering post
- Status: discussion
- Sources: Christof Kaser writeup, HN discussion
- Summary: Christof Kaser shows a branchless-quicksort partition loop where rewriting an
if/elsethat stores to and advances two pointers into a more compact idiom leads Clang to emit a conditional-select (csel) instead of a branch, cutting the time to sort 50 million doubles from 4.39 seconds to 0.70 seconds (about 6.3x) and running faster than C++std::sortat 1.33 seconds. GCC still generated branch-based code for the same source. The author frames the outcome as depending on whether the source happens to hit a compiler's optimization heuristics. - Why it matters: It shows how sensitive generated code can be to source phrasing a programmer treats as equivalent, so performance-critical loops can hinge on codegen heuristics that differ across compilers.