[4.1.22]Update tags and penalities

Hi,
I update 32 grid nodes for change hers penalties and tags and with this version perf are better ( was 310ms) but it takes again 4 ms to update in astarpath.update.
My gridgraph is huge : 1200*1200

I want to know if this time is normal or not. I don’t change walkability of nodes.

Thanks

Hi

How are you updating the graph?
That does seem a bit high. I tried updating the tags of 200 nodes in a 1024*1024 grid graph and that took around 0.05 ms. If I however use ‘updatePhysics=true’ on the GraphUpdateObject so it recalculated walkability too, then it takes about 2.5 ms.

Here my update script

using Pathfinding;
using System.Collections.Generic;
using UnityEngine;

namespace HMH.Runtime
{
    public class UpdateGridGraphManager : MonoBehaviour
    {
        public void Init()
        {
            _updateAction = new AstarWorkItem(UpdateAction);
        }

        public void UpdateNode(UpdateGridNode data)
        {
            lock (_nodeWaiting)
                _nodeWaiting.Add(data);

            AskUpdate();
        }

        private void LateUpdate()
        {
            lock (_nodeWaiting)
            {
                if (_nodeWaiting.CountD > 0)
                    AskUpdate();
            }
        }

        private void AskUpdate()
        {
            if (_updateRequested)
                return;

            _updateRequested = true;
            AstarPath.active.AddWorkItem(_updateAction);
        }

        private void UpdateAction(IWorkItemContext wic)
        {
            UpdateNodeAction();

            _updateRequested = false;
        }

        private void UpdateNodeAction()
        {
            lock (_nodeWaiting)
            {
                _nodeUpdating.UnionWith(_nodeWaiting);
                _nodeWaiting.Clear();
            }

            var gg = AstarPath.active.data.gridGraph;

            foreach (var updateData in _nodeUpdating)
            {
                var node = gg.GetNearest(updateData.Position).node;

                node.Penalty = (uint)((int)node.Penalty + updateData.PenaltyDelta);

                if (updateData.UpdateTag)
                    node.Tag = (node.Tag & ~updateData.TagToRemove) | updateData.TagToAdd;
            }

            _nodeUpdating.Clear();
        }

        #region Variables

        private static UpdateGridGraphManager _instance;

        private Liste<UpdateGridNode>   _nodeWaiting  = new Liste<UpdateGridNode>();
        private HashSet<UpdateGridNode> _nodeUpdating = new HashSet<UpdateGridNode>();
        private AstarWorkItem           _updateAction;
        private bool                    _updateRequested;

        #endregion

        #region Proprieties

        public bool HasRequestedUpdate { get { return _updateRequested; } }

        #endregion
    }

    public struct UpdateGridNode
    {
        public Vector3 Position;
        public bool    UpdateTag;
        public uint    TagToAdd;
        public uint    TagToRemove;
        public int     PenaltyDelta;
    }
}

is there a way to say to not recalculate walkability in a AstarWorkItem ? Because I think the problem come from here.

AstarWorkItem is essentially just a fancy callback for when you want to execute code when it is safe to update the graphs. It does not know about walkability at all. If you don’t update walkability inside the callback, then walkability will not be updated at all.
Can you post a screenshot of the Unity profiler when this happens? Perhaps as well with deep profiling enabled?

Sadly, no :frowning:
This picture show the profiler for 32 nodes without deep profiling

The same situation with deep profiling

I don’t know why hashset.unionwith is so slow …

By the way, do you have a better way to find a node from a position ?
Because GetNearest loop over all the nodes .
Do you have considered a quadtree of something like that ?

GetNearest is overriden in the GridGraph class. It really shouldn’t be that slow. Do you think you could expand the profiler for the GetNearest call?

Are you using Heuristic Optimization? It seems like that is taking some time (it is implicitly called by the ‘Release’ call)

Nope

Weird. I cannot see anything that seems to go wrong, things just seem to take a lot of time for some reason. The GetNearest method is a pretty simple method for grid graphs.

Also. When profiling in the Unity editor, make sure Unity settings -> External Tools -> Editor Attaching is disabled. Otherwise code in the Unity editor runs a lot slower due to a lot of debugger support code being added.

Oh, I will test that asap

a little better