An EUDVariable is a trigger containing exactly one SetDeathsX action. The value lives in that action's number field, and reading or writing the variable means reading or writing that field.
A trigger is 2,400 bytes in the map's chk structure, plus 8 for the two node pointers in memory: 2,408. Yet a variable is said to cost 72. Both are true - 72 is the cost of one MORE variable. A hundred of them come to (100 - 1) × 72 + 2,376 bytes.
The most revealing part is what happens when you pass a variable as an argument. The value is not copied: the destination address of the variable's own SetDeathsX is rewritten at runtime. A variable is less a thing holding a value than a trigger holding where to put one.
Why a read is expensive becomes obvious if you do the same job in classic triggers. There is no action that reads a value, so copying one death counter into another means moving it one bit at a time.
| The 1.16.1 way | 65. Clear both, use 32 to move the source into the destination and a scratch unit (Kakaru) while draining the source, then 32 more to restore the source from the scratch. The source is consumed, so you need a copy to put back. |
|---|---|
| Remastered DeathsX | 33. The bitmask condition lets you test each bit without draining the source, so the scratch unit and the restore pass both disappear. |
This is where "reads are expensive and writes are cheap" comes from. A write is one action. A read has to move the value through several triggers to wherever it is going to be used. So reading once and reusing beats reading the same value repeatedly, and it is the same reason the functions ending in _cp are faster: fewer steps to go through.
This is not an argument for hoarding variables. 72 bytes is cheap and readable code is worth far more. The point is knowing why a loop that reads the same value ten times every cycle is slow - and why it does not look slow when you read the code.
It is also worth knowing where the 2,400 bytes of a trigger go.
| Previous · next trigger pointers | 4 + 4 bytes - in memory only, not in the map file. |
|---|---|
| 16 conditions × 20 bytes | 320 bytes. |
| 64 actions × 32 bytes | 2,048 bytes. |
| Exec flags · 27 player slots · current action | 4 + 27 + 1 bytes. |
One number falls out of that: in memory the first action's value field sits 348 bytes into the trigger - 8 + 320 + 20. Every piece of code that patches a trigger's action value from outside uses that 348, including UnitGroup and EUDVArray. In the map file it is 340 instead, because the pointer pair is not there. And the relation between Ptr and EPD is Ptr = EPD x 4 + 0x58A364.
That makes “where to put a record” - eight fields per unit, say - a cost question. There are three ways and the sizes differ a lot.
| One variable per field (what epScript's object does) | 576 bytes - what epScript's object does. The most flexible and the biggest; only the fields you use execute. |
|---|---|
| One eight-action trigger | 296 bytes. When all eight are used it needs one nextptr change instead of eight; but using one field still runs all eight actions. |
| Plain contiguous memory (Db, EUDArray, death counts) | 32 bytes. The smallest, and not trigger-shaped, so copying is expensive. |
Eight actions is the overlap sweet spot: eight triggers of eight actions is 296 x 8 = 2,368 bytes, while one trigger of 64 actions is 2,376. Past eight, more actions per trigger stops paying.
The 32 bytes of the third way are paid back at the use site. Without caching it is a read per access; with caching it is a 132-byte skip trigger plus three actions at every place that reads it. The cache machinery outweighs what the struct saved.
And there is a measurement that contradicts the table: an epScript-object linked list came out slower than a cache-free read-every-time Db, while using more space. Some of that is unoptimised example code on both sides, but the stated root cause is that euddraft does not optimise. So measure with epTrace rather than reasoning from the sizes.
Finding what is making the map lag