AI ABpath.calculateSteps() and general performance

Hello. So I started using the free version of your AI and it’s really something :slight_smile:

Currently making a simple town builder (inspired by banished, just on a smaller scale) andusing the generic AIpath script, without a character controller/rigidbody. Just basic raycasting to stick to the ground. After trying to stress test a simple scene with 400 entities I noticed that almost all (70-85% and reaching 12-20ms alone) of the process is used for a call ABpath.CalculateStep() > GridNode.Open()
The AIpath script is barely changed, just some logic added at ReachedDestination which has a very minimal impact.

So what exactly is ABpath.CalculateStep() > GridNode.Open() ? I’m guessing it’s used to create a new path towards a target? Is it normal for it to have such a huge footprint?

Also any general quick advice on optimization?

Thank you in advance for any response or advice you can provide :slight_smile:

Okay, this is weird, but after saving and reloading the project, i’m not getting any of the bottleneck from that function. It doesn’t even appear in the profiler. Don’t really get why it happened since i didn’t change anything else with the script. Now the only top 2 calculations that are being done are calculate velocity and rotate towards, so that’s good, since atleast now I know what those things are, what they’re doing and maaaaybe how to reduce the calls. Considering I don’t need the smoothest path in the universe.

Hi

ABPath.CalculateStep is part of the core pathfinding functionality. It is what calculates how to get from point A to point B as quickly as possible.

400 units will do quite a lot of path requests, so I am not surprised that it uses some CPU power.
I suggest that you enable multithreading (A* Inspector -> Settings) to offload pathfinding to a separate thread. Doing that will let you have the main thread all for yourself and the pathfinding system will have one thread all for itself. This will only result in performance improvements on 2+ core machines, but that’s pretty much all machines (and smartphones) nowadays (unless you plan to release to WebGL which is limited to a single thread).

THANK YOU. Now I know fully well what fixed it haha. It’s amazing how much of a performance boost it was.

Another quick question. I’m running a few tests with a couple hundred entities. Can run ~650 at 65-80fps and it all works fine. The problem is the beginning. They all start moving one by one. Takes a good 10-15 seconds to get everyone moving. After that there’s another delay for a repath to everyone who reached their path. Why could this be happening? It’s weird because at the last 100 entities or so, they all start their pathing at the same time.

Great that it works better now! :smile:

It still takes a while for all the paths to be calculated so your characters will not get their paths at the same time.

Check the log. By default it will log each path request and say how long it took to calculate it.
You can change this behaviour in A* Inspector -> Settings -> Debug -> Path Log Mode.

I don’t think this is the case, but it might be that returning the paths is being the bottleneck. Currently there is a hardcoded time limit of 1 ms that the system can use to return paths. You can try to see if increasing it helps. Take a look at AstarPath.cs -> ReturnPaths method.

In the pro version there are some ways to improve pathfinding performance even more. Such as using more than one additional thread, or using heuristic optimizations

Nope that didn’t help. Weirdly enough, even with 50 entities now, they all start moving one by one with a short delay between one another. Where before you couldn’t see the difference or maybe once every other time, but not with every single AI like it does now ;s .

Also, another question if you don’t mind.
Grid graphs…Wouldn’t it be better performance wise to create multiple of them?
Right now, I have a generated level that’s more or less static(the terrain never changes), except for object changes. Would it be possible to split the graph into multiple smaller ones, and then use that for pathfinding? Since if the map is of average size, i’m guessing multiple calls for a path request takes a long time on a single big graph?
Or for when updating an object that was built or destroyed.

Hi

Not really.
When updating a graph, the cost is roughly proportional against the number of nodes updated (there is a single pass over all nodes in the graph, but that pass is pretty fast, and if you have multiple graphs it would have to be done for all graphs anyway). When searching a graph, the cost is roughly proportional against the number of nodes that are searched (which doesn’t change regardless of how many graphs you split it up in).

Weird. I honestly thought that multiple graphs would mean better performance.

Still, that’s what pivot points are for, right?
I’m honestly really considering on just buying the pro version, considering it would let me push even more for better performance. I’m able to get some great results with free already, and I doubt i’d want more AI entities than what I already tested.

Overall, would you say your system could work in a city builder game? Imagine Banished(a game if you heard of it), but on a much smaller scale(~450 entities is the goal. 1000 is the dream). How do you personally feel your pathfinding would work in a similar environment?

Also thank you for all the responses :slight_smile: Considering i’m only using the free version, i’m suprised at how fast you respond.

Yeah, lots of other people think the same thing. I get that question on average about once every 1 or 2 months.

Pivot points? I’m sorry but I don’t quite follow you there.

I think it will work. It’s hard to say for sure since I know almost nothing about your game (the technical details), but I think it should work.

I am currently working on http://gamesfoundry.com/ which uses my system. It uses a lot of custom stuff (custom navmesh graph), but it definitely works there. 1000 entities might be a bit hard to handle, but 450 should be doable. You will probably have to optimise the movement scripts a bit since when moving 450 entities the cost adds up quickly.

It varies a lot. Sometimes I answer every post as soon as I see them in my inbox, other times I batch up the responses until I get the time. I usually do not differentiate much between users of the free version and the pro version. If people are happy with the support when using the free version they are often more positive to buying the pro version (which this thread seems to be a good example of :slight_smile: ).