conv.

All stories
TechQuiet 12d · day 13

Blog post traces how GCC lowers switch statements beyond jump tables

A detailed technical writeup dissects GCC's switch-statement optimizations, including bitset-based lowering, and gains traction on Hacker News and Lobsters.

What to know

  • GCC can lower a switch statement into a jump table, a branch (comparison) sequence, or a bitset test, choosing based on an internal profitability heuristic.
  • The post finds the jump-table threshold is roughly eight table entries per case label, and that the heuristic actually counts comparisons rather than raw case labels when cases are contiguous.
  • A worked example shows gcc emitting a bit-test instruction keyed to a constant (2753205) that encodes ASCII case values as a bitset instead of using cmp or jump-table code.
  • The article walks through GCC's actual source (pass_lower_switch, analyze_switch_statement) to explain the mechanism, and has propagated from Lobsters to Hacker News's front page with no substantive discussion yet in the evidence.

xoranth.net author Blog authorGCC (GNU Compiler Collection) developers Compiler maintainers

How it unfolded 3 developments, newest first · click a bar or a number to jump articlesposts

Peak 8 pieces in 3h at Oct 31, 3 AM; 17 pieces over 13 days (2 articles · 5 posts · 10 comments) Oct 30, 3 PM — 1 piece · 1 post — Lobsters 1Oct 30, 6 PM — quietOct 30, 9 PM — quietOct 31, 12 AM — quietOct 31, 3 AM — 8 pieces · 2 articles · 3 posts · 3 comments — Hacker News 4, Newswires 2, Mastodon 2Oct 31, 6 AM — 4 pieces · 4 comments — Hacker News 4Oct 31, 9 AM — 1 piece · 1 post — Mastodon 1Oct 31, 12 PM — 2 pieces · 2 comments — Hacker News 2Oct 31, 3 PM — quietOct 31, 6 PM — 1 piece · 1 comment — Hacker News 1Oct 31, 9 PM — quietNov 1, 12 AM — quietNov 1, 2 AM — quietNov 1, 5 AM — quietNov 1, 8 AM — quietNov 1, 11 AM — quietNov 1, 2 PM — quietNov 1, 5 PM — quietNov 1, 8 PM — quietNov 1, 11 PM — quietNov 2, 2 AM — quietNov 2, 5 AM — quietNov 2, 8 AM — quietNov 2, 11 AM — quietNov 2, 2 PM — quietNov 2, 5 PM — quietNov 2, 8 PM — quietNov 2, 11 PM — quietNov 3, 2 AM — quietNov 3, 5 AM — quietNov 3, 8 AM — quietNov 3, 11 AM — quietNov 3, 2 PM — quietNov 3, 5 PM — quietNov 3, 8 PM — quietNov 3, 11 PM — quietNov 4, 2 AM — quietNov 4, 5 AM — quietNov 4, 8 AM — quietNov 4, 11 AM — quietNov 4, 2 PM — quietNov 4, 5 PM — quietNov 4, 8 PM — quietNov 4, 11 PM — quietNov 5, 2 AM — quietNov 5, 5 AM — quietNov 5, 8 AM — quietNov 5, 11 AM — quietNov 5, 2 PM — quietNov 5, 5 PM — quietNov 5, 8 PM — quietNov 5, 11 PM — quietNov 6, 2 AM — quietNov 6, 5 AM — quietNov 6, 8 AM — quietNov 6, 11 AM — quietNov 6, 2 PM — quietNov 6, 5 PM — quietNov 6, 8 PM — quietNov 6, 11 PM — quietNov 7, 2 AM — quietNov 7, 5 AM — quietNov 7, 8 AM — quietNov 7, 11 AM — quietNov 7, 2 PM — quietNov 7, 5 PM — quietNov 7, 8 PM — quietNov 7, 11 PM — quietNov 8, 2 AM — quietNov 8, 5 AM — quietNov 8, 8 AM — quietNov 8, 11 AM — quietNov 8, 2 PM — quietNov 8, 5 PM — quietNov 8, 8 PM — quietNov 8, 11 PM — quietNov 9, 2 AM — quietNov 9, 5 AM — quietNov 9, 8 AM — quietNov 9, 11 AM — quietNov 9, 2 PM — quietNov 9, 5 PM — quietNov 9, 8 PM — quietNov 9, 11 PM — quietNov 10, 2 AM — quietNov 10, 5 AM — quietNov 10, 8 AM — quietNov 10, 11 AM — quietNov 10, 2 PM — quietNov 10, 5 PM — quietNov 10, 8 PM — quietNov 10, 11 PM — quietYesterday, 2 AM — quietYesterday, 5 AM — quietYesterday, 8 AM — quietYesterday, 11 AM — quietYesterday, 2 PM — quietYesterday, 5 PM — quietYesterday, 8 PM — quietYesterday, 11 PM — quietToday, 2 AM — quietToday, 5 AM — quietToday, 8 AM — quietToday, 11 AM — quietToday, 2 PM — quietToday, 5 PM — quiet 12–3
Oct 31Nov 1Nov 2Nov 3Nov 4Nov 5Nov 6Nov 7Nov 8Nov 9Nov 10now · 8:19 PM ET
  1. 3

    Automated accounts mirror the HN submission on Mastodon

    Bot accounts (h4ckernews@mastodon.social and hkrn@mstdn.social) repost the Hacker News link and title to Mastodon shortly after the HN submission.

    • > The compiler is able to implement them as a decision tree (even though maybe it shouldn't)Sometimes a decision tree is definitely faster. When developing System.HashCode, I used a switch (which I think always compiles to a jump table in .Net - at least my case did back then). This was for the trailing non-block sized bytes, I think. It was…

      zamalekHacker News12d agoview on Hacker News ↗
    2 more of the top 3 · 12 posts in this stretch
    • hkrn@mstdn.social

      Switch Lowering in GCC L: https:// xoranth.net/gcc-switch/ C: https:// news.ycombinator.com/item?id=5 0314085 posted on 2026.10.31 at 04:02:26 (c=0, p=4)

      hkrn@mstdn.socialMastodon12d agoview on Mastodon ↗
    • I once played around with having a very large switch statement (all mac address manufactors in one switch) and in LLVM it would compile fine without optimizations, but if I turned optimizations on it would just spend around half an hour compiling it. I think GCC did it faster, but its been some time ago since I did it. In the Rust crate that was…

      erk__Hacker News12d agoview on Hacker News ↗
    all of them →
  2. 2

    Post reaches Hacker News front page

    The same article is submitted to Hacker News by signa11 and appears on the HN front page RSS feed, with an associated comments thread.

  3. 1

    Blog post on GCC switch lowering posted to Lobsters

    The xoranth.net article explaining GCC's switch-statement lowering strategies (jump tables, branch sequences, bitset tests) is submitted to Lobsters by user jo3_l.

    “gcc considers the threshold to be around eight jump table entries per case label.”
    — xoranth.net (blog author), blog author · source
    1. first by HN Best, 12d ago · also HN Frontpage

What people are saying 9 voices from 2 sites · best of 12 · verbatim