Identifying and Fixing AI Performance Bottlenecks

Discussion of all aspects of the game engine, including development of new and existing features.

Moderator: Forum Moderators

Post Reply
giantspider52
Posts: 4
Joined: November 6th, 2021, 12:32 am

Identifying and Fixing AI Performance Bottlenecks

Post by giantspider52 »

Hello everyone! I wasn't sure if the WML workshop or technical support forum was more appropriate for this topic, although to be sure, this question might span the AI's implementation of either the WML, lua, or C++ (or my own usage of WML, WFL, and lua), so I posted this here.

I am updating my custom era add-on (which has not yet been published on the add-ons server) for BFW version 1.16. It was originally designed for version 1.14. In BFW version 1.14, the default AI (RCA) had no performance issues, although I noted that they existed for the experimental AI, where the game would freeze for a few seconds at the start of every AI player's turn, as well as sometimes after they have recruited units. Now that version 1.16's default AI has implemented features from the experimental AI, I am noticing this slowness with the new default AI as well. I run Wesnoth on a slow laptop from 2013 (updated to Windows 10), so I'm not sure if these performance issues are noticeable on newer, faster machines, although that is certainly something I'd like to test in the future. Anyways, the slow AI performance makes my game almost unplayable on larger maps, and I was able to determine that the likely reason was my own code (in other words, slow AI does not affect any eras other than my own, with very few exceptions) :( .

So, I have made it a priority to clean up my add-on code in order so that AI in my add-on's eras run at least almost as fast as eras I also like to play such as Ageless Era. Unfortunately, tough decisions have to be made that might involve removing features (and perhaps units and multiplayer factions connected to those features) in order to optimize my game's default AI. My add-on is very macro-heavy and event-heavy, in order to implement complex features such as: custom sprites/animations for unit recruits implemented by [object]s which "overwrite" their sprites, rare unit traits (such as half-elf or half-orc), custom micro-AIs, complex [disable] weapon specials which use lua functions or WFL, and units/features dependent on the version of my add-on that is running (for compatibility with replays running older versions of my add-on which lacked said units or features). This has understandably resulted in a lot of code bloat the AI probably has to consider, and troubleshooting which code is mostly responsible for slower performance by removing it is taking a long time.

While I mentioned that my add-on uses micro-AIs, admittedly, I have a very minimal understanding of the game's AI in general. I have played for years and had gotten used to playing (and defeating) the 1.14 default AI by taking advantage of how it makes decisions, although unfortunately this does not translate to understanding its code implementation. With that being said, I wouldn't yet be capable of trying to fix the default AI in my era, and the only surefire solution remains to make all AI use the old default AI (RCA) to restore AI performance.

So, what I'd like to know is, does there exist any utilities or debug options for the game that might be able to help me identify where the AI is spending most of its time "thinking"? Right now, most of my troubleshooting is relying on removing code and seeing if the AI runs faster, although this process is kind of time-consuming :doh: . I program as a hobby and have occasionally used tools like memory profilers to optimize my code, although I wasn't sure if utilities like these existed for Wesnoth. I've usually Googled most of my issues in Wesnoth because I rarely like to ask for help :oops: hehe~
User avatar
Pentarctagon
Project Manager
Posts: 5564
Joined: March 22nd, 2009, 10:50 pm
Location: Earth (occasionally)

Re: Identifying and Fixing AI Performance Bottlenecks

Post by Pentarctagon »

In the advanced preferences there's a "logging" option, which has a variety of options for the AI. A lot of the core AI is written in lua as well, so you would be able to add some of your own temporary debugging to that (though they would be overwritten on updating).
99 little bugs in the code, 99 little bugs
take one down, patch it around
-2,147,483,648 little bugs in the code
giantspider52
Posts: 4
Joined: November 6th, 2021, 12:32 am

Re: Identifying and Fixing AI Performance Bottlenecks

Post by giantspider52 »

Ah, I see. Wow, I totally overlooked that actually! :lol: Thank you for the advice! I'll be sure to look into that.
Post Reply