[SOLVED] Halite CLI CPU usage


I’m currently working on my bot params optimization and run hundreds of games. And the game with 2 bots load all 4 core almost completly. But I don’t understand why halite CLI use about 40-45% of my CPU. I mean after map generation it only parses bot output and give them new game state, right?
P.S.: My CPU is i5-2450M 2.5GHz (it’s outdated I know :slight_smile: )


On Windows, the engine could be more efficient about reading bot input. The next version (1.2) should have improvements in this area, thanks to reCurse, so keep an eye out for the update (hopefully later this week).


Ok, thank you. Btw I use Windows on this machine.


reading input is not a problem. the engine is spending most of its time in update_inspiration() according to my samples and this function could benefit from some trivial improvements.


Ah, alright. I’ll find some time to profile the engine.


update_inspiration() has a terrible performance. Disabling inspiration results in execution time cut down by a factor of 3. 20s vs. 55s. Inspiration can be disabled by using a json file with game constants and passing its name to the game engine via --constants-file.

A sample file is at https://pastebin.com/fB3bsg6x or you can run
$ ./halite --print-constants blah

and change the value of INSPIRATION_ENABLED to false.


A perf run shows update_inspiration is only responsible for about 5% of the CPU time on Linux, not 66%. (This is with a 4-player game with a few hundred ships.) How are you testing this, and what platform are you on? (If you turn off inspiration and re-run, your bot is liable to behave differently; fewer ships would drastically speed up a game in terms of time the bot spends.)


You can set INSPIRED_BONUS_MULTIPLIER:0.0 to have identical scoring with and without inspiration enabled. I see the same performance difference on both Linux and Mac. My bots are generating over 100 ships each. You can also observe the difference with top.

I also think perf by default catches CPU time of the bots so you have to pass -i to it to avoid collecting bots stats.

Here is a flame graph from a sample run - https://svgshare.com/i/9DC.svg

update_inspiration - 57.65%
process_turn - 14.33%


Hmm, you’re right, not accounting for bot time it is more significant, though arguably it’s still rather insignificant compared to the amount of time bots spend processing, as well as the amount of time just reading from/writing to bots. We’ll look at some optimization here.


Game engine does not use any CPU time doing I/O. The select() call sleeps until data is available but the profiler shows the process is “executing” it - https://en.wikipedia.org/wiki/Asynchronous_I/O#Select(/poll)_loops


I think I was using an unoptimized, debug build

$ cmake -DCMAKE_BUILD_TYPE=Debug .



Yeah, that could do it. I did put in a change that drops the CPU usage a bit, but it’s not too big a deal I think.


It looks like the default build type was set to Debug back in June by an intern - https://github.com/HaliteChallenge/Halite-III/commit/bb6f5d162370e075d97a0560b1d16f7c30c05e3a

Not my fault. :innocent: