Profiling and performance improvements

Issue #269 new
joseasoler repo owner created an issue

Although I don't plan to confront all of the performance problems in ExtraModMod (that would consume too much time), I will add tools to profile performance better and include the most obvious and/or simple fixes. All of what I'm going to say would be tackled in More Naval AI, in order to allow everyone to profit from it.

I would need to start by porting the automated OOS testing feature to More Naval AI and requesting its inclusion. It would allow to test if any performance improvements cause OOS errors as painlessly as possible.

The next step would be including Caveman 2 Cosmos's profiler. It is very useable, includes a lot of info and it even supports thread profiling (which would come in handy if someone is feeling really adventurous). For more information see: http://c2cdev.blogspot.com.br/2013/06/multi-threading-support-infrastructure.html

After that, CFLAGS should be tested. K-Mod just adds /Og to what ExtraModMod is currently using, but it should also be worth the effort of checking if /Ox also improves this situation (see https://msdn.microsoft.com/en-us/library/59a3b321.aspx). /Oy- could also be considered (see https://msdn.microsoft.com/en-us/library/2kxx5t2c.aspx).

At this point, test: http://forums.civfanatics.com/showthread.php?p=14455377#post14455377

The idea I mention there is creating a wrapper for CvGlobalContext which would contain a lazy initialized cache of everything that can be obtained using getInfoTypeForString. Having this wrapper is a necessity to avoid "breaking" all existing getInfoTypeForString calls, as this would break all mods that depend on MNAI and also make more complicated to merge any mods into it. Sadly, the wrapper itself may slow down the game so much that the benefits of the cache are not good incomparison.

K-Mod also includes a lot of performance improvements. In general I don't intend to borrow them as most of them are in the AI code and I assume that More Naval AI will include them in their own time if they are feasible or desirable. Having said this, the following particular commit includes a few obvious improvements that could be added without much problems: https://github.com/karadoc/Civ4-K-Mod/commit/fa0d9878526ac2dcb29543c30ca04edd392ff3ad

After these changes, some generic profiling would be done in order to try to identify places in which the code can be made more fluid without requiring big changes. Whenever an issue is found, other mods (C2C, K-Mod, Realism Invictus, MoM...) should be checked in order to see if they already have a fix for that particular problem.


Another simple but potentially useful change would be reducing MAX_CIV_PLAYERS (currently at 50. This would also imply reducing MAX_PLAYERS and MAX_TEAMS, which have values based on MAX_CIV_PLAYERS. I doubt that even after many revolutions, puppet states and the like, the real number of players will ever be greater than 30, let alone 50.

There are 200 for loops in the DLL that do MAX_CIV_PLAYERS iterations, 421 for loops that do MAX_PLAYERS iterations. The first thing that most of them do is grabbing a reference to the player and checking if it is alive or not. There are also 125 for loops with MAX_TEAMS iterations, which also mostly check if the team is alive first. That's a lot of idle computations that are being done on all turns, this kind of operation is probably done thousands or even millions of times every turn.


The following issue contains the performance improvements that I'm not planing to tackle, because of the sheer effort and time they would require: https://bitbucket.org/Terkhen/extramodmod/issue/268/possible-performance-improvements


http://forums.civfanatics.com/showthread.php?t=515090

Comments (11)

  1. Log in to comment