phobos-rs icon indicating copy to clipboard operation
phobos-rs copied to clipboard

Optimize resizing scratch allocator

Open NotAPenguin0 opened this issue 2 years ago • 2 comments

  • When allocating a new block, make it twice as large as the current largest block
  • When resetting the frame, use the largest block as the new buffer

NotAPenguin0 avatar Aug 14 '23 21:08 NotAPenguin0

Currently the scratch allocator "merges" all blocks when it gets reset and create a mega-block with a size equal to the sum of all current blocks like this https://github.com/NotAPenguin0/phobos-rs/blob/2a1e539611bb3ede5c2d7978300353630c7c553b/src/allocator/scratch_allocator.rs#L223

Would we allow users to switch between this and the proposed method or should we just implement the proposed method as new default behavior?

Imo I think we should since it would simplify the reset function (since we wouldn't need to allocate a new buffer, just re-use the largest existing one) but it could cause issues when something like this happens (for example):

  • Allocate 1024 (creates block with size of 1048 or chunk_size)
  • Allocate 1000 (creates block with size of 1048 (for alignment maybe?) or chunk_size)
  • Reset (uses block of 1024 as new buffer)
  • Does 1. and 2. again, which would cause the second allocation to actually need to actually allocate a new buffer for it (unless buffers do not get deallocated, in which case idk why even bother)

jedjoud10 avatar Oct 10 '23 02:10 jedjoud10

In your example, the second block allocated would be 2096 bytes large and that would be the one used next frame (allocate the new block with 2x the size of the previous largest block)

NotAPenguin0 avatar Oct 10 '23 07:10 NotAPenguin0