Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Synchronized Buffer vs Struct Component, Which is Better?

Discussion in 'DOTS Dev Blitz Day 2022 - Q&A' started by Kmsxkuse, Dec 8, 2022.

  1. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    Say I have a constant amount of 9 floats that require synchronization per entity.

    Should I make a dynamic buffer with 9 elements each containing 1 float value field or a single struct component containing 9 floats?

    Each float will most likely be changing every frame.
     
  2. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    306
    Hey Kmsxkuse! The two approaches will be near identical, except:
    • Buffer length will be replicated.
    • Changemask generation will change between the two.
    • If you expect values to change together, and you use the struct, ensure `Composite = true` in the `GhostField`. This will ensure you only generate one changemask bit for the entire struct.
    My guess is that the struct will be marginally better from a bandwidth POV, but I'm unsure which will be better from a CPU POV. I.e. It will likely be very similar, so probably not worth worrying about. Focus on what is easier to code with (and reason about).

    Also, for concrete answers, nothing beats profiling:
    • Serialization size via the NetDbg tool is trivial.
    • CPU is harder, as you likely mostly care about prod speeds. You'd probably need a benchmark.
     
    Kmsxkuse likes this.
  3. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    882
    The dynamic buffer replication has some more costs in terms of CPU performance and really minimal bandwidth costs. Unless there is a specific reason for using buffers, I will use a struct (with proper buffer-like accessors).
    Given the small size I can also argue that the buffer is embedded in the chunk for slightly performance improvements. However, you are going to lose the advantage to offload that on the heap and potentially gaining some extra entities per chunk.
     
    Kmsxkuse likes this.