Currently, cmpBarter.PlayerHasMarket is polling-based: it goes through all the entities of the given player, queries cmpIdentity and cmpFoundation from each of them, and tries to find a built market.
Since that function is called each turn in order for the GUI to allow or disallow bartering, this is a huge performance hit.
I detected it using the SpiderMonkey tracelogger while working on upgrading SM. The tracelogger estimates that 15% of the JS execution time is spent in this function...
This patch makes the function event-based. When a market is built or captured, it is added to a list of markets in the Player component. When a market is lost, it is removed from the list. The function now just checks whether the list is empty.
Performance comparison in a non-visual replay, on the current SVN, using some AIs:
The perf loss from the previous code is even more important under SM52.