News for hackers. Stuff that matters.
Re:The art of creating very tiny programs for the 80x86 fami... by kccqzy on Friday September 14, 2018 @09:23PM (#17992418)
The CPU most definitely still supports self-modifying code. Intel takes compatibility seriously.
Re:The art of creating very tiny programs for the 80x86 fami... by kragen on Friday September 14, 2018 @10:01PM (#17992551)
Oh, yeah, it's supported. But it's also slow.
Re:The art of creating very tiny programs for the 80x86 fami... by userbinator on Saturday September 15, 2018 @01:20AM (#17993105)
"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.

Re:The art of creating very tiny programs for the 80x86 fami... by kragen on Saturday September 15, 2018 @12:06PM (#17994779)
Sure, 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.
Re:The art of creating very tiny programs for the 80x86 fami... by kccqzy on Thursday September 20, 2018 @02:58AM (#18030305)
I 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.
Re:The art of creating very tiny programs for the 80x86 fami... by kragen on Sunday September 23, 2018 @10:18PM (#18054581)
Self-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.

Re:The art of creating very tiny programs for the 80x86 fami... by ddingus on Saturday September 15, 2018 @12:37AM (#17993009)
Has to be, right?

Artifact of modern execute and cache schemes.