Tuesday 11 December 2018

Improving AI Performance

Hi all, Steve here!  Here's a look into what I've been working on recently: 

There's been some monstrous performance spikes in our AI, and I've spent time over the last month working to mitigate this issue.  Two significant changes have been made to improve this:

1 - the addition of a pool of Dictionaries to store states, and
2 - making each state item immutable

This vastly reduces object creation, which is slow, and reduces the number of objects in existence, which helps reduce memory fragmentation (another Bad Thing) - which, being C#, is harder to control than in C++.


Dictionary Pool

So what's a Dictionary Pool, you may ask! Well, at its simplest (which this is), it's a big list of pre-made Dictionaries, each with a flag on to indicate if it's being used.

When we want a Dictionary, we grab one from the pool, put it in a list of 'in-use' Dictionaries, and mark it as being used. When the object using it is destroyed, we mark it as free, and put it back in the pool for someone else to use. The key here is to be very strict on who is using your object! It's critical that nobody is using it when it's marked as free, or the whole concept breaks down.

Immutability

And immutable states?  Immutability is another weird software-engineer-y word. In english, it means something that never changes. But that's perhaps misleading when it comes to thinking about objects in Unity - what use is an object you cannot alter?

The answer is that while you can't change the object, you can create a new one to reflect this change. This means you have precisely one instance of any particular combination of object.  For us, this means that instead of creating hundreds of thousands (no exaggeration) of states, all duplicates of one another, we create... about seventy. The overhead of querying a Dictionary for the instance of the state we want is much, much less than creating a tiny object, then destroying it, repeatedly.



These two changes weren't trivial, as such, but were fairly self contained, and only produced a handful of super frustrating bugs that I had to then work against for a week or two.

Back within 60fps, but still a lot of garbage to deal with!

But the good news is that performance improved drastically!  We are now back to 60fps within the editor on my machine, mostly. Which was a relief because I feared for a moment or two that I'd accidentally extended the AI system in such a way that it would never be useful in a finished game, which would have been embarrassing.

-Steve

If you haven’t already - be sure to wishlist Off Grid on Steam - each wishlist makes a big difference to us, and we really appreciate your support!

No comments: