(This question is totally unrelated to my very recent other topic.)
I've just translated Pong for the first time using my own VMTranslator (super satisfying when that finally works!). Though it's not exactly an "optimizing" translator, it does implement a few changes that make it about 2k lines shorter than the supplied Pong from project 6. And yet, my version of Pong is running noticeably slower than the supplied version, and I'm having trouble understanding why.
The supplied Pong even uses subroutines for all comparisons (GT, LT, EQ) wheras mine does these inline. Mine also does significantly less pushing and popping to the stack.
I haven't really checked the Pong jack files to understand how the game works, since I haven't moved onto chapter 9 yet. So I was hoping someone could outline a bit how the game works and where the bottleneck is. I'm guessing the specific part of the code that does game state update or maybe screen update ended up longer than it should be..
Ok, after some inspection, it looks like the supplied Pong.asm from project 6, for some reason doesn't call Sys.wait at all during execution (cheaters!), unlike the .jack version from project 11. Simply cutting that out makes both run at around the same speed (what's the point of this function?).
If anyone else is wondering why their translation is lagging, try the above.
The HACK architecture does not provide any real time, so the wait function is implemented as a busy loop. Here is what they wrote in the book:
Sys.wait: This function can be implemented pragmatically, under the limitations
of the simulated Hack platform. In particular, you can use a loop that runs
approximately n milliseconds before it (and the function) returns. You will have to
time your specific computer to obtain a one millisecond wait, as this constant varies
from one CPU to another. As a result, your Sys.wait() function will not be portable,
but that’s life.
Pong in chapter 6 is provided as a more realistic test for the assembler. It's actually written in Jack and compiled to assembly. The Jack sources are in projects/11/Pong directory and they do include calls to Sys.wait. I would expect that the asm code also includes those calls, but they are removed for some reason.