Repository navigation
Conversation
FreeListAllocator::allocate scanned every free chunk in the block on each allocation. Keep free chunks in a BTreeSet ordered by (size, chunk id) and walk up from the requested size, taking the first chunk that fits after alignment and granularity padding. Ties between equal-size chunks now go to the lowest chunk id instead of HashSet order.
|
Can you let claude build and report a simulation of various allocation patterns (mixes of big and small allocation such as constant buffers and render targets) and report both fragmentation as well as memory high water marks. I suspect overall this is a good change but the remark about the 4-64KB chunks makes it so I want to investigate that a bit more. |
|
Will do
to clarify, that was outlining the method for measuring performance. it's not an outcome of the change. The old and new code pick the same chunk (except tie breaking between equal size chunks was previously random and is now the lower chunk id). So fragmentation and peak memory use should match, and I'll show that with the simulation |
|
Ran it: fragmentation and peak memory are unchanged across five simulated workloads (constant buffers, staging buffers, render targets up to 132 MiB, streamed textures, 64 KiB granularity), and allocation is 1.6–17× faster. That's expected, since both versions pick the same chunk; only the tie-break between equal-sized chunks changed. The 4–64 KiB range was just how the microbenchmark fragmented its block. Simulator and raw results: https://gist.github.com/sargunv/96776b49e18aa454c426fdd4653a43b6 DetailsWhy the result matches. The old loop skipped chunks smaller than the request and kept the smallest one that passed the alignment and granularity checks. The new loop starts at the requested size and stops at the first chunk that passes, which is the same chunk. Ties between equal-sized chunks used to go by Setup. The simulator calls
Each workload ran 50 times per version in a release build on Apple Silicon. Values are the median, with the range in parentheses when runs differed. "Stranded free" is the share of free bytes that sit outside each block's largest free chunk, sampled every 10 frames.
Mean capacity over mean live memory and the number of blocks created also stay inside the old range. The old code produced a different layout on 43–50 of 50 runs in four workloads, and twice occasionally needed an extra 256 MiB block because of it. The new code gives the same layout every run and always matches the old best case. One worst case is left. If many big-enough chunks fail the alignment or granularity check, the new loop still walks past all of them. That costs at most what the old full scan did on every allocation. AI-assisted — Tool: Claude Code; model: anthropic/claude-opus-5-5 |
FreeListAllocator::allocatechecks every free chunk in a block to find the best fit, so allocation slows down as a long-running app frees buffers. In one Vulkan map-rendering benchmark, about 1,300 free chunks made this scan 41% of our render thread's instructions.This keeps free chunks in a
BTreeSet<(size, chunk_id)>and walks up from the requested size. The first chunk that passes the existing alignment and granularity checks is the best fit.Measured with a release build on Apple Silicon. One block was fragmented into N free chunks of 4–64 KiB; I timed allocate+free pairs and took the best of 5 runs.
USE_BEST_FITis removed because first fit and best fit now pick the same chunk.AI-assisted — Tool: Claude Code; model: anthropic/claude-opus-5-5