Far Away, at One-Sixth Speed

Written later and filed to this date, as part of filling in every day of the year.

perf imp

That is the whole message. Two words, no body, fourteen files, on a Friday afternoon in April, in a browser survivor shooter whose history is mostly commits like it. Eight days earlier a commit called performance had taught the enemy AI to skip frames: an enemy more than eight hundred pixels from the player updates every sixth frame, one between four and eight hundred every third, and bosses always. Two thousand enemies at the cap, most of them far away, most of them doing nothing most frames. A good saving.

The skipped frames had to go somewhere, and the first version put them in the wrong place. On a skipped frame it advanced the enemy’s main timer by one frame’s worth of time and moved on. On the frame it did run, it handed the behaviour function a single frame’s delta, the same delta a nearby enemy gets every frame. The main timer kept honest time. Everything else inside the behaviour did not. A sniper’s shot cooldown counts down by the delta it is handed, and it was handed one frame of time for every six that passed. A shielded enemy’s shield regenerated at a sixth of its rate. A zigzag’s phase advanced at a sixth. Far away, the game ran in slow motion, and nothing looked wrong, because far away is where you are not looking.

The fix is the smallest hunk in the commit. A skipped frame now advances nothing. The frame that runs is handed the delta times six, or times three, so a cooldown that should have expired in two seconds expires in two seconds whether its owner is updated every frame or every sixth. The behaviour functions are unchanged; each receives one argument whose meaning changed from “one frame” to “the frames since you last ran.”

What the author understood is in that one line: throttling how often you compute is only a saving if you also pay the computation for the time it missed. What the message does not say is that this is a correction, and of the commit the same author made eight days before. perf imp reads as more of the same. It is the opposite: the earlier performance work had quietly made the game easier at range, and this put the difficulty back.

The other twelve files are a different kind of less. The instruction file a model reads before touching this repository went from 2,772 words to 2,305, the same sections in shorter sentences, with the long version kept beside it under a new name. Fewer tokens per read, the way the AI change is fewer updates per enemy. Both halves of the diff are a performance improvement, and in both the subject line is true. It just does not mention that one of them was a bug.


← all writing