News for hackers. Stuff that matters.
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.

There are no comments on this comment yet.