Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elminate all pointers! #41

Open
ExpandingMan opened this issue Mar 8, 2019 · 0 comments
Open

elminate all pointers! #41

ExpandingMan opened this issue Mar 8, 2019 · 0 comments

Comments

@ExpandingMan
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant