Iterum
December 1, 2026·Coding

How I Fixed Iterum's FPS Drop When Selecting Big Armies

by Elias Wennerberg

How I Fixed Iterum's FPS Drop When Selecting Big Armies

Selecting a big army in Iterum used to drop the game to around 20 FPS and soft-lock the UI. The game kept running but clicks got stuck in a queue behind the army panel’s updates and could take anywhere from 10 seconds to forever to go through. The cause turned out to be the UI searching for its own elements by name, twice for every unit, every frame. This is how I tracked it down and fixed it.

How the UI Finds Its Elements

Iterum runs on the Evergreen Game Engine, which has its own UI system. Screens are parsed from declarative .gui files into a tree of UI elements that live on the graphics thread. The main gameplay thread doesn’t touch elements directly. It queues messages to the graphics thread, and each message finds the element it targets by name, searching everything below the element it starts from. These messages all start from the root of the UI. Oskar Norberg goes deeper into the .gui format and the message system in Crafting Iterum’s Animation Editor. For the army panel, the part of the tree that matters looks like this:

Hud
└─ ...
   └─ List lArmiesAndUnits
      └─ List lArmyUnits
         └─ CompositeButton ArmyUnitButton<id>   (one per unit)
            ├─ Element ArmyUnitButton_LeftCont
            │  ├─ Image Icon
            │  └─ Label Name
            └─ ColumnList ArmyUnitButton_RightCont
               ├─ Element HpBarFrame
               │  └─ Element ArmyUnitHp<id>
               └─ Element FoodBarFrame
                  └─ Element ArmyUnitFood<id>

Finding the Bottleneck

All numbers below were measured on the commits right before and after the fix: FPS in a debug build, profiler numbers in both debug and release.

On a test map, I selected an army of 50 units and ran Visual Studio’s CPU Usage profiler. In a debug build, the graphics thread spent 64% of its time processing messages, and UIElement::GetElementsByName alone took 29%. In release, the figures were 60% and 48%. In debug, the name comparisons were only about 2%, so the cost looked like it was all in the walk. Release disagreed: with the walk optimized, the comparisons (String::Equals) took 34%, most of the lookup’s time.

Why It Was So Slow

While an army is selected, the HUD updates the HP and food bar of every unit in it, every frame, with one message per bar:

HUD bar updates, every frame

for (int i = 0; i < units.Size(); ++i) {
    Unit* unit = units[i];
    // ...
    QueueGraphics(new GMSetUIf("ArmyUnitHp" + unit->Id2Str(),
        UITarget::SizeRatioX, unit->HpRatio()));
    QueueGraphics(new GMSetUIf("ArmyUnitFood" + unit->Id2Str(),
        UITarget::SizeRatioX, unit->FoodSuppliesRatio()));
}

With 50 units that’s 100 messages per frame, each one walking the whole tree from the root and allocating a new list at every element it visits. Every unit also added its own elements to the tree, so the cost grew with the square of the army size: double the army, four times the work.

Old lookup, before the fix

List<UIElement*> UIElement::GetElementsByName(const String& byName, UIFilter filter) const {
    List<UIElement*> list;                       // a new list, at every element visited
    for (int i = 0; i < children.Size(); ++i) {
        UIElement* child = children[i];
        if (child->name == byName)               // a name comparison, at every element
            list.AddItem(child);
        List<UIElement*> childResults = child->GetElementsByName(byName, filter); // recurse
        if (childResults.Size())
            list.Add(childResults);              // merge the child's results upwards
    }
    return list;
}

The graphics thread has a 30 ms budget per frame for messages, and the leftovers are put back for the next frame. Once the bar updates took longer than that, the queue started growing, and button presses ended up waiting behind old bar updates. Even with just 20 units selected, a button press could take 10 to 30 seconds to go through.

At 100 units, copying that growing queue became a bottleneck of its own. It’s a classic death spiral: the bigger the queue, the longer each frame takes to get through it, and the more gets left over for the next one.

Diagram of the death spiral: messages go over 30 ms, leftovers are requeued, a bigger queue to copy next frame

Caching Children by Name

I gave each UI element a cache of its children, keyed by a hash of its name. Names aren’t unique (every unit button has its own Icon), so each hash points to a list of elements. The cache is marked dirty whenever a child is added, deleted, copied or renamed, along with the caches of every parent above it. When rebuilding the cache, each element adds its children’s caches to its own, so a lookup on any element covers its whole subtree.

Name cache lookup

struct NameCacheEntry {
    UIElement* element = nullptr;
    String cachedName;
};

UnorderedMap<size_t, List<NameCacheEntry>> childElementNameCache;
bool childElementNameCacheDirty = true;

List<UIElement*> UIElement::GetElementsByName(const String& name, UIFilter filter) const {
    if (childElementNameCacheDirty)
        RebuildChildNameCache();

    List<UIElement*> list;
    const size_t nameHash = HashString(name);
    List<NameCacheEntry>* foundBucket = childElementNameCache.Get(nameHash);
    if (!foundBucket)
        return list;

    // Check the entries are still valid (same name, still in this tree),
    // otherwise rebuild and look again
    // ...
}

Trade-offs

The cache isn’t free. Every element adds its children’s caches to its own, so an element deep in the tree is stored in every cache above it, and a change down there rebuilds all of them on the next lookup. UI that changes every frame would pay that cost every frame.

It’s still a good trade for the army panel. The panel’s elements only change when a different army is selected, while with 50 units, the bar updates look up elements 100 times per frame, so one rebuild saves a lot of tree walks.

The cache also only covers the common lookup filters (None, Visible and TopMostInStack). The rest still walk the tree like before.

Results

On the same map with the same army, the game went from ~20 FPS to ~55–60 FPS and the UI was responsive again. In debug, message processing dropped from 604 to 144 samples over the same amount of time, and from 64% to 20% of the graphics thread. GetElementsByName disappeared from the top functions in both builds. Rendering’s share of the graphics thread rose from 18% to 60% in debug and from 30% to 65% in release.

At 100 units the lookups are gone from the profile too. In debug, message processing is still the biggest cost there, but in release, rendering went from 8% or less of the graphics thread to 50%, and message processing from 77% to 32%.

Visual Studio profiler before the fix, 50 units in debug, showing GetElementsByName recursing deep into the hot path

Before, 50 units (debug): message processing 64%, GetElementsByName 29%

Visual Studio profiler after the fix, 50 units in debug, with rendering dominating the graphics thread

After, 50 units (debug): message processing 20%, rendering 60%

Conclusion

Debug pointed at the tree walk and release pointed at the name comparisons, so profiling both builds paid off. The cache gets rid of both: one hash lookup, then a name check on the few elements that share that hash.

For players, it means you can select your whole army without the game grinding to a halt.