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
167 stars 11 forks source link

`AbstractArray` -> `MallocArray` conversion? #44

Open jonniediegelman opened 1 year ago

jonniediegelman commented 1 year ago

I find MallocArray useful for working with ccall because it allows me to more easily pass in ref-wrapped structs that contain arrays. But it's a little clunky to manually have to convert from a plain Array to a MallocArray. Would there be anything wrong with having a default conversion like this?

function Base.convert(::Type{MallocArray{T, N}}, x::AbstractArray{T, N}) where {T, N}
    return MallocArray(pointer(x), length(x), size(x))
end
brenhinkeller commented 1 year ago

Ah, so the trouble there is that MallocArray's are expecting to be given a pointer to memory that has been manually allocated with malloc. This could technically still work if for Arrays (or more generally DenseArrays -- but not all AbstractArrays) if you could guarantee that the memory backing the Array would not be garbage collected by Julia in the meanwhile, and that you wouldn't ever attempt to free the resulting MallocArray.

If you can guarantee for your use case that Julia won't GC the underlying memory and wanted to implement a method like this, I'd accept a PR -- but it should probably convert to StaticTools.ArrayView (which also just has fields of pointer, length, and size) rather than StaticTools.MallocArray to ensure the resulting object can't be accidentally freed (because that would be bad).

In the meanwhile, we do also have this method (which works on any AbstractArray and just copies the data to a new MallocArray):

julia> a = collect(1:5)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> MallocArray(a)
5-element MallocVector{Int64}:
 1
 2
 3
 4
 5
jonniediegelman commented 1 year ago

Okay, thanks! I didn't know about ArrayView, it seems like that's what I actually want here.

brenhinkeller commented 1 year ago

Cool! PRs welcome if you want to change anything here!