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.

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


    Feb 15, 2019
    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


    Unity Technologies

    May 18, 2021
    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


    Unity Technologies

    Mar 18, 2020
    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.