ExpandingMan / Arrow.jl

DEPRECATED in favor of [JuliaData/Arrow.jl](https://github.com/JuliaData/Arrow.jl)
Other
56 stars 9 forks source link

elminate all pointers! #41

Open ExpandingMan opened 5 years ago

ExpandingMan commented 5 years ago

I have a major rewrite of this package coming up after which I plan to fully support the arrow standard so we can use it for IPC and all the other cool things all the kids these days are doing.

In the meantime, I just wanted to record here that I should be able to completely eliminate all pointers from this package and have its memory safetcy guaranteed by the memory safety of Julia. Observe!

using BenchmarkTools

const N = 128

v = zeros(UInt8, N*sizeof(Int))  

function wrap(::Type{T}, v::AbstractVector, i1::Integer, ℓ::Integer) where {T}
    p = convert(Ptr{T}, pointer(v) + i1)
    w = unsafe_wrap(Array, p, ℓ)  
    copyto!(Vector{T}(undef, ℓ), w)
end

function safe(::Type{T}, v::AbstractVector, i1::Integer, ℓ::Integer) where {T}
    w = reinterpret(T, @view v[i1:(i1 + ℓ*sizeof(T) - 1)])
    copyto!(Vector{T}(undef, ℓ), w)    
end
julia> @btime safe(Int, v, 8, 2)
  71.926 ns (3 allocations: 176 bytes)

julia> @btime wrap(Int, v, 8, 2)
  75.115 ns (2 allocations: 176 bytes)

Keno is definitely a badass.

Note that views are now going to be of a really strange type (a reinterpret array of a view). Might have to look into whether Julia needs to add a method for reinterpreting the middle of an array.