Search Unity

Question Applying Collections attributes to nested Native Collections

Discussion in 'Entity Component System' started by jkeon, Jan 16, 2023.

  1. jkeon

    jkeon

    Joined:
    Aug 13, 2020
    Posts:
    17
    Hi All,

    I have something like this in my project:

    public UnsafeParallelHashMap<uint, UnsafeList<int2>> ListsByID;

    This works just fine if only one job accesses it as you would expect.

    If the job goes wide and runs in parallel, I would need to add the
    [NativeDisableParallelForRestriction
    attribute. And if multiple different jobs were running at the same time I'd need to add the
    NativeDisableContainerSafetyRestriction
    attribute.

    However, both of those attributes only solve the access issues for the Hash Map. The nested UnsafeList doesn't get these attributes and so when I do something like this:

    Code (CSharp):
    1. [NativeDisableContainerSafetyRestriction]
    2. public UnsafeParallelHashMap<uint, UnsafeList<int2>> ListsByID;
    3.                
    4. //This is allowed across multiple jobs because of the attribute
    5. UnsafeList<int2> list = ListsByID[id];
    6. //This errors with range safety check because the List didn't get the attribute
    7. int2 element = list[index];
    I end up getting the
    System.IndexOutOfRangeException: Index X is out of range in container of 'Y' Length.
    error.

    Which is because the UnsafeList uses the
    CollectionHelper.CheckIndexInRange(index, Length);
    call before getting the element.

    Is there any way I can ensure that I can bypass these safety checks for nested collections?

    Thanks!
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    585
    This doesn't really sound like the kind of safety issue you seem to be thinking about. [NativeDisableContainerSafetyRestriction] doesn't factor into the range check which is causing an exception to be thrown. This just indicates that your index is wrong for the size of the list (like getting index 3 in a list of length 3), not anything to do with access safety.
     
  3. jkeon

    jkeon

    Joined:
    Aug 13, 2020
    Posts:
    17
    Ah, yes.. you are correct. I was barking up the wrong tree and my issue was completely different.

    Thanks for taking the time!
     
  4. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
  5. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    585
  6. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    You mean that with Unity's implementation you could get a different keyed element in between to identical keyed element in the memory layout if both he's share the same bucket ?
    So iterating over a key would have to jump memory slots ?
     
  7. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    585
    When's the last time you did map[key][index] with a multi hash map?
    The user code above specifically gets an indexed element off of a key. Multi hash map doesn't support that. At best, you would iterate a number of times to get the value at the index. There's no requirement on ordering within the multi hash map anyway. Trying to apply multi hash maps here doesn't sound useful.
     
  8. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    992
    I never did.
    A hash map uses a key if I need to get a single element form that key then it should be unique.
    Using a map of list to get a specific index from the list does not make sense to me.
    Even more so if the order of the list matters.

    I can't presume what the OP wants to do base on 3 lines of code out of context.
    I don't know what the index is. For all I know it could just be an iteration variable...

    I'm just pointing out the existence of a container that could replace the unsafe map of unsafe list he uses.

    If it helps great ! If it doesn't, then sorry to have wasted your time...