Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to pass a lookup table in all jobs within an update function?

Discussion in 'Entity Component System' started by Aleksei_Lund, Jun 17, 2020.

  1. Aleksei_Lund

    Aleksei_Lund

    Joined:
    May 28, 2019
    Posts:
    3
    Hi all,
    I tried to find a proper question throughout the Internet, but so far cannot find it.

    I have a big lookup table LUT (~100k elements) generated in the very beginning of my unity simulation. My parallelfor loops should read some chunks of this table during each update. The question is how to organize a nativearray to be accessible from different jobs, and then dispose it at the very end of the simulation.

    If I assign nativearrays based on the LUT every update, it takes too much time. Is it possible to create a nattivearray NALUT that consists of all elements from the LUT once and access it as many times as required? Thanks in advance!

    The code below shows my procedure of copying LUT into NALUT. Unfortunately, it takes a couple of milliseconds.
    Code (CSharp):
    1. NativeArray<T> NALUT= new NativeArray<T>(LUT.Count, Allocator.TempJob);
    2. NativeArray<Vector2Int> NALUTIndexes = new NativeArray<Vector2Int>(LUTIndexes.Count, Allocator.TempJob); // LUTIndexes show edges of chunks, which are not overlapping
    3. for (int i = 0; i < LUT.Count; i++)
    4. { NALUT[i] = LUT[i];}
    5. for (int i = 0; i < LUTIndexes.Count; i++)
    6. { NALUTIndexes[i] = LUTIndexes[i];}
    7.  
    8. NALUT.Dispose();
    9. NALUTIndexes.Dispose();
     
  2. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    Can't you make your NALUT array persistent and only create it when you create LUT?
     
  3. Aleksei_Lund

    Aleksei_Lund

    Joined:
    May 28, 2019
    Posts:
    3
    If I do this, how to properly dispose NALUT? It is continuously used in Update and should be disposed after simulation stops.
     
  4. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    If you keep the collection on a Monobehaviour, you can dispose it on OnDestroy or OnDisable (make sure to check NALUT.iscreated), if you keep in on a SystemBase OnStopRunning should do it