Aliasing and the job system
Read time 1 minuteLast updated 12 days ago
Unity's job system infrastructure has some limitations on what can alias within a job struct:
- Structs attributed with (for example,
[NativeContainer]andNativeArray) that are members of a job struct don't alias.NativeSlice - Job struct members with the attribute can alias with other members. This is because this attribute explicitly opts in to this kind of aliasing.
[NativeDisableContainerSafetyRestriction] - Pointers to structs attributed with can't appear in other structs attributed with
[NativeContainer]. For example, you can't have a[NativeContainer].NativeArray<NativeSlice<T>> - Job structs are by default structs, i.e. the address of the struct is not allowed to alias with its members.
[NoAlias]
It is possible to opt out of these aliasing guarantees for job structs and for their members by using the experimental attribute , which is gated behind the preprocessor define.
[Alias]UNITY_BURST_EXPERIMENTAL_ALIAS_ATTRIBUTEThe following example job shows how these limitations work in practice:
[BurstCompile]private struct MyJob : IJob{ public NativeArray<float> a; public NativeArray<float> b; public NativeSlice<int> c; [NativeDisableContainerSafetyRestriction] public NativeArray<byte> d; public void Execute() { ... }}
- ,
a, andbdon't alias with each other.c - can alias with
d,a, orb.c