Unity-Technologies / 2d-extras

Fun 2D Stuff that we'd like to share!
Other
1.54k stars 342 forks source link

GridInformationKey should implemented IEquatable<GridInformationKey> and override GetHashCode() to avoid GC alloc #344

Open tanghuipang opened 2 years ago

tanghuipang commented 2 years ago

as GridInformationKey used as a Dictionary key, current version will alloc 292B GC when call dict.TryGetValue(). For better performance it should be like below:

    internal struct GridInformationKey : IEquatable<GridInformationKey>
    {
        public Vector3Int position;
        public String name;

        public bool Equals(GridInformationKey key)
        {
            return position == key.position && name == key.name;
        }

        public override int GetHashCode()
        {
            return position.GetHashCode() + name.GetHashCode();
        }
    }
ChuanXin-Unity commented 2 years ago

Thanks! We will check out this issue!