Skip to content

Implementation of a Incrementally Defragmenting Auto-Sizing Multi-Arena Allocator#3634

Draft
douira wants to merge 10 commits into
CaffeineMC:devfrom
douira:arena-allocator
Draft

Implementation of a Incrementally Defragmenting Auto-Sizing Multi-Arena Allocator#3634
douira wants to merge 10 commits into
CaffeineMC:devfrom
douira:arena-allocator

Conversation

@douira
Copy link
Copy Markdown
Contributor

@douira douira commented May 7, 2026

Refactors the buffer arena to consist of multiple large buffers that are shared by multiple regions with logic to incrementally defragment, allocate, and deallocate them and move regions between them as buffers fill up.

Remaining todos:

  • Limit maximum buffer allocation size more aggressively, even with weird resource packs it's problematic to create too large buffers
  • Add ability to reduce size of buffer when there's just one big buffer left over
  • Scale amount of allowed defragmentation work with size of buffer and number of segments as it doesn't keep up otherwise

Closes #2683
Closes #2233

@douira douira added this to the Upcoming Major milestone May 7, 2026
@douira douira force-pushed the arena-allocator branch from 4f9deb3 to 40b7d89 Compare May 14, 2026 16:29
douira added 10 commits May 14, 2026 19:56
always check segment assertions for now, potentially improve arena sizing

fix free list corruption bug

work more on compaction, disable for now as it doesn't work well

bug fixes, correct owner change logic, improved deallocation heuristics, improved arena sizing

compact smallest arena into free space if possible, new arena sizing heuristics

prevent oscillation when emptying arenas by not allowing new allocation during emptying-related eviction

properly handle migrating zero-usage regions, and fix bug by not deleting the only existing arena if there's only one

deallocate arenas when there's enough margin of usable free space in other arenas to evict its contents

update only the segments that changed when defragmenting

use 32mib index arena by default

improve heuristics to reduce defrag effort when not needed

refactor defrag code for moving free spaces towards each other

limit copy and byte budget for defragmenting

renames for clarity

improve fragmentation measure, show total defrag copies and volume, modulate brightness of visualizer segments

defragment towards second biggest segment to reduce number of copies and copied bytes

Incremental defragmentation

buffer visualization

detect best shared arena to place region correctly

notes on defragmentation

split free tracking into a separate class

use multiple shared buffers

rename to shared arena

TreeMap has been profiled to be much faster

fix bugs, improve names

more assertion

use tree list to track free segments

fix crashes when f3 is open and arenas are gone

young gen buffer handling

migrate gl buffer arena to be generated by a central instance to make buffer sharing more elegant and not leak shared buffers
…ps percentiles and the fps display on the debug screen
@douira douira force-pushed the arena-allocator branch from f0e2582 to 2c7ed95 Compare May 14, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use TLSF allocation strategy for terrain geometry Geometry buffers are never compacted, leading to excessive VRAM usage

1 participant