<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141638>141638</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Code with goto is 3x faster than code with continue
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
gbaraldi
</td>
</tr>
</table>
<pre>
While debugging https://github.com/JuliaLang/julia/issues/47542 I managed to reproduce the same slowdown with C code, https://godbolt.org/z/789GdG3bE.
The code is
```c
__attribute__((noinline)) size_t foo(int64_t *arr, size_t len) {
size_t i = 0;
foo:
while (i < len) {
if (arr[i] == 0) {
i += 1;
goto foo;
}
i += 2;
}
return i;
}
__attribute__((noinline)) size_t bar(int64_t *arr, size_t len) {
size_t i = 0;
while (i < len) {
if (arr[i] == 0) {
i += 1;
continue;
}
i += 2;
}
return i;
}
```
The difference is that code with continue gets compiled to a select where the goto keeps the branch. I imagine this is a choice done in SimplifyCFG but I imagine that branch prediction/speculation on this is good enough that the conditional increment done on the continue case is much worse. Goto was faster on everything I tested (2 aarch64 cpus and 2 x86 cpus)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVE2P2zYQ_TXUZRBDpr4POni91SJFby3Qo0GRI2lSihRIap3Nry8o2fE2SIMGKGHAms9HPj6O8J5Gg9iy4okVz4lYw2RdO_bCCa0o6a16a_-cSCMo7NdxJDPCFMLiWXZivGO8GylMa3-Qdma8-3XVJH4TZmS8-xS_Ge_I-xU9411eFTmHjzALI0ZUECw4XJxVq0QIE4IXM4LX9qrs1cCVwgRnkFYh4-dvUa3qrQ4H6yLUF8a7qm5e1EvW_3Jg6Ymlpz8m3GqBPERPme4_ydLT5SJCcNSvAS8XxmvGa2PJaDLIeMN4A56-4CXAYC3jNZlQ5pcAjJ-Ec3Ezt7BGE5NZ9cTS091JwLJnSFm2OQG2JtnpZlw3MmNTYNn5mw63RUNMiFDFE7HiOTbcez5SCRh_is7jA-i-Rhvsjvo-wqrn9xj3ev7IumU4DKszQHtgd_4Mab1w_500gH_l7f-i6t36EWvSmkBmxZ9i7RH-Lmt30T0kqWgY0KGRmzDDJMKu0k3t9y3AiMGDtPNCen8oAjxqlAGuE7r9tWy3_Bfi4jezd8LI6QAfgWYxkolJ5COIADlZkgjKGgQy8DvNi6bh7dy9QL-Gf5SIcOsEi0NFMpA1jHd-QblqES2w5mvr0VoFaOw6Tntt2F6dURQzhQYy0uGMJuzgWyk-zimF33iYVznB1TqPB3iJ57oKD4PwAV2swVd0b2GKw-cjBPQBVbx2DkI4OZU5yGX1IIwCDp_rcjOjJtNTotpMNVkjEmyPVV5nZdmUVTK1Q582eV4MQzVgc6wLlQ9NM6Q5VirLskom1PKUF2nBq2PFG14dZNGInB_LSmU41EKwPMVZkD5o_TrHQZRsk6495scyqxMtetR-G6ycG7zCFmWcxznr2lj0oV9Hz_JUkw_-0SZQ0Niev6piu2jykH2-UxImYb4jm2R1uv3BdI4It78Pi7OfUIb38_m28deW_x0AAP__CinVyw">