Grid graph initial path speed

Hi,

I’m using a fairly large grid graph for an open terrain (500X500 nodes). I’m using the AIPath script and assigning a target through scripting. The initial path seems to take a long time, often 3 or 4 seconds (the console shows about 520 milliseconds, but it seem longer). After that, it drops to a couple of milliseconds for each path update, which is great. Is there a way to improve the initial pathfinding speed? What’s happening is that my AI just sits there for a few seconds walking in place, then after a few seconds pivots toward the target and starts moving forward. After that, everything is great. It works perfectly. I’ve cached the graph so I don’t think that scanning is the problem, but I’m not sure.

Hi

The first path will kick in the JIT compiler since that’s the first time those functions are called. The JIT compiler takes some time to do it’s job so the first path will be slower. 520 milliseconds is indeed quite large (I usually get just 2 or maybe up to 4 times longer calculation times for the first path), it might be that the initial path is very long and the JIT makes everything take a longer time instead of adding a constant delay, I am not an expert in how the JIT works. What you could do is to simply call a very short path at start to trigger the JIT and make your later initial path request (hopefully) faster.
Something like
Path p = new Path (Vector3.zero, Vector3.zero, null); Pathfinding.active.StartPath (p);
Which should be done before anything else (once would be enough). But make sure you don’t call it in Awake, then the pathfinding graphs might not have been scanned.

It probably seems like longer since the log will say how much processing time it spent on the path, not the total time difference between the request time and the time the path finished calculation.