30 workers broke my frame rate, so we went hunting

So, after the RTS planning mode, the obvious next thing was to give the player a proper crew. More goblins, more digging, more building. Then I spawned 30 workers and the game started freezing for a second or two at a time.

Not great.

Some numbers first, so we know what we are dealing with. With 30 workers the average frame was over 200 ms. A smooth game needs about 16 ms to get 60 fps. Every now and then a single frame took several seconds.

Where did the time go?

We did a read-through of the whole codebase, looking for anything that does work every frame and gets more expensive as you add workers. It found a bunch. Three of them were the big ones:

– Build planner = decides which ground cells get a building job next.

– Material demand = figures out how many logs and stones each planned wall still needs.

– Route search = finds a path for a worker through a crowd of other workers.

Lets go through them.

Build planner: the same list, 400 times

Every simulation tick the planner looks at each unfinished cell of a building plan. A 20×20 pad is about 400 cells. For every cell it asked two questions: “is someone already working here?” and “is this blocked?”. Both answers came from scanning the whole job list again.

400 cells times a few hundred jobs, every tick. And when the game falls behind it runs up to 8 ticks per frame to catch up, which makes it all worse exactly when it is already bad.

The fix was almost embarrassingly simple: read the plan’s jobs once per tick into a lookup table, then answer every cell from the table.

End result: 13.3 ms → 1.35 ms per tick. About 10x faster, and the answers are exactly the same. We checked that by running the old and new code side by side and comparing over 12 000 answers. Zero differences. 🙂

Material demand: finished walls never left

This one was sneaky. For each planned wall, the code copied the entire job list just to find that wall’s own build job. On top of that, the list of “planned” pieces was made by scanning *every piece ever built*. So the longer you played and the bigger your base got, the slower it got. Forever.

Now the job pool keeps a small index of build jobs per piece, and the building registry keeps a set of what is actually still planned. Finished walls cost nothing.

End result: 5.1 ms → 4.1 ms per tick with 60 planned walls. Smaller win, but it stops growing. The rest of that cost lives in two other spots (stockpile bookkeeping and shortage messages), and we fix those next time.

Route search: the actual freezes

This was the big one. When a worker needs a route through a crowd, the search could take 1 to 3 seconds. And it ran on the main thread, so nothing else ran meanwhile. That’s your frozen game.

The plan was obvious: move the search to a background thread. Getting it right was not.

First try: search on a thread, but share the “slots” with route building. No more freezes! But fewer routes got delivered than before. After MANY re-does, we finally measured *where* the time went instead of guessing. Turns out new route builds kept grabbing every free slot. A finished search waited up to 10 seconds, and by then the terrain had changed, so the answer was thrown away and the whole thing done again.

What worked: searches got their own slots. Two at a time, plus one kept for the player, oldest first. Each search works on its own private copy of the world, so the thread never touches anything the game is changing.

End result:

– frames over 1 second: gone (main still had 1-5 per run)

– worst stall: about 0.1-0.8 s instead of 4-7 s

– routes delivered: back to the old level

Good enough. 🙂

Learned something

Biggest lesson for me: measure where the time goes before trying the second fix. I tried two “obvious” tweaks on the route search, and both made things worse or did nothing. One proper measurement pointed straight at the real problem.

Also: three test runs is not enough when the numbers bounce around. Five per side at minimum.

What next

Four more of these are lined up:

– the catch-up budget thing (game behind = 8 budgets per frame instead of 1)

– the worker panel, which re-ranks every worker name against every other worker every frame

– a retry check that looks at every waiting job in one go

– navigation copying every wall in the world for a tiny search area

We fix those next time.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *