Feature Request: IJobNativeMultiHashMapVisitKey

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

[BurstCompile]
private struct ExtractMapKeysJob : IJob
{
    [ReadOnly] public NativeMultiHashMap<float3, TileLink> MultiHashMap;
    [WriteOnly] public NativeArray<float3> KeyArray;

    public void Execute()
    {
        var tmpArray = MultiHashMap.GetKeyArray(Allocator.Temp);
        KeyArray.CopyFrom(tmpArray);
        tmpArray.Dispose();
    }
}

[BurstCompile]
private struct InstantiateAdjacentTilesJob : IJobParallelFor
{
    [ReadOnly] [DeallocateOnJobCompletion]
    public NativeArray<float3> TileLinkHashMapKeyArray;

    [ReadOnly] public NativeMultiHashMap<float3, TileLink> TileLinkHashMap;

    [NativeDisableParallelForRestriction]
    public BufferFromEntity<AdjacentTileBufferElement> AdjacentTileBufferLookup;

    [WriteOnly] public EntityCommandBuffer.ParallelWriter EcbWriter;

    public void Execute(int i)
    {
        var tileKey = TileLinkHashMapKeyArray[i];
        if (!TileLinkHashMap.ContainsKey(tileKey)) return;
        var tileLinksEnumerator = TileLinkHashMap.GetValuesForKey(tileKey);
        if (!tileLinksEnumerator.MoveNext()) return;

       ...
     }
}

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.

Checkout following thread.
May be of use for you, since hashmap jobs are depreciated from older Entities preview.

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 !

You are welcome. :wink:

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

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 ?

1 Like