3 ms·
The CPU most definitely still supports self-modifying code. Intel takes compatibility seriously.
by kccqzy 8y ago
The CPU most definitely still supports self-modifying code. Intel takes compatibility seriously.
- kragen 8y agoOh, yeah, it's supported. But it's also slow.
- ddingus 8y agoHas to be, right? Artifact of modern execute and cache schemes.
- userbinator 8y ago"continuously modifying" code, yes. But do something like modify a constant used in a tight loop once before it, and you will likely get better performance, especially since you now don't have to use a register or an extra memory access to load that constant. That is, after all, how JITs work. One of the best examples is something like an image codec --- input images have diferent widths, heights, bits per channel, channels, coding modes, etc. but they are constants throughout the same image. You can essentially "compile" a decoder specialised to the exact image you're decoding, with all the loop counters replaced with constants and the branches eliminated to directly execute the code they would otherwise jump to.
- kragen 8y agoSure, I didn't mean to imply that running dynamically generated code was slow (although, as you point out, that's usually done by generating new code, not tweaking existing code). I meant that it's slow to modify code as you execute it — like the MIX subroutine return system where you poke the subroutine return address into the return instruction when you enter the subroutine, or the Johnniac array indexing system where you modify the address in the instruction instead of indirecting the memory access through a CPU register. Not that you should program that way if it were fast.
- kccqzy 8y agoI feel like this is something the compiler could do automatically. Give it a hint, let it do some alias analysis and discover that a particular variable won't change throughout a loop. And then let it treat that variable as a constant (as an immediate in instructions) and generate the code to modify it at the beginning. Perhaps let it pass on this information so that eventually the section could be mapped W+X.
- kragen 8y agoSelf-modifying code to optimize a hot loop is only a useful thing to do on a machine without a split I/D level-1 cache. All modern high-performance CPUs will suffer an instruction cache flush on each iteration of the hot loop, which will kill your performance. Also, though, even on CPUs with no instruction cache, it's probably a very minor improvement at best. Suppose you write the variable in one place and read it in 4 places. If no register is allocated to the variable, on a load-store machine such as a RISC, the usual approach involves one store and four loads. But if you use self-modifying code instead, you have four stores (and no loads), but those stores are modifying only part of an instruction word — so, on most hardware, they actually require a preceding load, even if the architecture tries to hide that from you. I'm convinced there are cases where this kind of optimization actually speeds up your code, but they are pretty unusual.