News for hackers. Stuff that matters.
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.
Re:Self-modifying code using GCC (2011) by CarVac on Monday August 07, 2023 @11:38AM (#37035936)
Does that not actually cause a mispredict if the speculative execution happened before the code change?
Re:Self-modifying code using GCC (2011) by messe on Monday August 07, 2023 @11:40AM (#37035968)
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.
Re:Self-modifying code using GCC (2011) by monocasa on Monday August 07, 2023 @11:49AM (#37036080)
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.
Re:Self-modifying code using GCC (2011) by messe on Monday August 07, 2023 @12:33PM (#37036752)
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.