SCRMaps
/
Sign in

Finding what is making the map lag

Which code is expensive is measured, not guessed - and the tool comes with euddraft.

19 min read

When a map slows down the usual move is to find the code that looks expensive and change it. That guess is often wrong, because the cost of code translated into triggers is not visible in the source: one read is dozens of triggers, and two lines that look alike can differ tenfold.

epTrace ships beside euddraft. It prints a cost figure per annotated call, which turns "iterating every unit is expensive" into a number.

[ 24 ] using_loopunit.main();     // 맵에 유닛 약 30마리 / about 30 units on the map
[ 10 ] using_unit_group.main();

[ 97 ] using_loopunit.main();     // 500마리 이상 / 500+ units
[  9 ] using_unit_group.main();
A full sweep grows with what the game is holding; a set you maintain yourself grows with what you put in it - which is where the tenfold gap at 500 units comes from.

Reading it is simple: measure two implementations under the same conditions and compare. "The same conditions" is the part that matters - in the table above the gap is a little over twofold at 30 units and more than tenfold at 500. Measure one situation only and you will pick the wrong one.

Measure before changing anything that runs every cycle, and measure before accepting somebody's claim that a pattern is cheap. Most cost claims in this scene were passed along without a measurement, and they change between versions.

If measuring is impractical, one rule carries most of the weight: do not read the same value repeatedly. Reads are expensive and writes are cheap, so anything that does not change inside a loop should be read once outside it.

In practice three wastes come up again and again, and all three are the same mistake: the question is being asked at the wrong granularity.

The three common ones
A Bring inside a unit loopA Bring asks a per-player question and a unit loop runs per unit, so inside one it runs once per unit instead of once per player. Check it before the loop, store the answer in a counter, and have the loop read the counter. Switching to a unit group changes nothing.
An array read over and overEvery read of an array element is dozens of triggers. Pull one that does not change inside the loop out into a const.
A runtime loop that branches per playerA runtime loop that then lists "if player 1", "if player 2" pays for the loop and keeps the duplication. Seven separate triggers are better, and a compile-time foreach writes them from one copy of the code.
foreach(p : py_range(7)) {
    const locations = list($L("p1"), $L("p2"), $L("p3"), $L("p4"),
                          $L("p5"), $L("p6"), $L("p7"));
    const loc = locations[p];
    if (Bring(P8, AtMost, 0, unit, loc) && /* ... */) {
        // 트리거 7개가 나오고, 코드는 한 벌입니다
        // seven triggers come out, from one copy of the code
    }
}
When there is no formula for the location numbers, a list does it. A loop is for removing the duplication between the P1 to P7 triggers - not for branching per player inside the loop, which is worse than writing the seven triggers out by hand.

There is a reason for the last one: there is a CurrentPlayer but no CurrentLocation. A Bring whose location varies per player cannot ride the CP trick, and rewriting its location fields from a variable costs more triggers than it saves. If the locations are numbered consecutively, $L("p1") + p is the whole of it.

And look at the classic triggers before you profile the EUD side. The shape that makes a map slow is an Always() trigger with a pile of unit-targeting actions on it. Each one has to walk units and test them against a location, every cycle, whether or not anything changed.

On the condition side the gap between heavy and light is larger than it feels, and somebody has taken both apart down to the byte.

Command against Bring
Command (cold)Three memcpys totalling 23,232 bytes, the same whatever is on the map - O(1).
When Command gets slowWhen the player slot is a group - AllPlayers, Foes, Allies, a Force - because it then walks twelve players accumulating.
Bring (cold)About a hundred times that: the same 23,232 bytes zeroed, four binary searches, then two x-range scans and one y-range scan, then a callback per touched unit.

One rule for design falls out: the x range is scanned twice and the y range once, so a wide, short location is slow and a narrow, tall one is fast. And if either range holds no units at all, the scans are skipped and it returns zero immediately.

A Deaths condition is cheaper than either, because it is a plain memory comparison with no cache to fill or invalidate. The ordering to design around is Deaths, then Command, then Bring a long way behind - which is also why reading a unit count straight out of memory beats asking either condition.

The biggest saving is not a lighter condition but not reaching the trigger at all. Walking triggers and parsing their conditions and actions is a large share of the trigger loop's cost: a trigger that is skipped costs nothing, while one that is visited and found false still costs the parse.

The expensive actions, and why
MoveUnit · the Modify family · SetInvincibility · KillUnitAt · RemoveUnitAt · OrderAll of them have to work out which units are inside a location.
An action costs more than a conditionA Modify and a Bring make the same test and the action costs more, so gating the action behind the condition wins - on a map where that unit is usually absent, one Command in front of it is a large saving.
The worst is a MoveUnit with nowhere to goWhen the destination is full of units or off the map, the engine hunts for free space and does not find any. This is the exact contrast with writing coordinates into the struct, which goes to the exact spot or nowhere.

And checking several players on a classic trigger runs it that many times: players = {P5, P6, P7, P8} with CurrentPlayer in the actions is four executions. Where the work is not really per-player - invincibility on a whole force's buildings, say - give the trigger to one player and address the force.

Beyond making a condition cheaper, there is a saving in the order alone. When a trigger has several conditions to get through, put the one that leaves the fewest survivors first: everything behind it then runs on that many fewer things.

An example makes it concrete. Say the job is "take P1's Marines that are at 50 HP down to 40". The map has 100 units for each of P1 to P8, so 800; of those, 64 are Marines and 200 are at 50 HP.

Which one to test first
Player firstLeaves 100.
Unit type firstLeaves 64 - the fewest.
Hit points firstLeaves 200.

So type goes first. Then you count again within the Marines: of the 64, 32 belong to P1 and 10 are at 50 HP. So HP is next and owner last. Nothing changed but the order of three conditions, and the later ones now run an order of magnitude less often.

The same holds in classic triggers. For "owns unit A and death B equals 8", if death B is the rarer of the two, put it on top. The more triggers there are the more it shows.

And work that does not have to happen every frame should be spread across frames. Split a class of work into N pieces and do one piece per frame, and the worst frame costs a fraction of what it did - the same total work, without the hitch.

What costs the most in classic triggers
Order and AI scriptsThey issue an order to each unit, so repeating them every cycle on a crowd lags immediately - 240 Mutalisks on a permanent patrol is unplayable at classic turbo's 13 passes a second. Gate it to once every second or two with a death counter.
A MoveUnit into a blocked spotWhen the destination is packed the engine hunts for free space. Several sources feeding one blocked destination makes the game unplayable.
KillUnitThe death animation leaves a sprite, which makes it more expensive than RemoveUnit. Enough accumulated sprites lag the game and start eating attacks.
A repeated PlayWAVA footstep sound on every step is the usual case. It costs more when every player hears it at once, and past ten a second the game stops progressing.

Do not use a Wait to space something out - it breaks the hyper triggers. Count the interval with a death counter or a variable.

Why a variable costs 72 bytesWhich way round you write a condition is the cost