Re:Self-modifying code using GCC (2011)
by countWSS on Monday August 07, 2023 @11:12AM
(#37035579)
Instead of if( condition) then code1: else code2, computed goto
will just switch the branch to unconditional jump to either code1/code2,
which bypasses branch misprediction penalty and is generally
more elegant.
[ Parent
| on HN ]
Does that not actually cause a mispredict if the speculative execution happened before the code change?
Worse. It's more than likely to cause a cache miss, and waiting on memory is going to be even slower than a mispredicted branch.
It could be even worse. I've seen x86 cores where the icache/dcache consistency logic ended up internally being most of a page fault and recovery once noticed by the processor.
Oof on that note: at least in the example in the article, it's going to cost a TLB miss as page permissions are getting changed. So that's a page-table walk which means %cr3 -> PML4 -> PDPT -> PD -> PT -> the actual address you want to access; potentially up to 5 memory accesses (6 if your system has more than 256TB of RAM and you're using 5-level paging).
That being said, in a loop you'd only take this hit on the first run. You'd be back to cache misses after that.