At the last post I wrote about how I was going to try out to use threads for the first time, to offload the work needed to be done by AIs on a different thread. It turns out things didn't go as I imagined initially. You see, a thread can run a single Method, and it only runs it once. If it finishes it, you have to re-tell the thread what to run, and initiate its execution again. And that also means that instead of having a thread running all the AI routines, you have to have a separate thread for each routine of each AI instance. And you know what ? Threads are expensive! It takes considerable time to get a thread running in parallel to the main thread. So can you guess the result for my test scenario ? Where 16 AIs are simultaneously active on screen ? The whole engine hanged and became unresponsive. Failure. Apparently the time it requires to start a thread is much more than the time the code of the routine takes to be executed. That means that using threads to offload the AI routine that updates each frame, is out of the question.
So what's next ? I ran the profiler again and found out that the Garbage Collector was eating most of the processor's power, whenever the AI routine ran. So I did some smart caching, and I got a marginally better frame rate. I kept checking for small improvements, removing some methods that are called by the main routine that runs each frame from it, and calling them at the start, plus some smart bool checking so some operations don't run every frame even when they don't have too, and with each small change, the frame rate would raise up a bit. With all these little changes I managed to raise the number of simultaneous AIs from 16 that I reported yesterday, to 24, and keeping a fair frame rate between 35 and 48.
Then I tried something else. In each frame the routine will be checking what the distance from the player is. And if the distance is greater than a certain amount, (yet to be decided) most of the code that used to run every frame won't. The idea is that if the AIs are far enough from the player, maybe behind some physical objects and thus not visible, there is no need to check for everything. But instead to be updating only basic stuff like if the AI is alive, and its position. This change lead to a frame rate that when the AIs are further than the certain distance I mentioned earlier, gets up to small 80s. Much better.
So now I can have up to 24 AIs fully active, with even better frame rates if they are X distance away from the player. That's 50% more AIs than what I could have yesterday. Cool!
So what's next ? I ran the profiler again and found out that the Garbage Collector was eating most of the processor's power, whenever the AI routine ran. So I did some smart caching, and I got a marginally better frame rate. I kept checking for small improvements, removing some methods that are called by the main routine that runs each frame from it, and calling them at the start, plus some smart bool checking so some operations don't run every frame even when they don't have too, and with each small change, the frame rate would raise up a bit. With all these little changes I managed to raise the number of simultaneous AIs from 16 that I reported yesterday, to 24, and keeping a fair frame rate between 35 and 48.
Then I tried something else. In each frame the routine will be checking what the distance from the player is. And if the distance is greater than a certain amount, (yet to be decided) most of the code that used to run every frame won't. The idea is that if the AIs are far enough from the player, maybe behind some physical objects and thus not visible, there is no need to check for everything. But instead to be updating only basic stuff like if the AI is alive, and its position. This change lead to a frame rate that when the AIs are further than the certain distance I mentioned earlier, gets up to small 80s. Much better.
With 22 out of 24 AIs being far away, the frame rate went from 30s to 80s. |
So now I can have up to 24 AIs fully active, with even better frame rates if they are X distance away from the player. That's 50% more AIs than what I could have yesterday. Cool!
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου