Flow fields for 100k units: caching per cluster instead of per unit
Why the obvious approach dies at 5,000 agents and what to do instead.
The approach that dies at 5,000
The first pathfinding system every RTS prototype gets is the honest one: each unit runs A* from where it is to where it was told to go, stores the path, and repaths when something changes. It works beautifully at 200 units. It survives 1,000 with some smoothing. Somewhere around 5,000 it stops being a performance problem and becomes a design problem, because the team starts designing around it: fewer units, smaller maps, slower repaths, “the AI just does not react to walls that quickly”.
The math is simple. Cost scales with units multiplied by path length multiplied by repath frequency. Five thousand units, paths of a few hundred nodes, a repath every half second, and you are expanding millions of nodes per second before you have simulated anything else. Memory is worse than CPU: every unit owns a path, paths are lists, lists are allocations, and allocations at that rate fight the garbage collector all game long.
A flow field inverts the relationship. Instead of one path per unit you compute one field per destination: a grid where every cell stores the direction to move to reach the goal. Computing it is a single breadth-first or Dijkstra pass over the map. Following it costs each unit one cell lookup per tick. Ten thousand units heading to the same rally point cost the same as one.
This is the trick behind every RTS that shows big armies, and the reason the first version of LEGION, my simulation engine for Unity DOTS, used it from day one. It is also where the second problem starts.
The catch: fields multiply
A field is per destination. On a 512 by 512 map that is 262,144 cells, one or two bytes each. Fine for one field. Now a battle with 64 players, each with a few groups going to different places, plus units chasing moving targets, plus gatherers going back and forth. Fields go from “one” to “one per order”, and every new order pays a full-map pass plus a quarter megabyte of memory. At that point you are back to designing around the system.
The standard fixes are all partial. Sharing fields between units that happen to have the same target helps in scripted scenes and not in real play. Lowering the resolution ruins movement near walls. Computing the field lazily, cell by cell, turns one predictable cost into thousands of unpredictable ones.
Caching per cluster
The fix that scales is to stop thinking in whole-map fields. Split the map into clusters, 32 by 32 cells in my implementation, and build a coarse graph on top: the nodes are the openings between adjacent clusters, the portals, and the edges are the walkable connections between portals inside a cluster. This is hierarchical pathfinding, and by itself it only speeds up long searches.
The interesting part is what happens when you combine it with fields. A unit that needs to cross the map no longer wants “the field to the destination”. It wants the field that takes it from its current cluster to the next portal on its coarse route. That field is tiny, 1,024 cells, and it depends only on two things: the cluster and the portal. Not on the unit, not on the final destination, not on the order.
So the cache key becomes (cluster, portal). A map with 256 clusters and a handful of portals each has a few thousand possible fields in total, most of them never needed. Every unit crossing cluster 17 towards its east portal shares one field, regardless of where it started or where it ends. When a new order arrives, the expensive part is a coarse search over a graph of a few hundred nodes; the fields it needs are almost always already in the cache.
I keep the cache as an LRU with a fixed budget. Four thousand fields at one byte per cell is four megabytes, which is nothing, and the hit rate in real matches is high enough that the integration cost stopped being something I profile. With Burst, integrating a 32 by 32 field is a matter of microseconds anyway; the point of the cache is not to save those microseconds, it is to make the cost independent of the number of units and orders.
What this looks like in the tick
LEGION runs its simulation at 15 Hz in deterministic lockstep, so every tick has about 66 milliseconds of wall time to share between pathfinding, movement, avoidance, combat and everything else, on every client, with identical results. Per-cluster fields make the movement part embarrassingly parallel: a job iterates over unit chunks, reads the cluster each unit is in, looks up the field for its current portal target, reads one direction, and hands it to steering. There is no per-unit path, no list, no allocation, and the memory access pattern is friendly because units in the same chunk tend to be in the same cluster.
Invalidation is where most implementations get sloppy. Buildings get placed, walls get destroyed, and the cached fields around them lie. I version each cluster: any change to walkability inside a cluster bumps its version, and fields carry the version they were built against. A stale field is simply rebuilt on the next lookup. Neighbouring clusters are unaffected unless a portal itself opened or closed, in which case the coarse graph is patched and the affected routes are re-planned on the next tick. In practice a base-building phase invalidates a handful of fields per second, which is invisible.
Determinism
Because the simulation is lockstep, the fields must be identical on every machine. That rules out anything with floating point in the integration step and anything whose result depends on iteration order. Costs are integers, the Dijkstra frontier is processed in a fixed order, ties are broken by cell index, and the cache eviction is driven by the simulation tick counter, not by wall-clock time. Get any of these wrong and two players will watch the same army walk two different routes, which is a desync you will not find for weeks.
Pitfalls I hit so you do not have to
Cluster borders create local minima. A unit standing exactly on a portal cell can oscillate between two fields if the coarse route flips under it. The fix is hysteresis: commit to a portal until the unit has actually crossed into the next cluster.
Moving targets break the cache. A unit chasing another unit does not have a fixed portal. I handle it by recomputing a small local field around the target only when it changes cluster, and by letting avoidance and steering handle the last few cells.
Do not put units into the cost field. It is tempting to make crowded cells expensive so armies spread out. It invalidates every field every tick. Crowding is an avoidance problem, not a pathfinding one, and a separate article covers it.
Takeaways
Per-unit paths scale with units. Whole-map fields scale with orders. Per-cluster fields scale with the map, and the map does not grow during a match. If your simulation is designing around its pathfinding, the problem is the cache key, not the algorithm.
If this is your problem right now, the large-scale simulation review is a week of my time on exactly this kind of code.