GetNearest Performance

Hey there,
I use GetNearest for find out when tag is changed and also GetNearest on PointGraph for finding NodeLink(s) and it is done in each frame and upto 50 agents.

CurrentNodeTag = AstarPath.active.GetNearest(transform.position).node.Tag;
var node = AstarPath.active.data.pointGraph.GetNearest(transform.position).node;

I noticed GetNearest is slow in my situation. As you can see in below image. (Profiler)
(27 agents)

How can I fix this issue ?

Hi

Are you using the free or pro version?

Pro version …

Would you mind posting your point graph settings?

It is made automatically with NodeLink2.

image

And it is LayeredGraph which I use in my game.

Ah. I’d recommend that you check the ‘optimize for sparse graph’ toggle. That will improve the performance.
It really should be ticked by default for the node link graph.

Also Settings if you want to know.

image

Okay, I will check it out now.
What about GetNearest for layered grid graph ?

GetNearest for layered grid graphs should be fairly fast as long as the query point is not a very long distance away from the nearest node.

But as you can see that AstartPath.GetNearest() is on 10.2% in my profiler image

Yes but I expect that is the point graph’s get nearest function.

It is for both of them I think.

In UpdateHeightRegion I use this line:

CurrentNodeTag = AstarPath.active.GetNearest(transform.position).node.Tag;

And in CheckNodeLinks I use this one:

var node = AstarPath.active.data.pointGraph.GetNearest(transform.position).node;

And both of them are called each frame for each agent.

Note that the first one will search all graphs (including the point graph).
The second one will only search the point graph.

In any case, since you already seem to be using the profiler in deep profiling mode you can expand that GetNearest item in the profiler to see what is the slowest part.

You are totally right. First GetNearest problem is because of PointGraph GetNreast function.
And GetNearest of PointGraph is fixed by enabling Optimize For Sparse Graph which I should read about it in doc :smiley:

So if I want to just GetNearest on Layered Grid Graph I should use this line ?

AstarPath.active.data.layeredGridGraph.GetNearest(transform.position)
1 Like

That will work as long as you do not need to use a constraint on your search.
The actual recommended way is to use a graphMask on an NNConstraint struct.

// A 'None' NNConstraint doesn't filter anything, but you can configure it to only return walkable nodes for example
NNConstraint nn = NNConstraint.None;
// Search only that graph, you can also specify multiple graphs by ORing values together
nn.graphMask = 1 << AstarPath.active.data.layeredGridGraph.graphIndex;
AstarPath.active.GetNearest(position, nn);
1 Like

For this line:

nn.graphMask = 1 << AstarPath.active.data.layerGridGraph.graphIndex;

It says Operator << cannot be applied to operands of type int and uint

Oops. Forgot a conversion there.
Also I remembered I have added a better API for this:

nn.graphMask = GraphMask.FromGraph(AstarPath.active.data.layeredGridGraph);
1 Like