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

Why chunk has size 16k?

Discussion in 'Entity Component System' started by lijianfeng, Apr 6, 2020.

  1. lijianfeng

    lijianfeng

    Joined:
    Sep 8, 2015
    Posts:
    54
    I read the entities source code,and I find the chunk size is 16K,I known the chunk was designed to be cpu cache friendly and soa data structer,but I don't know why it size was 16K,any theory under it? can you share it ?
     
  2. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    I'm pretty sure it's simply so that the entire chunk's maximum load of 16kb can be copied directly into the cache so that the work can be done and then updated chunk has it's values assigned to the original. This is tied to the decision for all component data to be structs so that they are passed by value instead of by reference. So the chunk literally passes over all it's componentdata by value to the cpu cache and after the work is done the components are passed back by value.

    As for the size of the chunk itself, that's literally due to the fact that cache sizes grow in powers of 2, and across different cpu architectures and cache levels, 16kb strikes the best balance between being able to fit into most caches as a single "chunk" of data to process and cramming as much data into one chunk as possible (This is the best of my understanding but I'm sure there's like another 50 people in this forum that can explain this point better than I can).

    Since chunks allocate up to 16kb worth of data, each chunk can fully utilise the cache of each cpu core in parallel. This is why Entities.ForEach uses IJobChunk under the hood instead of IJobParallelFor.