After a long hitch, a fixed-step game loop falls further behind every frame and never recovers; what is this spiral of death and how do you prevent it?
answer
- more steps owed, longer frame
- step cost versus step length
- clamp the frame time
- cap steps per frame
- slow motion beats freezing
basics
~20 sThe spiral of death is a fixed-step loop whose catch-up steps cost more real time than they simulate, so each frame owes more steps. Clamping frame time, capping steps per frame and cheap steps prevent it.
solid answer
~50 sA fixed-step loop banks elapsed time and runs as many steps as it owes. After a 300 ms hitch it owes about 18 steps at 1/60 s. If a step costs less real time than the 16.7 ms it simulates, catching up shrinks the debt and the loop recovers within a few frames. If load has pushed a step's cost to, say, 20 ms, each catch-up frame takes longer than the time it simulated, the next frame owes more steps, and the frame time grows without bound until the game freezes. Prevention has three layers: clamp the frame time added to the accumulator, cap the number of steps per frame and drop the excess, so the game runs in slow motion rather than locking up; and keep the step's cost well inside its length at peak load. On a dedicated server, where slowing time hurts every client, the last layer is the one that matters.
code
pseudocode · 15 linesSTEP = 1.0 / 60.0
MAX_FRAME = 0.25
MAX_STEPS = 4
frameTime = min(now - lastTime, MAX_FRAME)
accumulator = accumulator + frameTime
steps = 0
while accumulator >= STEP and steps < MAX_STEPS:
previousState = currentState
currentState = simulate(currentState, STEP)
accumulator = accumulator - STEP
steps = steps + 1
if steps == MAX_STEPS and accumulator >= STEP:
accumulator = 0.0 // drop the debt: run slow, never spiral
reportOverBudgetFrame()go deeper
Recall that a fixed-step loop runs extra steps to catch up after a slow frame, and that those steps can make the next frame slower still.
Explain the ratio that decides recovery versus runaway, and show where the frame-time clamp and the step cap go in the loop.
Walk a scenario with numbers, separate the trigger from the cause, and give the content-side fixes that keep a step inside its budget at peak load.
Decide the failure policy a team accepts, slow motion or dropped time, and how per-step budgets are enforced on clients and dedicated servers.
## The feedback loop A **fixed-step loop** adds each frame's real elapsed time to an **accumulator** and runs whole simulation steps while the accumulator holds at least one step. That is what keeps simulated time in pace with real time. It also creates a feedback loop: the longer a frame takes, the more steps the next frame owes, and running those steps makes that frame longer too. Whether the feedback dies out or explodes depends on one ratio: **how much real time one step costs compared with how much time it simulates**. Call the step length `step` (16.7 ms at 60 steps per second), the CPU cost of one step `c`, and the fixed per-frame cost of rendering and everything else `r`. Running `n` steps takes about `n*c + r` of real time while simulating `n*step`. - If `c` is well below `step`, each catch-up frame simulates more time than it consumes, and the debt shrinks. - If `c` is at or above `step`, every catch-up frame consumes more time than it simulates, and the debt grows every frame. That runaway is the **spiral of death**. - Close to the boundary, the loop does not explode but settles at a poor frame rate, running several steps per frame just to keep up. ## A worked scenario The figures below are a scenario's, with `step` = 16.7 ms and `r` = 6 ms, starting with a 300 ms hitch. | Frame | Cheap step (`c` = 8 ms): steps run, frame time | Heavy step (`c` = 20 ms): steps run, frame time | |---|---|---| | 1 | 18 steps, 150 ms | 18 steps, 366 ms | | 2 | 8 steps, 70 ms | 21 steps, 426 ms | | 3 | 5 steps, 46 ms | 26 steps, 526 ms | | 4 | 2 steps, 22 ms | 32 steps, 646 ms | | 6 | back to 1 step, 14 ms | 46 steps, 926 ms | With cheap steps the hitch produces a short burst where the world fast-forwards, then normal frames. With heavy steps, which a large fight or a crowd of physics objects can cause, the hitch starts a slide the loop never leaves. The hitch is the trigger; a step cost near or above its own length is what sustains the spiral. ## Prevention, layer by layer 1. **Clamp the frame time** added to the accumulator, for example to a quarter of a second. A hitch of any length then owes a bounded number of steps, and real time beyond the clamp is simply dropped: the world loses that time instead of fast-forwarding through it. 2. **Cap the steps per frame** at a small maximum, and discard the leftover debt when the cap is hit. In the heavy scenario a cap of four steps settles at about 86 ms frames that simulate 66.7 ms each, so the game runs at roughly 78% speed. That is ugly, but it stays responsive and recovers as soon as load drops, instead of freezing. 3. **Keep the step inside its budget at peak load.** This is the real fix; the first two only choose how to fail. Typical tools: - simulate distant or idle objects less often, or put them to sleep; - cap the number of active expensive objects such as physics debris; - move work that does not need the fixed step, such as cosmetic effects, onto the variable frame time; - lower the step rate only as a project decision, since designers tune against it. 4. **Detect it** by logging sustained frames that hit the step cap, so the problem is fixed in content rather than discovered by players. ## Choosing what to do with dropped time | Policy | What players see | Where it fits | |---|---|---| | Clamp and cap, drop the excess | brief pause or slow motion | single-player and local simulation | | No clamp, catch up fully | fast-forward burst, then freeze if steps are heavy | rarely acceptable | | Lower step rate under load | changed feel and tuning | a deliberate mode, not a reflex | ## The server case A **dedicated server** runs a fixed tick with no rendering, but the same arithmetic applies: if a tick costs more than its period, ticks start late and the server falls behind real time. Clamping and dropping time there makes the server's world run slower than its clients expect, which harms every connected player. Some games accept deliberate, announced slow-down of server time under extreme load; most treat a tick that exceeds its period as a capacity failure and fix it by keeping per-tick work bounded and by running fewer matches or players per process.
- Why is clamping the frame time not enough on its own if each step costs more than its length?The clamp only bounds the debt one hitch can create. If a step costs 20 ms to simulate 16.7 ms, a normal frame still owes slightly more than it paid, and the debt keeps creeping up to the clamp. Capping steps per frame and dropping the remainder bounds each frame's cost, and only cheaper steps restore real-time speed.
- When a per-frame step cap is hit, why discard the remaining accumulated time instead of carrying it into the next frame?Keeping the full remainder would carry a debt of whole steps into the next frame and restart the catch-up. Discarding it, or keeping only the part below one step, accepts that the world fell behind real time. The interpolation factor then stays meaningful because it is still a fraction of one step.
- Believing a spiral of death means the game ran out of memory
- Fixing it by switching physics to a variable timestep without discussing stability
- Thinking a frame-time clamp alone fixes steps that cost more than their length
- Catching up every owed step after a hitch so no time is ever lost
- Blaming the hitch alone and never measuring the per-step cost