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.
[ad_2]
Source link
More on the time the x86 emulator team
Stay tuned for updates on the time the x86 emulator team as the story develops. Related: An Ex-Meta L8’s Agentic Engineering Setup.
Frequently Asked Questions
During an exchange of war stories, a colleague found a program that allocated about 64KB of stack memory where the compiler had unrolled the initialization loop into 65,536 individual write-byte instructions — 256 kilobytes of code to initialize 64 kilobytes of data.
A Windows processor emulator for x86-32 on systems running a different native processor, using binary translation (essentially a JIT compiler) rather than interpretation.
They were so offended by the bloated function that they added special code to the translator to detect it and replace it with an equivalent efficient version during emulation.
Instead of a small tight loop after a stack probe, the compiler generated one 4-byte write instruction per byte — turning a routine 64KB initialization into 256KB of machine code.




