Feedgarden

Browse

Switch Lowering in GCC

https://xoranth.net/gcc-switch/

Article

GCC lowers switch statements using different strategies depending on case density. For dense, contiguous or offset case values, it builds a jump table: the switch value is bounds-checked, then used as an index into an array of target addresses. Cases need not start at zero; a constant offset is subtracted first. GCC tolerates sparsity up to roughly eight jump-table slots per case label, so compact_7 with values 0, 7, 14, 21 and 34 still becomes a table whose gaps are filled by the default target. Beyond that threshold, as in sparse_8 with values 0, 8, 16, 24 and 40, GCC expands the switch as a balanced decision tree of comparisons, branching on midpoints such as 16 and 24. The post shows the generated x86-64 assembly and a visualization of the comparison tree. All optimizations except bitset lowering also apply to Clang, and the examples use -O3. The author notes that GCC can identify constant offsets and that a manual implementation would use a function-pointer array, which the compiler effectively reproduces.

HN discussion

Based on 9 selected comments and replies.

LarsKrimi observes that sparse_8 could be simplified by dividing x by eight, but notes this would route seven values per case to function calls instead of the default; rep_lodsb replies that since all values are divisible by eight, a modulo check could guard a jump table. rurban argues compilers should implement perfect hashing for large sparse switches, citing Unicode work, and says GCC and Clang still lack it. jcranmer counters that perfect hashing is rarely worth it because hot large sparse switches are uncommon and parse tables can be optimized by their generators. erk__ reports that LLVM once spent about half an hour compiling a huge switch of MAC-address manufacturers, while perfect hashing compiled much faster. zamalek notes decision trees can outperform jump tables in some cases, citing .NET HashCode's trailing bytes. tsukikage asks why the jump table targets jmp instructions rather than the functions directly; Findecanor explains the indirection likely comes from a later pass optimizing calls into jumps. Andi adds that similar generated dispatch in JavaScript brought no gain for immutable Maps and Sets, but roughly doubled speed for complex Unicode regular-expression ranges.

Selected comments: LarsKrimi · rep_lodsb · rurban · jcranmer · erk__ · tsukikage

Read the full HN discussion

Search this site

The index loads when search opens.

    On this page

    Image