TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Fast, simple, hard real time allocator for Rust

207 pointsby practicalrsabout 1 year ago

8 comments

exDM69about 1 year ago
I wrote almost the exact same thing after seeing Sebastian Aaltonen&#x27;s original offset allocator repo which inspired me to write my own clone in Rust.<p>It&#x27;s possible to further improve the fragmentation characteristics if the maximum size of the memory to be allocated is known ahead of time. The original used a 5.3 &quot;floating point&quot; scheme for the free bins, but given the maximum size you can opt for 4.4 or 6.2 or whatever can cover the whole region, giving more dense (or sparse) allocation granularity as needed.<p>This same idea can also be extended to allocating texture atlas regions when the regions have power of two size. 256 bins can cover all possible texture sizes that GPUs can handle, so you can get hard O(1) texture packing.<p>I&#x27;m also curious about making something like this work as a general purpose allocator (as alluded to by the README). For that it would be necessary to make a reverse lookup from pointer address to allocation handle, which would require something like a red-black tree covering the address space, which would no longer be 0(1). If anyone has ideas on this front, I would be happy to hear them.<p>This algorithm is so clever and kind of a game changer. Allocating and freeing are fast real time operations (only dozens of CPU cycles and a few memory writes). It kind of makes &quot;bump allocators&quot; obsolete because this allocator is almost as fast but supports trimming and freeing allocations as well. This algorithm requires more memory but the amount is quite low.
评论 #40235633 未加载
评论 #40236099 未加载
评论 #40239067 未加载
pcwaltonabout 1 year ago
Author here. Didn&#x27;t expect this to make HN :)<p>I don&#x27;t deserve any credit for this; @SebAaltonen did all the work to write the C++ version. I just did a line-by-line translation.
widdershinsabout 1 year ago
What&#x27;s the difference between this and a slab allocator? Is it just that the bin size distribution wastes less memory (assuming the slab allocator used pow2 bin sizes)?
评论 #40239578 未加载
Ono-Sendaiabout 1 year ago
I have a best-fit allocator for this problem (managing GPU resources). It has higher CPU overhead due to using a std::map, but should waste less space due to not using fixed bucket sizes.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;glaretechnologies&#x2F;glare-core&#x2F;blob&#x2F;master&#x2F;utils&#x2F;BestFitAllocator.cpp">https:&#x2F;&#x2F;github.com&#x2F;glaretechnologies&#x2F;glare-core&#x2F;blob&#x2F;master&#x2F;...</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;glaretechnologies&#x2F;glare-core&#x2F;blob&#x2F;master&#x2F;utils&#x2F;BestFitAllocator.h">https:&#x2F;&#x2F;github.com&#x2F;glaretechnologies&#x2F;glare-core&#x2F;blob&#x2F;master&#x2F;...</a>
lionkorabout 1 year ago
&gt; Please note that offset-allocator isn&#x27;t a Rust allocator conforming to the GlobalAlloc trait<p>Aw. Why not?<p>&gt; find the next available bin using 2x LZCNT instructions to make all operations O(1)<p>Isn&#x27;t that sort of implementation defined? The LZCNT operation itself is a loop over all bits -- the fact that the number of bits is constant doesn&#x27;t make this O(1), it just makes it O(n) where n := 64. But if it was 16 or 32 bit, it may be faster, which means its not O(1), or rather, big o notation really breaks down here.
评论 #40234390 未加载
评论 #40234614 未加载
评论 #40234359 未加载
评论 #40236836 未加载
评论 #40236018 未加载
评论 #40236979 未加载
hinkleyabout 1 year ago
Why is this hard realtime in the face of arbitrary allocations and deallocations? These dots aren&#x27;t connected anywhere within two links distance from the given URL.
joshsynabout 1 year ago
How is this exactly going to be used in bevy?
评论 #40235829 未加载
评论 #40239151 未加载
mgrithmabout 1 year ago
whats a offset allocator?
评论 #40239287 未加载