Search Unity

Is there a ParallelWriter for DynamicBuffers?

Discussion in 'Entity Component System' started by davenirline, May 29, 2021.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    NativeList has a method AsParallelWriter() while I don't see one for DynamicBuffers. I want to be able to add items to it in parallel. I guess a rephrase of the question is can I add items to DynamicBuffers in parallel? Is that safe?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Nope. But you can for example resize Dynamic buffer prior parallel job, then write to the buffer in the parallel, with no size change.
     
    davenirline likes this.
  3. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    729
    You can also use EntityCommandBuffer.ParallelWriter.AppendBuffer(). Though I wouldn’t recommend it unless there weren’t other options.

    Preallocating your DB to the max size you need, and then writing to it in parallel from multiple jobs (assuming none of those jobs ever touches the same index in the DB), is usually a better way to go.
     
    Antypodish and davenirline like this.