Search Unity

Writing to a DynamicBuffer across multiple Jobs

Discussion in 'Entity Component System' started by AndesSunset, Mar 17, 2019.

  1. AndesSunset

    AndesSunset

    Joined:
    Jan 28, 2019
    Posts:
    60
    Right now I have a regular componentSystem, which iterates over a bunch of message entities. Each of these messages includes information about a new element that needs to be added to the same, single DynamicBuffer.

    I’m trying to figure out if there’s a good way to Jobify this system, that will give improved in performance over doing it all on the main thread.

    I don’t believe there’s any way to safely async Add() to a DynamicBuffer. And I can’t schedule the work to happen later using an EntityCommandBuffer.

    Do I have any other good options?

    Thanks for any advice!
     
    Last edited: Mar 17, 2019
  2. AndesSunset

    AndesSunset

    Joined:
    Jan 28, 2019
    Posts:
    60
    One thing I was considering was passing NativeArrays into Jobs, which the Jobs would add the Elements to. Then back on the main thread, I could loop over them linearly and add those elements to the DynamicBuffer in question.

    Not sure what kind of perf benefit this would lead to, though.
     
  3. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,266
    You probably don't want multiple threads touching the same DynamicBuffer at the same time (assuming that buffer is relatively small) as that will cause false sharing. Otherwise you can use IJPCD.ScheduleSingle() and pass it a BufferFromEntity and the single entity. This gives you burst and lets your main thread advance to the next system to begin parsing and scheduling more jobs.
     
    AndesSunset likes this.
  4. AndesSunset

    AndesSunset

    Joined:
    Jan 28, 2019
    Posts:
    60
    Thank you! That's a good idea.