Reduce lag spikes from SquadManagerBotModule.
Updating squads is the most expensive part of SquadManagerBotModule. It involves ticking the current squad state. This usually involves finding nearby enemies and evaluating the fuzzy state machine to decide whether to interact with those enemies. Since all the AI squads for a player get ordered on the same tick, this can result in a lag spike. To reduce the impact, we'll spread out the updates over multiple ticks. This means overall all the AI squads will still be refreshed every interval, but it'll be a rolling update rather than all at once. By spreading out the updates we avoid a lag spike from the cumulative updates of all the squads. Now the lag spike is reduced to the worst any single squad update can incur.
This commit is contained in:
@@ -125,6 +125,7 @@ namespace OpenRA.Mods.Common.Traits
|
||||
readonly HashSet<Actor> activeUnits = new();
|
||||
|
||||
public List<Squad> Squads = new();
|
||||
readonly Stack<Squad> squadsPendingUpdate = new();
|
||||
readonly ActorIndex.NamesAndTrait<Building> constructionYardBuildings;
|
||||
|
||||
IBot bot;
|
||||
@@ -347,7 +348,16 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
attackForceTicks = Info.AttackForceInterval;
|
||||
foreach (var s in Squads)
|
||||
s.Update();
|
||||
squadsPendingUpdate.Push(s);
|
||||
}
|
||||
|
||||
// PERF: Spread out squad updates across multiple ticks.
|
||||
var updateCount = Exts.IntegerDivisionRoundingAwayFromZero(squadsPendingUpdate.Count, attackForceTicks);
|
||||
for (var i = 0; i < updateCount; i++)
|
||||
{
|
||||
var squadPendingUpdate = squadsPendingUpdate.Pop();
|
||||
if (squadPendingUpdate.IsValid)
|
||||
squadPendingUpdate.Update();
|
||||
}
|
||||
|
||||
if (--assignRolesTicks <= 0)
|
||||
|
||||
Reference in New Issue
Block a user