Search Unity

Feedback Feature Request: IJobNativeMultiHashMapVisitKey

Discussion in 'Entity Component System' started by Deleted User, Apr 15, 2021.

  1. Deleted User

    Deleted User

    Guest

    Hi,

    Not sure if this is an appropriate way of asking or if maybe this already exists, I'd love to know.
    Long story short, I have a job that wants to iterate over each key in my NativeMultiHashMap.
    I found out about JobNativeMultiHashMapVisitKeyValue but from my understanding, it's going to iterate over each Key-Value pairs which means I'm going to have multiple workers running with the same key value.
    I use said key to instantiate unique entities and therefore this isn't the behavior I'm looking for

    I think some way of iterating over each key in the map would be nice.

    The workaround I found is that I allocate an array for the keys that I copy and iterate over in an IJobParallelFor

    Code (CSharp):
    1. [BurstCompile]
    2. private struct ExtractMapKeysJob : IJob
    3. {
    4.     [ReadOnly] public NativeMultiHashMap<float3, TileLink> MultiHashMap;
    5.     [WriteOnly] public NativeArray<float3> KeyArray;
    6.  
    7.     public void Execute()
    8.     {
    9.         var tmpArray = MultiHashMap.GetKeyArray(Allocator.Temp);
    10.         KeyArray.CopyFrom(tmpArray);
    11.         tmpArray.Dispose();
    12.     }
    13. }
    14.  
    15. [BurstCompile]
    16. private struct InstantiateAdjacentTilesJob : IJobParallelFor
    17. {
    18.     [ReadOnly] [DeallocateOnJobCompletion]
    19.     public NativeArray<float3> TileLinkHashMapKeyArray;
    20.  
    21.     [ReadOnly] public NativeMultiHashMap<float3, TileLink> TileLinkHashMap;
    22.  
    23.     [NativeDisableParallelForRestriction]
    24.     public BufferFromEntity<AdjacentTileBufferElement> AdjacentTileBufferLookup;
    25.  
    26.     [WriteOnly] public EntityCommandBuffer.ParallelWriter EcbWriter;
    27.  
    28.     public void Execute(int i)
    29.     {
    30.         var tileKey = TileLinkHashMapKeyArray[i];
    31.         if (!TileLinkHashMap.ContainsKey(tileKey)) return;
    32.         var tileLinksEnumerator = TileLinkHashMap.GetValuesForKey(tileKey);
    33.         if (!tileLinksEnumerator.MoveNext()) return;
    34.  
    35.        ...
    36.      }
    37. }
    One of the annoying problems with this workaround is that you have to know the maximum size of your key array and check if the key you're iterating over is not uninitialized.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
  3. Deleted User

    Deleted User

    Guest

    Oh wow.

    Well, 1st of all thanks a lot.
    2nd of all... Just what the hell lol. Why is that so convuloted and so not in the documentation.
    At least that explains how I was getting a length mismatch in my extractMapKeys job if the keys aren't unique.

    I see you guys talking about some sort of fix coming in the next update back then, has this not been fixed after all ?

    Thanks for the help !
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    You are welcome. ;)

    Not really. These hash map jobs will be / has been removed in later entities packages.
    So you need different solution all together.
     
  5. Deleted User

    Deleted User

    Guest

    They're still in my 0.17 and I didn't see a deprecation notice yet.

    Is it just the jobs that are gonna be deprecated or should I be walking away from nativehashmaps ?
     
    PublicEnumE likes this.