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.
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
public interface IJobNativeHashMapVisitKeyValue<TKey, TValue>
where TKey : struct, IEquatable<TKey>
where TValue : struct
{
void ExecuteNext(TKey key, TValue value);
}
I also have this more specialized job
public interface IJobNativeHashMapVisitKeyValueGroup<TKey, TValue>
where TKey : struct, IEquatable<TKey>
where TValue : struct
{
void Execute(NativeSlice<TKey> keys, NativeSlice<TValue> values);
}
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
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.