Search Unity

Job Type to iterate over a NativeHashMap (Not a NativeMultiHashMap)?

Discussion in 'Data Oriented Technology Stack' started by PublicEnumE, Nov 26, 2019.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    161
    We can use IJobNativeMultiHashMapVisitKeyValue to loop over key/value pairs in a NativeMultiHashMap.

    But is there a good way to loop over the key/value pairs of a regular NativeHashMap?

    Since NativeHashMaps can't be accessed by index, IJobParallelFor can't be used without called GetKeyArray() on the hashmap. That call can be quite slow, so I'm hoping there's an alternative which works like IJobNativeMultiHashMapVisitKeyValue.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    1,955
    I wasn't going to make my common library public yet as I wanted to clean up a few things first but here ya go, I have a couple of custom jobs for this

    https://gitlab.com/tertle/com.bovin...Collections/IJobNativeHashMapVisitKeyValue.cs

    Code (CSharp):
    1.     public interface IJobNativeHashMapVisitKeyValue<TKey, TValue>
    2.         where TKey : struct, IEquatable<TKey>
    3.         where TValue : struct
    4.     {
    5.         void ExecuteNext(TKey key, TValue value);
    6.     }
    I also have this more specialized job

    https://gitlab.com/tertle/com.bovin...ctions/IJobNativeHashMapVisitKeyValueGroup.cs

    Code (CSharp):
    1.     public interface IJobNativeHashMapVisitKeyValueGroup<TKey, TValue>
    2.         where TKey : struct, IEquatable<TKey>
    3.         where TValue : struct
    4.     {
    5.         void Execute(NativeSlice<TKey> keys, NativeSlice<TValue> values);
    6.     }
    7.  
    This groups a few keys together (up to the indicesPerJobCount) in a slice and passes it to a job.
    This is useful if you need to do allocations on an algorithm so you don't have to do them per entity.

    Anyway there is a bunch of other stuff available in this library people might find interesting

    https://gitlab.com/tertle/com.bovinelabs.common
     
    vildauget and PublicEnumE like this.
  3. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    161
    Thank you very much for posting this. It's an impressive piece of work, and I see why you needed to make all these imposter collection types (Unity hides the neccessary fields to write something like this using their native types).

    Unfortunately for this work, using a non-officially supported library (as elegant as it may be) isn't a liability-safe option. It's frustrating, since Unity already wrote very similar to code to make IJobNativeMultiHashMapVisitKeyValue. It's right there. I wonder why they didn't also include a NativeHashMap version.

    Right now I'm looking at just writing and maintaining my own imposters.

    Can anyone from Unity please comment?
     
    Last edited: Nov 26, 2019
  4. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    161
    Alright, I've rolled my own, based on Unity's source. Seems to work so far. More tests are required.
     
unityunity