# GCC turns switch statements into jump tables, binary search, or bit tests

Original title: Switch Lowering in GCC

> Hacker News story 50314085 · language: en · submitted 2026-10-31
> Web version: https://zeli.app/story/50314085
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 210 · by [signa11](<https://news.ycombinator.com/user?id=signa11>) · 💬 49
[original](<https://xoranth.net/gcc-switch/>) · [HN discussion](<https://news.ycombinator.com/item?id=50314085>)

## Summary

A deep dive into how GCC lowers switch statements, revealing jump tables for dense cases, comparison trees for sparse ones, and even bitset tests for non-contiguous labels. The author digs into the compiler source to explain the heuristics—like the eight-entries-per-case threshold—and shows why some seemingly profitable jump tables are skipped in favor of fewer comparisons.

## Key quote

> If we examine the binary representation of 2753205, we find that it is in fact a bitset. The bits that are set to one correspond to the ASCII values of the case labels, each value being reduced by 48.

## HN discussion (49 comments)

### [LarsKrimi](<https://news.ycombinator.com/item?id=50314563>)

> \> The switch in sparse_8 could be simplified by dividing x by eight before the jump. Interestingly, gcc doesn't find this simplification.
>
> It could not because that would ensure that 7 values for each case would be handled by function calls instead of the default case - 0/8 is 0, but 3/8 is also 0

### [rurban](<https://news.ycombinator.com/item?id=50314612>)

> What a shame for clang and gcc! They still don't implement perfect hashes for large sparse switch tables. I did that recently for my rcc C compiler, and it was just a single day of work. Esp. beneficial for unicode work. The LLVM patch was rejected a decade ago. Gcc has several unicode patches sitting in branches, which would also need that

### [p-t](<https://news.ycombinator.com/item?id=50317666>)

> I assume 34 in the compact_7() one was supposed to be 35, and edx was supposed to be used at some point in the bittest() one (unless i'm missing something lol)

### [tsukikage](<https://news.ycombinator.com/item?id=50314741>)

> Why the indirection? - why not have the jump table reference f0..fX directly rather than jumping to a jump?

### [Andi](<https://news.ycombinator.com/item?id=50314912>)

> I remember having implemented similar things in JavaScript (by generating code), for immutable Maps and Sets - no significant performance gain in modern engines. For regular expressions of complex unicode ranges, the speedup was around 2x!

---

## Cite

- Page: https://zeli.app/story/50314085
- Date: 2026-10-31
- Source: xoranth.net (https://xoranth.net/gcc-switch/)
- Hacker News: https://news.ycombinator.com/item?id=50314085

## Other languages

- zh: https://zeli.app/zh/story/50314085 · markdown: https://zeli.app/zh/story/50314085.md
- ja: https://zeli.app/ja/story/50314085 · markdown: https://zeli.app/ja/story/50314085.md
- ko: https://zeli.app/ko/story/50314085 · markdown: https://zeli.app/ko/story/50314085.md
- de: https://zeli.app/de/story/50314085 · markdown: https://zeli.app/de/story/50314085.md
- es: https://zeli.app/es/story/50314085 · markdown: https://zeli.app/es/story/50314085.md
- ru: https://zeli.app/ru/story/50314085 · markdown: https://zeli.app/ru/story/50314085.md
