Performance issue on ConstantPath computation

  • A* version: 5.2.4
  • Unity version: 6000.0.33f1

Hello, I have a grid graph whose resolution is 24*24, and I have performance issue when computing a ConstantPath to get the reachable tiles for one character. My measurements give me around 20ms to compute one, and seems constant whatever the range/traversal cost I ask for. There is a big difference between the time I measure by myself and the time logged by Astar (most of the time between 0 and 1ms). Here is my test code :

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        m_Astar = FindFirstObjectByType<AstarPath>();
        m_Seeker = FindFirstObjectByType<Seeker>();

        StartCoroutine(TestSearchRanges());
    }
}

private IEnumerator TestSearchRanges()
{
    int range = 3;
    while (range <= 16)
    {
        yield return SearchMoveRangeUsingSeeker(range, new Vector3(20.16f, 0, 20.16f));
        range += 1;
    }
}


public IEnumerator SearchMoveRangeUsingSeeker(float range, Vector3 unitPos, OnPathDelegate pathDelegate = null)
{
    double timeStart = Time.timeAsDouble;

    int gscoremax = Mathf.RoundToInt(range * m_Astar.data.gridGraph.nodeSize * Int3.Precision) + 1;
    ConstantPath cpath = ConstantPath.Construct(unitPos, gscoremax, null);

    cpath.Claim(this);
    m_Seeker.StartPath(cpath, pathDelegate);

    yield return cpath.WaitForPath();

    cpath.Release(this);

    double duration = Time.timeAsDouble - timeStart;
    Debug.LogError("range compute time for range = " + range + " : " + duration);
}

I’m wondering why I get a such difference between the Astar log and what I calculate by myself, and why it takes so long. Is there a way to optimize that ? Currently it takes too much time to be usable for our needs (compute dozen ranges at one moment), did I missed something ? Thanks.

Hi

A coroutine will only run one step per frame. So when you call yield, unity will never resume the coroutine until the next frame.
If you want to block until the path is calculated instead, you can use cpath.BlockUntilCalculated().