June 15th, 2026
heartmind blownlikecompelling13 reactions

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

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.

Topics

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

27 comments

Sort by :
  • Igor Levicki

    @Raymond Chen

    Come on Raymond, don’t leave us hanging, shame that compiler which did infinite loop unrolling?

  • Michael Brundage 1 day ago

    Thanks?

  • Josh Burna 2 days ago

    .. is it just me, or does *EVERY SINGLE ONE* or the comments – other than mine – absolutely scream “I was written by a shitty AI comment bot” or no??

    • Chris Iverson

      Says the guy using em-dashes in his writing.

      In all seriousness, though, I didn’t see anything that screams “AI” to me.

      There’s a bunch of technical stuff, but that’s to be expected on a post explicitly talking about technical details.

      • LB 13 hours ago

        Microsoft’s blog software coverts normal dashes to emdashes. Take a guess why Microsoft would make that change.

    • IS4 2 days ago

      I wanted to write that I am surprised people in 2026 don’t understand how replies work… not that the comments seem somewhat AI-ish.

      • Chris Iverson

        Blame the commenting website software for that one.

        There’s been tons of reports of people’s comments getting added to the wrong thread, or not added to a thread at all, or being shown out-of-order.

  • Michael Brundage 3 days ago

    We did this a lot in the Xbox emulator on Xbox 360. Every Ubisoft Xbox game scanned the entire DVD twice to figure out its localization; some games had multithreading or memory management bugs; no games took advantage of WriteScatter/ReadGather; so many other obvious inefficiencies and bugs. Eventually, we got it so games loaded faster in the emulator than on the original console!

    • Neil White 3 days ago

      So nothing has changed in terms of game optimization 😄. Even games on medium settings are bringing top-of-the-line GPUs to their knees. That being said, we greatly appreciate the efforts you all on the emulation team are doing. 🙂