Hi, is there some simple method to access the NativeMultiHashMap(NMHM)'s unique key array for job code?
I’m working on a graph algorithm, the job will process the edges and generate a lot of <node, node_modification> pairs, the node key could appear multiple times as a node could be pointed to by multiple incoming edges.
Now a part of the algorithm needs to loop on the nodes in the hashmap for once each.
I’ve tried several methods to approach that:
I used a NativeHashMap in the same job of generating the NMHM, to get a unique node set. But there’s not a job type to loop on NativeHashMap;
I tried with an IJob running on NMHM and get the key-array, sort it, and acquire an unique array. It works, but it cannot use burst-compile or parallel, and the performance is extremely terrible.
Is there some simple way to make that? It would be great to have a job type can run on NativeHashMap or NativeQueue.
Well, that’s basically what I did in the second solution with IJob.
I copied the code out of GetUniqueKeyArray() and made a little change to make it work on Entity keys.
The only drawback is that It cannot parallel or burst, and takes a huge amount time.
I’ve figured out a method that can do it with parallel job + burst.
It’s kinda dumb and takes 3x memory, but it does work.
The point is to add two more containers:
NativeHashMap<K, bool>: it works as a Set, and use TryAdd() to test if a K has already appeared before;
NativeMultiHashMap<K, bool>: this works as the container of unique keys, and will be passed down to next job, and only insert when the NativeHashMap.TryAdd() test passes;
GetKeyArray working in burst is I think fairly new, I actually hit where it didn’t work for me just a couple of weeks back, on burst 1.0.4 and 2019.1. Although I can’t remember if it was NativeHashMap or NativeMultiHashMap. It complained about the allocator if I remember correctly.