[Safety System] Schedule 2 jobs in parallel if one modifies an irrelevant part of a struct?

Say I have a struct and 2 jobs like this:

struct Vertex
{
    public float3 position;
    public float2 uv;
}

struct ColliderJob : IJob
{
    [ReadOnly] public NativeArray<Vertex> vertices;
    [WriteOnly] public NativeReference<PMeshCollider> output;
    public void Execute() { /* read from position, ignore UV, write to output */ }
}

struct UVJob : IJob
{
    public NativeArray<Vertex> vertices;
    public void Execute() { /* read from position, write to UV */ }
}

These jobs should be able to run in parallel since the ColliderJob does not read from the UV field, which is the only thing the UVJob will modify. However the safety system (rightly) complains since I am actually modifying the contents of an array while in use by another job. Is there a way to tell the safety system to just take a chill pill for a bit and not make these jobs depend on one another?

1 Like

Try putting this attribute in front of the vertex array:
[NativeDisableContainerSafetyRestriction]

1 Like

this will trash the cache, as writing the uv invalidates the entire line (false sharing). it may be faster to run them serially.

you should have an array for positions and one for uvs

4 Likes

And then another job to combine them into their final form? Hmmm… this might work.