The time the x86 emulator team found code so bad they fixed it during emulation

An emulator developer shares a story from the days of Windows x86-32 emulation, where a compiler's terrible loop unrolling forced the emulator team to write a custom optimizer to fix the code on the fly.
During an exchange of war stories, a colleague of mine told one from back in the days when Windows included a processor emulator for x86-32 on systems that natively ran some other processor. (This has happened many times. And no, I don’t know which processor this particular story applied to.)
This particular emulator employed binary translation, generating native code to perform the equivalent operations of the original x86-32 code. This offered a significant performance improvement over emulation via interpreter. You can imagine that x86-32 is just a bytecode, and the emulator is a JIT compiler.
Anyway, my colleague found that there was one program that needed to allocate around 64KB of memory on the stack and initialize it. The standard way of doing this is to perform a stack probe to ensure that 64KB of memory is available, then subtracting 65536 from the stack pointer, and then initializing the memory in a small, tight loop.
But using a loop to initialize the memory was too mundane for whatever compiler was used to compile this code. Instead of generating a loop to initialize each byte of the buffer, the compiler “optimized” the code by unrolling the loop into 65,536 individual “write byte to memory” instructions, each 4 bytes long.
All in all, it took this program 256 kilobytes of code to initialize 64 kilobytes of data.
This offended the team so much that they added special code to the translator to detect this horrible function and replace it with the equivalent tight loop.
This is actually done in modern CPU’s. Unrolling the loops in the fetch ahead pipeline. And it is a thing in computer science. I specifically design my code to take advantage of it.
There is an expression, you can either have slow, memory efficient code or fast, but it hogs memory. But I don’t think it applies to using 256kb to initialise 64kb lol. The is a bit much 🤣🤣
Source: Hacker News












