GCC 14 Quietly Turned Your Computed Goto Back Into a Switch. musttail Doesn't Let It.
Published: October 17, 2026 | Compilers & Toolchains CPU & Microarchitecture Networking & Market Data
GCC 14 Quietly Turned Your Computed Goto Back Into a Switch. musttail Doesn't Let It.
Every interpreter writer knows the folklore. A switch loop has one indirect branch at the top, the predictor cannot learn a branch with twenty-six targets, so you use computed goto and give the predictor one indirect branch per handler; the history at each site is simpler and the mispredicts go away. It is why CPython moved to goto * in 2008, why every fast WebAssembly interpreter tail-calls, and why the CPython 3.14 team rewrote their interpreter as 225 [[clang::musttail]] functions. It is also why, when a FIX parser is slow, someone eventually suggests "threaded dispatch."
Here is the folklore, in code, on a FIX 4.4 field scanner. Twenty-six handlers, one per tag; the handler bodies are identical across every version below and only the dispatch differs. The one everybody writes:
extern "C" size_t scan_switch(const uint8_t* p, const uint8_t* end, Msg* out, size_t cap) {
Ctx c{out, out + cap, {}};
while (p < end) {
unsigned tag = parse_tag(p); // digits up to '=', consumes '='
switch (tag) {
case 8: op_begin(p, c); break; // BeginString
case 9: op_bodylen(p, c); break; // BodyLength
case 35: op_msgtype(p, c); break; // MsgType
case 34: op_seq(p, c); break; // MsgSeqNum
case 44: op_price(p, c); break; // Price -> int64 ticks
case 38: op_qty(p, c); break; // OrderQty
case 10: op_checksum(p, c); break; // CheckSum: commit the record
// ... 19 more
default: op_unknown(p, c); break; // skip to SOH, count it
}
}
return size_t(c.out - out);
}
The one interpreter people write, GNU computed goto: a 256-slot table of label addresses, and every handler ends in its own indirect jump.
extern "C" size_t scan_goto(const uint8_t* p, const uint8_t* end, Msg* out, size_t cap) {
static void* tbl[256];
static bool init = false;
if (!init) {
for (auto& e : tbl) e = &&L_unknown;
tbl[8] = &&L_begin; tbl[9] = &&L_bodylen; tbl[35] = &&L_msgtype;
tbl[44] = &&L_price; tbl[38] = &&L_qty; tbl[10] = &&L_checksum; /* ... */
init = true;
}
Ctx c{out, out + cap, {}};
#define NEXT() do { if (p >= end) goto L_done; goto *tbl[parse_tag(p)]; } while (0)
NEXT();
L_begin: op_begin(p, c); NEXT();
L_bodylen: op_bodylen(p, c); NEXT();
L_msgtype: op_msgtype(p, c); NEXT();
L_price: op_price(p, c); NEXT();
L_qty: op_qty(p, c); NEXT();
L_checksum: op_checksum(p, c); NEXT();
// ... 20 more labels
L_unknown: op_unknown(p, c); NEXT();
L_done:
return size_t(c.out - out);
}
The one CPython 3.14 switched to: every handler is a function with the same signature, the table holds function pointers, and the dispatch is a tail call that the compiler must turn into a jump or refuse to compile.
typedef void (*Fn)(const uint8_t* p, const uint8_t* end, Ctx* c);
static Fn tbl[256];
#define TNEXT() do { if (p >= end) return; \
[[clang::musttail]] return tbl[parse_tag(p)](p, end, c); } while (0)
static void t_price(const uint8_t* p, const uint8_t* end, Ctx* c) { op_price(p, *c); TNEXT(); }
static void t_qty (const uint8_t* p, const uint8_t* end, Ctx* c) { op_qty(p, *c); TNEXT(); }
// ... one function per tag, plus t_unknown
extern "C" size_t scan_tail(const uint8_t* p, const uint8_t* end, Msg* out, size_t cap) {
/* fill tbl once, as above */
Ctx c{out, out + cap, {}};
if (p < end) tbl[parse_tag(p)](p, end, &c); // enter the chain; it returns at end
return size_t(c.out - out);
}
And the CPython refinement: the same chain with preserve_none on every handler and on the table's pointer type (a musttail call must match calling conventions exactly, or Clang refuses).
#define PN __attribute__((preserve_none))
typedef PN void (*Fn)(const uint8_t* p, const uint8_t* end, Ctx* c);
static Fn tbl[256];
static PN void t_price(const uint8_t* p, const uint8_t* end, Ctx* c) { op_price(p, *c); TNEXT(); }
For completeness there is a fifth, subroutine threading, a table of ordinary function pointers called in a loop, with the handler returning the advanced cursor so it never lives in memory:
typedef const uint8_t* (*Fn)(const uint8_t* p, Ctx* c);
static const uint8_t* f_price(const uint8_t* p, Ctx* c) { op_price(p, *c); return p; }
// ...
while (p < end) p = tbl[parse_tag(p)](p, &c);
Same handler bodies, same tag parser, same 256 KiB batch of NewOrderSingle and ExecutionReport messages, same compiler. On aorus (Threadripper 3960X, Zen 2, isolated core, clang++-20 -O3 -march=native): the switch loop takes 133.2 ns per message. Computed goto: 127.5. The musttail chain: 118.5, with preserve_none 118.1. That is 11% under the switch and 7% under computed goto, medians of eleven samples, every variant verified field-by-field against generator ground truth before a single timing was recorded. Same ordering on a second Zen 2 box (hz2), on Zen 5, on a Comet Lake i7 with GCC 16, and on an Apple M2 where the gap widens to 19%.
So far the folklore is holding: more dispatch sites, faster. Then we put perf stat on it. Every variant, including a GCC 14 build whose computed goto had been collapsed back into a single indirect jump, takes 0.13 branch mispredicts per field. Identical, to two digits. The musttail chain wins for a duller reason than the folklore, and the duller reason changes what you should actually do.
And that GCC 14 build is the second story. Count the indirect jumps left in the compiled computed-goto scanner: Clang keeps all 53. GCC 13 keeps 10. GCC 14 at -O3 keeps one. It has quietly rebuilt your threaded code as a switch with extra steps, the folklore flags (-fno-gcse -fno-crossjumping) do nothing about it, and the result is slower than the switch you were avoiding. GCC 15 and 16 keep 11. A musttail handler is a function; nothing can merge its tail into another handler's tail. That, more than the 11%, is the argument.
Where the claim comes from
CPython 3.14 ships a "tail-calling interpreter": every bytecode handler is its own C function, declared preserve_none, ending in [[clang::musttail]] return next_handler(...). Ken Jin's EuroPython 2025 talk, written up by LWN as "Python, tail calls, and performance" (Jake Edge, August 2025), is worth reading for the part where the initial 15% speedup turned out to be a compiler bug in the baseline: Clang had factored the common goto *DISPATCH_TABLE[*ip] out of every handler into one shared jump label, so the computed-goto interpreter was doing two jumps per instruction. Corrected against a proper baseline the tail-call interpreter is about 2% faster on x86-64 Linux. Jin's summary: "the compiler can probably outsmart us, even in the case of normal computed goto."
Keep that failure mode in mind. We are going to see it again, in GCC, on purpose.
The claims that survive are: musttail makes the tail call a compile error if it cannot be a jump; preserve_none removes the callee-saved register saves at every dispatch edge; the interpreter is 225 small functions instead of one 12,000-line function, which is kinder to the register allocator and to compiler bugs; perf can finally see individual handlers; and it needs Clang 19+ or GCC 16. A comment on that LWN article from the Wasmi maintainer (August 2026) reports an "enormous" difference between switch-loop and threaded dispatch for a WebAssembly interpreter, which is why every fast Wasm interpreter tail-calls.
Those are interpreters with fat handlers. A FIX field scanner is a state machine with skinny handlers: parse some digits, store an integer, move on. Whether the CPython result transfers to that is exactly the question, and it is the question a feed-handler or OMS author would ask before restructuring a parser around a Clang-19-only attribute.
What the compilers actually accept
Before writing a line of benchmark we probed every compiler on the fleet with a two-handler tail chain, -Werror=attributes on, so an ignored attribute fails instead of warning:
musttail |
preserve_none (x86-64) |
|
|---|---|---|
| GCC 13, 14 | silently ignored (warning under -Wattributes) |
ignored |
| GCC 15 | [[gnu::musttail]] accepted |
ignored |
| GCC 16 (trunk 2026-03-15 snapshot) | accepted | accepted, and it changes codegen |
| Clang 18 | [[clang::musttail]] accepted |
unknown attribute |
| Clang 20 | accepted | accepted |
| Apple Clang 21 (AArch64) | accepted | accepted |
Two notes. GCC 16's changes page lists preserve_none under AArch64 only; the x86-64 support is real (a six-push handler becomes a one-push handler in the probe) but you find out by compiling, not by reading. And "GCC 16" here is a March 2026 trunk snapshot from the Ubuntu toolchain PPA, not a release.
The experiment
One FIX 4.4 field scanner, one Msg record per message, seven dispatch mechanisms wrapped around the same 26 inline handler bodies so that only the dispatch edge differs:
switch: the loop above; both GCC and Clang emit a jump table for it.switch_dense: tag mapped through a 256-byte table to a dense index 0..26, then switch: forces a jump table on every compiler, at the price of one dependent load per field.goto: computed goto, 256-slot label table.goto_nm: same source, built with the CPython folklore flags-fno-gcse -fno-crossjumpingon GCC (a plain rebuild on Clang).call: 256-slot function-pointer table,p = tbl[tag](p, &c)in a loop: subroutine threading, call/ret per field.tail: musttail chain, default SysV calling convention.tail_pn: musttail chain,preserve_noneon every handler and on the table's pointer type.
The handler bodies are static inline functions shared by every variant, one per tag, generated from an X-macro so the seven wrappers cannot drift:
struct Msg {
uint64_t sender_h, target_h, clordid_h, orderid_h, execid_h;
uint64_t symbol, sending_time, transact_time;
int64_t price, last_px, avg_px;
uint32_t body_len, seq, qty, last_qty, leaves, cum_qty, checksum;
uint16_t msg_type;
uint8_t side, ord_type, tif, exec_type, ord_status, begin_ok;
uint32_t nfields, nunknown;
};
struct Ctx { Msg* out; Msg* out_end; Msg cur; };
#define OP(name) static inline __attribute__((always_inline)) \
void op_##name(const uint8_t*& p, Ctx& c)
OP(qty) { FxR r = fx_uint(p); p = r.p; c.cur.qty = r.v; ++c.cur.nfields; }
OP(price) { FxR r = fx_price(p); p = r.p; c.cur.price = int64_t(r.v); ++c.cur.nfields; }
OP(clordid) { FxR r = fx_hash(p); p = r.p; c.cur.clordid_h = r.v; ++c.cur.nfields; }
OP(side) { c.cur.side = fx_char(p); ++c.cur.nfields; } // one char + SOH
OP(symbol) { c.cur.symbol = fx_symbol(p); ++c.cur.nfields; } // pack <= 8 bytes
OP(unknown) { fx_skip(p); ++c.cur.nunknown; ++c.cur.nfields; }
OP(checksum) {
FxR r = fx_uint(p); p = r.p; c.cur.checksum = r.v; ++c.cur.nfields;
if (c.out < c.out_end) *c.out++ = c.cur; // commit the record
std::memset(&c.cur, 0, sizeof c.cur);
}
#define FIX_OPS(X) \
X(unknown, 0) X(begin, 8) X(bodylen, 9) X(msgtype, 35) X(seq, 34) \
X(sender, 49) X(target, 56) X(sendtime, 52) X(clordid, 11) X(symbol, 55) \
X(side, 54) X(qty, 38) X(ordtype, 40) X(price, 44) X(tif, 59) \
X(transtime, 60) X(checksum, 10) X(exectype, 150) X(ordstatus, 39) X(execid, 17) \
X(orderid, 37) X(lastqty, 32) X(lastpx, 31) X(leaves, 151) X(cumqty, 14) X(avgpx, 6)
so the switch body is literally #define X(name, t) case t: op_##name(p, c); break; followed by FIX_OPS(X), the goto body is #define X(name, t) L_##name: op_##name(p, c); NEXT();, and the tail chain is
#define X(name, t) \
static PN void t_##name(const uint8_t* p, const uint8_t* end, Ctx* c) { op_##name(p, *c); TNEXT(); }
FIX_OPS(X)
The tag parser is the same six lines everywhere. Slot 0 of every table is the unknown-tag handler (FIX has no tag 0), and tags ≥ 256 clamp to it, which is how the ExecutionReport's Parties group (447/448/452/453) gets skipped:
static inline unsigned parse_tag(const uint8_t*& p) {
unsigned t = 0;
while (*p != '=') { t = t * 10 + unsigned(*p - '0'); ++p; }
++p;
return t < 256 ? t : 0;
}
Every variant is built twice: lean, with the four value parsers (uint, decimal price to ticks, FNV hash, UTC timestamp) inlined into the handlers, and fat, with those four parsers as noinline externs in a separate translation unit compiled without LTO. Fat is the CPython situation in miniature: every handler now has an opaque call in the middle, so its live state has to survive that call. The parsers take the cursor by value and return {value, cursor} in RAX:RDX, so the cursor never touches memory in either mode; the first draft passed const uint8_t** and turned the whole thing into a store-forwarding benchmark. That is the second time this year a "how does the compiler handle X" experiment nearly published a memory pipeline artefact as a codegen result.
struct FxR { uint64_t v; const uint8_t* p; }; // returned in RAX:RDX
// lean: #define FX_LINK static inline __attribute__((always_inline))
// fat: #define FX_LINK extern "C" __attribute__((noinline)) (separate TU, no LTO)
FX_LINK FxR fx_uint(const uint8_t* p) {
uint64_t v = 0;
while (*p != SOH) { v = v * 10 + uint64_t(*p - '0'); ++p; }
return {v, p + 1};
}
FX_LINK FxR fx_price(const uint8_t* p) { // "4512.25" -> 45122500 (1e-4 ticks)
int64_t v = 0; bool neg = false;
if (*p == '-') { neg = true; ++p; }
while (*p != SOH && *p != '.') { v = v * 10 + (*p - '0'); ++p; }
int frac = 0;
if (*p == '.') { ++p; while (*p != SOH) { if (frac < 4) { v = v * 10 + (*p - '0'); ++frac; } ++p; } }
while (frac < 4) { v *= 10; ++frac; }
return {uint64_t(neg ? -v : v), p + 1};
}
The batch is 1,263 messages, 26,728 fields, ~207 bytes per message, with ExecutionReports carrying a Parties repeating group (tags 447/448/452/453) so the unknown-tag slot gets exercised. 256 KiB so it sits in L2, which is where a message is when a real parser sees it; a 16 MiB streaming pass as a control. Five machines, eleven samples each, every timing mode gated on a verify pass that checks message type, sequence, quantity, price ticks, side, symbol, ClOrdID hash, timestamp, field count and unknown count against the generator, and byte-compares every variant's output against the first.
Flags -O3 -march=native -std=c++20 -fcf-protection=none. That last one matters: Ubuntu's GCC turns on CET by default (an endbr64 at every indirect target, notrack jmp through jump tables) and Ubuntu's Clang does not, so without pinning it off a GCC-vs-Clang comparison of indirect dispatch is partly a CET benchmark.
Lean handlers: musttail wins everywhere, by about the same amount
Median ns per message, ratio to plain switch of the same compiler in brackets:
| machine | compiler | switch | switch_dense | goto | goto_nm | call | tail | tail_pn |
|---|---|---|---|---|---|---|---|---|
| aorus, Zen 2 | clang 20 | 133.2 | 133.8 [1.00] | 127.5 [0.96] | 127.1 [0.95] | 149.1 [1.12] | 118.5 [0.89] | 118.1 [0.89] |
| hz2, Zen 2 | clang 20 | 183.8 | 183.9 [1.00] | 175.6 [0.96] | 175.2 [0.95] | 206.5 [1.12] | 163.3 [0.89] | 161.5 [0.88] |
| Zen 5 | clang 20 | 119.1 | 121.3 [1.02] | 116.4 [0.98] | 116.4 [0.98] | 118.2 [0.99] | 105.7 [0.89] | 105.3 [0.88] |
| i7-10700K, Comet Lake | gcc 16 | 106.2 | 120.4 [1.13] | 103.1 [0.97] | 105.4 [0.99] | 91.2 [0.86] | 93.0 [0.88] | 93.1 [0.88] |
| Apple M2 | Apple clang 21 | 111.5 | 99.0 [0.89] | 104.6 [0.94] | 103.5 [0.93] | 101.8 [0.91] | 89.9 [0.81] | 89.6 [0.80] |

The tail chain is 11-12% under the switch on three AMD boxes spanning two generations, 12% on Intel with GCC 16, 19% on the M2. tail_pn is indistinguishable from tail in lean mode, which is what it should be: nothing is live across a call, so there is nothing for preserve_none to stop saving. Computed goto is 2-6% under the switch on Clang, a respectable second. And note call on the Comet Lake: a call/ret per field is the fastest dispatch on that core with GCC 16, 12% slower than switch on Zen 2. Do not generalise "subroutine threading is slow" from a machine you do not own.
The ordering and the size of the win survive the 16 MiB streaming pass on every machine. The scanner consumes 1.5-2 GB/s; DRAM does not enter into it.
It is not the branch predictor
The lean-mode ordering is exactly what threaded-code folklore predicts, so the obvious explanation is the folklore one: one shared indirect branch in the switch versus one per handler in goto and tail. Here is perf stat on aorus, clang 20, per field, over two seconds of each kernel:
| kernel | instr/field | cycles/field | branches/field | mispredicts/1000 fields | stores/field | loads/field |
|---|---|---|---|---|---|---|
| switch | 89.9 | 28.1 | 13.6 | 132 | 3.16 | 12.4 |
| switch_dense | 83.3 | 27.4 | 11.4 | 132 | 3.90 | 15.9 |
| goto | 84.9 | 25.5 | 12.2 | 132 | 3.22 | 14.9 |
| call | 74.5 | 29.0 | 12.5 | 132 | 3.73 | 17.5 |
| tail | 70.9 | 23.7 | 10.5 | 128 | 1.69 | 12.7 |
| tail_pn | 70.9 | 23.5 | 10.5 | 122 | 1.68 | 12.7 |
| gcc 14 goto (one dispatch site) | 91.6 | 27.3 | 12.5 | 130 | 2.63 | 17.0 |

Every variant mispredicts about once every eight fields, and it is the same once every eight fields whether there is one dispatch site or fifty-three. Those misses are the data-dependent digit loops (variable-length numbers, variable-length strings), not the dispatch. On a Zen 2 predictor, and on everything newer we tried, the "shared indirect branch" penalty that made threaded code a win on 2005 hardware is gone. Rohou, Swamy and Seznec said so in 2015 ("Branch prediction and the performance of interpreters: don't trust folklore") and this is one more data point.
What differs is instruction count and, above all, store count. The switch and goto scanners execute 85-90 instructions per field and perform 3.2 stores; the tail chain executes 71 and performs 1.7. Look at the disassembly of the goto function and the reason is right there: Clang has scalarised the Msg cur accumulator (thirty-odd fields) into thirty-odd SSA values inside one 9 KB function with 26 handler bodies and a loop, and under that register pressure most of them live in stack slots. Every handler's "store the parsed value" is a stack store, and the commit at tag 10 is a fifty-line sequence copying scalars out of -0x18(%rsp), -0x10(%rsp), -0x8(%rsp) into the output record. In the tail chain each handler is a 40-instruction function that receives Ctx* in a register, does exactly one field store and one incl on the field counter through that pointer, computes the next tag, and jumps. Twenty-six small register-allocation problems instead of one large one.
That is Ken Jin's second argument, the one that survived the baseline correction: "the interpreter is too big, and you hit magic thresholds in the compiler." It is not a branch-prediction effect. It is a code-quality effect that happens to correlate with the number of dispatch sites, because the way you get more dispatch sites is by splitting the function.
Which means the mechanism does not depend on the workload having a bad predictor profile. It depends on the monolithic version being large enough to spill. Twenty-six handlers and a 136-byte record is enough.
GCC and computed goto: the site count is a lottery, and GCC 14 loses it
Here is the finding in the title, the one that turned "musttail is 7% faster than goto" into "you should not be using computed goto on GCC in the first place." Count the indirect jmp * instructions in the compiled goto object, which is the number of dispatch sites that survived the optimiser:
| compiler | flags | dispatch sites |
|---|---|---|
| Clang 18, 20 | -O3 |
53 |
| GCC 13 | -O3 |
10 |
| GCC 13 | -O3 -fno-gcse -fno-crossjumping |
53 |
| GCC 14 | -O3 |
1 |
| GCC 14 | -O3 -fno-gcse -fno-crossjumping |
1 |
| GCC 14 | -O2 |
52 |
| GCC 14 | -O3 -fno-tree-loop-if-convert |
52 |
| GCC 15, 16 (trunk) | -O3 |
11 |
| Apple Clang 21 | -O3 |
13 |

The source has 26 handlers and 27 goto *tbl[...] sites. Clang keeps them all and duplicates the tag-parse loop's exit paths on top. GCC 13 merges most of them (10 left) and the folklore flags restore all 53. GCC 14 at -O3 merges every one of them into a single shared dispatch block, exactly the "compiler adds a second jump back in" bug that made CPython's computed-goto baseline 6-7% slow, and -fno-gcse -fno-crossjumping, the flags every interpreter README tells you to add, do nothing about it. It is an -O3 pass, not gcse: -O2 keeps 52, and so does -fno-tree-loop-if-convert at -O3. GCC 15 and 16 land somewhere in between at 11.
The numbers follow. On GCC 14 the computed-goto scanner is 5% slower than the switch in lean mode on Zen 2, 2% slower on hz2, 22% slower on Comet Lake. Zen 5 reads 6%, inside the spread of its GCC 14 cells on that laptop, so it agrees in direction and no more. The goto_nm build with the folklore flags is slower still (10%, 5%, 40%, and 18% on Zen 5, which does clear its spread), because the flags disable real optimisations without restoring the dispatch sites. In fat mode on Zen 2 the goto is 15-16% slower than switch and goto_nm 24-25%; on Zen 5, 12% and 21%. On GCC 13 the folklore flags do work and goto_nm is 12% faster than the switch. So the same source, same flags, same machine, moves from best to worst dispatch by changing GCC 13 to GCC 14, and back to middling on 15.
Musttail handlers do not have this problem, structurally: they are separate functions, so there is one dispatch site per handler by construction, and no pass can merge the tail of t_price into the tail of t_qty because they are not in the same function. This is the strongest argument for musttail on GCC even though (see the perf table) the site count is not why the tail chain is faster here. It is the argument that your dispatch structure survives the next compiler upgrade.
Fat handlers: preserve_none is real, and it is worth about 5%
Now the CPython case proper: an opaque call inside every handler, so end and ctx (the cursor comes back in RDX) have to survive it. This is the t_price handler in the default calling convention, clang 20:
t_price:
push %r14
push %rbx
push %rax ; 16-byte alignment for the call
mov %rdx,%rbx ; ctx -> callee-saved
mov %rsi,%r14 ; end -> callee-saved
call fx_price ; returns {ticks, cursor} in rax:rdx
mov %rax,0x50(%rbx) ; c->cur.price
incl 0x8c(%rbx) ; c->cur.nfields++
... ; parse next tag into %rax
mov %r14,%rsi
mov %rbx,%rdx
mov (%rcx,%rax,8),%rax
add $0x8,%rsp
pop %rbx
pop %r14
jmp *%rax
Three pushes and three pops per field, plus the register shuffle to get the arguments back where the next handler expects them. Same handler with preserve_none:
t_price:
push %rax ; alignment slot only
mov %r12,%rdi ; cursor arrives in r12
call fx_price
mov %rax,0x50(%r14) ; ctx arrived in r14 and is still there
incl 0x8c(%r14)
... ; parse next tag, cursor in %rdx
mov %rdx,%r12
mov (%rcx,%rax,8),%rax
pop %rcx
jmp *%rax
The trick is in the register choice. preserve_none passes its first arguments in r12, r13, r14, r15, the registers that are callee-saved in the ordinary SysV convention the helper was compiled with. So end in r13 and ctx in r14 ride through call fx_price untouched, the handler owes nothing to its caller because it has no callee-saved registers, and the only stack traffic left is the alignment slot the call demands. GCC 16 emits the same shape (sub $8,%rsp … add $8,%rsp; jmp *%rax).
Fat handlers, median ns per message, ratio to plain switch:
| machine | compiler | switch | goto | call | tail | tail_pn |
|---|---|---|---|---|---|---|
| aorus, Zen 2 | clang 20 | 141.9 | 141.3 [1.00] | 151.1 [1.06] | 132.3 [0.93] | 126.1 [0.89] |
| hz2, Zen 2 | clang 20 | 194.6 | 195.2 [1.00] | 206.9 [1.06] | 181.9 [0.93] | 173.9 [0.89] |
| Zen 5 | clang 20 | 127.4 | 114.6 [0.90] | 129.3 [1.02] | 118.3 [0.93] | 115.3 [0.90] |
| i7-10700K | gcc 16 | 112.6 | 109.2 [0.97] | 101.8 [0.90] | 102.9 [0.91] | 101.4 [0.90] |
| Apple M2 | Apple clang 21 | 113.2 | 99.8 [0.88] | 113.5 [1.00] | 94.9 [0.84] | 94.2 [0.83] |

Three things. First, the default-convention tail chain, three push/pop pairs per field and all, is still faster than computed goto and switch on Zen 2, Comet Lake and M2; only on Zen 5 does computed goto edge it, by 3.1% (114.6 against 118.3), and tail_pn closes that to 0.6%. Treat the Zen 5 row gently: that box is a laptop with no core isolation, those two cells carry IQRs of 1.2% and 1.4%, and other cells in the same table spread by 4%. A three percent ordering on that machine is a hint, not a result. The register-allocation win from small functions is bigger than the calling-convention cost. Our prediction file had tail losing 15-30% to goto here; it was wrong. Second, preserve_none buys a further 4-5% on Zen 2, 1.4% on Comet Lake with GCC 16, and 2% on Zen 5. That is what three fewer push/pop pairs per field is worth on a store-port-limited core, and it is the CPython mechanism, reproduced. Third, on the M2 it buys 0.7%, and the census of the AArch64 objects says why: a handler that contains a bl has to save the link register (stp x29, x30, [sp, #-16]!) no matter what convention it was declared with, so preserve_none can strip the callee-saved pair but not the frame. Apple silicon's calling convention already had more caller-saved registers than SysV x86-64; the attribute has less to remove.
The GCC ≤ 14 trap is optimisation-level dependent, and worse than "you lose the guarantee"
GCC 13 and 14 do not know [[gnu::musttail]]. They warn and carry on:
k_tail.cpp:38:5: warning: attributes at the beginning of statement are ignored [-Wattributes]
38 | [[gnu::musttail]] return tbl[parse_tag(p)](p, end, c);
| ^~~~~~~~~~~~~~~~~
Twenty-six of those, one per handler, scrolling past in a build log. So we forced the tail chain to build on GCC 14 and counted what it emitted:
| gcc-14 flags | jmp * |
call * |
|---|---|---|
-O0, -Og, -O1 |
0 | 27 |
-O1 -foptimize-sibling-calls |
26 | 1 |
-O2, -O3 |
26 | 1 |
At -O2 and above GCC's own sibling-call optimisation converts all 26 handler tails into jumps anyway. The release build works, and if you did not have -Werror=attributes on you would never know the attribute had been ignored. At -O1 and below every dispatch is a real call. That build passes the 256 KiB verify pass (26,000 stack frames fit in 8 MB) and segfaults on the 16 MiB batch, exit code 139, stack overflow. Clang honours musttail at -O0; that is the whole point of the attribute.
So the trap is not "you lose the guarantee." It is: a debug or -Og build of a musttail state machine on GCC ≤ 14 will overflow the stack on a large input while the release build passes every test. Ship -Werror=attributes (Clang: -Werror=unknown-attributes -Werror=ignored-attributes) or a compile-time #error on __has_cpp_attribute(gnu::musttail), and treat GCC 15 as the floor.
The profiler bonus is real
perf record on the computed-goto scanner:
99.69% [.] scan_goto_lean
On the musttail chain:
8.11% [.] t_transtime
8.04% [.] t_sendtime
6.93% [.] t_clordid
6.48% [.] t_unknown
6.43% [.] t_symbol
6.08% [.] t_target
6.06% [.] t_checksum
5.30% [.] t_lastpx
5.01% [.] t_price
4.94% [.] t_sender
We did not set out to learn that the two UTC-timestamp parsers are the hottest handlers in the scanner, 8% each, more than the price parsers. The structure told us for free. In a real feed handler that is the difference between "the parser is 30% of the profile" and "SendingTime is 8% and here is why."
Smaller findings
switch_dense is not worth it on x86. Mapping sparse FIX tags through a 256-byte table to a dense index so the compiler is guaranteed a jump table costs 0-15% over the sparse switch on x86 (nothing on Clang/Zen 2, 13-16% on GCC/Comet Lake): the dependent load is worse than whatever the compiler was going to do with the sparse cases, which turns out to be a jump table anyway on both GCC and Clang. Only the M2 prefers it (11% faster).
Clang 18 vs 20, GCC 13 vs 14 move the absolute numbers by up to 8% and occasionally the ordering of switch and goto. Nothing changes the position of the tail chain.
Zen 5 did not compress the gaps. The lean tail/switch ratio is 0.887 on Zen 5 and 0.890 on Zen 2. If the mechanism were prediction, a much larger indirect predictor should have closed it. It did not, because it is not.
Methodology, and what we got wrong before we got it right
One translation unit per dispatch variant, so the objdump counts above are separable per kernel, and a driver with verify, matrix, stream and single modes. Five machines: aorus (TR 3960X, Zen 2, isolcpus core 18), hz2 (Ryzen 9 3900, Zen 2, isolated core 10), zen5 (Ryzen AI 7 PRO 350, no isolation, taskset), intel (i7-10700K, isolated core 6), an Apple M2 (no pinning). Compilers per machine as available: GCC 13/14/15/16-trunk, Clang 18/20, Apple Clang 21. Eleven samples, each a ~50 MB pass over the batch, kernels interleaved per sample so thermal drift lands on all of them. Medians reported; the min is within 1-2% of the median on every isolated-core cell.
The first version of the fat mode passed the cursor to the helpers as const uint8_t**. That made the cursor address-taken, so the monolithic variants kept it in a stack slot for the whole scan and every ++p in the tag loop was a store-then-load. Zen 2 showed goto 50% slower than switch and call mysteriously fastest; Zen 5 showed tail_pn 2x slower than tail; Comet Lake showed nothing. None of it was about dispatch. Returning {value, cursor} in a register pair made every one of those effects vanish and the numbers above are from that version. If your "calling convention" benchmark has a pointer-to-pointer in the API, check the store-forwarding counters before you believe it.
The GCC 14 census took two tries as well: the first count of indirect jumps missed GCC's notrack jmp *%rax and reported that GCC compiled the switch to a compare tree. It does not; both compilers emit a jump table. The CET prefix is why the sweep pins -fcf-protection=none.
What to do with this
If you write parsers or state machines in C++ and you can require Clang 19+ or GCC 16, the musttail chain is the best dispatch we measured on every machine and every compiler that supports it, and the reason it wins, smaller functions and better register allocation and no accumulator spilled to the stack, does not depend on your CPU's predictor or your workload's branch profile. Add preserve_none when the handlers make calls; it costs one line and it bought 4-5% on Zen 2, 2% on Zen 5 and 1.4% on Comet Lake with GCC 16, so budget for the low end unless you are on Zen 2. Do not expect it to matter on AArch64.
If you cannot require those compilers, use the switch. On GCC 14 the computed-goto version is slower than the switch and the flags that are supposed to fix it make it worse; on GCC 13 the flags work; on 15 and 16 you get 11 of the source's 27 sites back, against Clang's 53. That is not a foundation. Guard the tail chain behind __has_cpp_attribute and fall back to the switch, not to goto *.
#if defined(__clang__) && __has_cpp_attribute(clang::musttail)
# define MUSTTAIL [[clang::musttail]]
# define HAVE_MUSTTAIL 1
#elif __has_cpp_attribute(gnu::musttail) // GCC 15+
# define MUSTTAIL [[gnu::musttail]]
# define HAVE_MUSTTAIL 1
#else
# define HAVE_MUSTTAIL 0 // GCC <= 14: use the switch
#endif
#if __has_attribute(preserve_none) // Clang 19+, GCC 16+
# define PN __attribute__((preserve_none))
#else
# define PN
#endif
and in the build, so that an ignored attribute is a build break and not a stack overflow six months later:
GCC: -Werror=attributes
Clang: -Werror=unknown-attributes -Werror=ignored-attributes
And measure with counters, not with the ordering you expected. This experiment's prediction file has the switch, goto and tail variants within 5% of each other in lean mode on the strength of "modern predictors do not care about dispatch sites." The prediction about predictors was right. The conclusion drawn from it was wrong, because the thing that split the function into 26 pieces for the predictor's benefit split it into 26 pieces for the register allocator's benefit too, and that is where the 11% lives.
Review quiz
Five questions about this article. Get four right and it counts as read. Review quizzes come with a membership.
Log in See membership
Comments
No comments yet. Be the first to share your thoughts.