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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Can someone explain me how ScratchpadAllocator works?

Discussion in 'Entity Component System' started by vectorized-runner, Oct 21, 2022.

  1. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    Is it better than using temp memory? Which good use cases does it have? How do I use it properly?
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Note that I haven't used Unity's particular Scratchpad allocator, but the concept looks pretty familiar to me, given my experience with memory management.

    Consider the definition of AllocateNativeList:

    "Allocate a NativeList of type T from memory that is guaranteed to remain valid until the Scratchpad allocator is Rewound (under user control). There is no contention for this memory between threads. There is no need to Dispose the NativeList so allocated. It is not possible to free the memory by Disposing it - it is automatically freed when the Scratchpad is Rewound."

    A "normal" allocator has a big chunk of memory that it serves allocations from. It also keeps track of freed blocks. When you ask for an allocation, it checks if there are any freed blocks and hands you one of those if possible. Otherwise, it slices off a piece of that chunk and hands it to you.

    (edit: i can't find the source for this, but i swear i read that Allocator.Persistent just passes the request along to your operating system, such as by calling malloc)

    Keeping track of freed allocations means that you can re-use memory, but it also means that you have to keep track of the stuff! That's non-trivial. Over time, you can also wind up with fragmentation: repeated allocations will give you chunks from all over the place, which can hurt performance.

    A Scratchpad is a big chunk of memory. When you want to allocate a list, you just slice a piece off the end and use that. You don't ever free allocations -- there is no mechanism for keeping track of that. Instead, you just reset (or, in this case, "Rewind") the entire Scratchpad.

    This is brilliant if you're allocating a bunch of stuff, then disposing of all of it. You just do your work and rewind the scratchpad to reset everything.

    On the other hand, this means that you can't recover any allocated space without destroying everything you have allocated. Of course, you could have multiple scratchpads, rewinding them at different points in time.

    Before you get too excited about using this, though, have a look at the documentation for Allocator.Temp:

    "Each frame, the main thread creates a Temp allocator which is deallocated in its entirety at the end of the frame. Each job also creates one Temp allocator per thread, and these are deallocated in their entireties at the end of the job. Because a Temp allocator gets discarded as a whole, you actually don't need to manually deallocate your Temp allocations (in fact, doing so is a no-op)."

    ...so, you're already using a scratchpad allocator :)
     
    Last edited: Oct 24, 2022
    Egad_McDad, bb8_1, Enzi and 2 others like this.