Add MallocSlabBuffer
and Bumper.jl compat
#54
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This might cause some testing problems on v1.9+ without MasonProtter/Bumper.jl#31 being merged, because of a circular dependency with StaticCompiler.jl, but other than that I think this implementation should be more or less working.
It's basically a re-implementation of https://github.com/MasonProtter/Bumper.jl/blob/515a4dd405de71da6621dd7b72841c6c794f2c2c/src/SlabBuffer.jl except it uses no mutable types and no
Vector
s, implementing everything via pointers directly, including the resizing of the slab queues.MallocSlabBuffer's implementation is kinda scary looking, but it's actually not that complicated. Here's a rundown of how it works:
slab_size
(default 1 megabyte).current
field is the currently active pointer that a newly@alloc
'd object will aquire, if the object fits betweencurrent
andslab_end
.current
andslab_end
, but is smaller thanslab_size
, we'llmalloc
a new slab, and add it toslabs
(reallocating theslabs
pointer if there's not enough room, as determined bymax_slabs_length
) and then set that thing as thecurrent
pointer, and provide that to the object.slab_size
, then wemalloc
a pointer of the requested size, and add it to thecustom_slabs
pointer (also reallocating that pointer if necessary), leavingcurrent
andslab_end
unchanged.@no_escape
block ends, we resetcurrent
, andslab_end
to their old values, and ifslabs
orcustom_slabs
have grown, wefree
all the pointers that weren't present before, and reset their respectivelength
s (but notmax_size
s).For this PR, I've opted to not rock the boat too much regarding the existing APIs and infrastructure, but I do think that this should be the default way dynamic memory is dealt with in StaticTools because it's very efficient, more user friendly than direct
malloc
/free
, and quite flexible.In the future then (or in this PR if we want) we'll need to figure out a good path to dealing with the zoo of different types here, and migrating the test suite, and modifying the documentation to nudge people more towards
MallocSlabBuffer
.Regarding types, one potential design question is that Bumper defaults to returning
PtrArray
s from StrideArraysCore.jl, whereas in StaticTools.jl stuff is built aroundMallocArray
, which is structurally the same, but has a different name and some different methods likefree
. There's a couple ways forward we could take:@alloc
produce aMallocArray
instead of aPtrArray
when used on aMallocSlabBuffer
. This could work fine, but I'm not a fan of it being namedMallocArray
if it's being handled by theMallocSlabBuffer
instead ofmalloc
directlyMallocArray(...)
withmallocarray(...)::PtrArray
since this change would bring StrideArraysCore.jl into StaticTools.jl anyways, andPtrArray
has some nice advantages like really good support from LoopVectorization.jl etc. It also has some potential annoyances though, like a shitload of type parameters: