Europa Universalis IV

Europa Universalis IV

MEIOU and Taxes v3.0
Brute 22 Sep, 2023 @ 5:56pm
A potential optimization opportunity related to UI_clear decision in DISP-Select.txt
I used profiling tool to find out some potential opportunities to optimize MT, and I seemed to have discovered one. In my first profiling attempt using observer mode, it occurs to me that UI_Clear.potential has a significant time cost, apparently even higher than population census. Digging into decisions/DISP-Select.txt, the original code in question looks like this

potential={
ai=no
NOT={has_global_flag=multiplayer }
any_province={
OR={
has_province_flag=Pin_Show
has_province_flag=UI_Select
}
}
}

allow={
always=yes
}

In EU4, potential scope determines the visibility of decisions for all countries, including players and AI, and allow scope determines the conditions to enact this decision. The problem of potential scope is that it cannot be lazily calculated as the allow scope, and
any_province={
OR={
has_province_flag=Pin_Show
has_province_flag=UI_Select
}
is a very costly condition as it will literally need to go over every province and see if those flags exist or not.
The best way to optimize is to rewrite the select functionality in province UI so that it sets a separate global flag indicating that at least a province is selected, and UI_Clear can just check and act upon that flag instead of iterating over every province all the time. However, a makeshift solution I had is to move the any_province check into the allow scope. After doing so, the time ratio between UI_Clear and country.delayed_events in the second profiling changed from roughly 1:1 to 1:4! If the latter doesn't change significantly, that would mean a drastic improvement of one of the most costly script operations.