brenhinkeller / StaticTools.jl

Enabling StaticCompiler.jl-based compilation of (some) Julia code to standalone native binaries by avoiding GC allocations and llvmcall-ing all the things!
MIT License
168 stars 12 forks source link

Add `MallocSlabBuffer` and Bumper.jl compat #54

Closed MasonProtter closed 10 months ago

MasonProtter commented 10 months ago

This might cause some testing problems on v1.9+ without https://github.com/MasonProtter/Bumper.jl/pull/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 Vectors, 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:

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 PtrArrays from StrideArraysCore.jl, whereas in StaticTools.jl stuff is built around MallocArray, which is structurally the same, but has a different name and some different methods like free. There's a couple ways forward we could take:

julia> @no_escape buf begin typeof(@alloc(Int, 10)) end PtrArray{Int64, 1, (1,), Tuple{Int64}, Tuple{Nothing}, Tuple{StaticInt{1}}} (alias for StrideArraysCore.AbstractPtrArray{Int64, 1, (1,), Tuple{Int64}, Tuple{Nothing}, Tuple{Static.StaticInt{1}}, Int64})

MasonProtter commented 10 months ago

MacOS failures seem to be the same as on https://github.com/brenhinkeller/StaticTools.jl/pull/55

brenhinkeller commented 10 months ago

Cool! I'll go ahead and merge!