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

improve performance #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ tags[:unionall] = d -> UnionAll(d[:var], d[:body])
lower(x::Vector{Any}) = copy(x)
lower(x::Vector{UInt8}) = x

reinterpret_(::Type{T}, x) where T =
T[reinterpret(T, x)...]
function reinterpret_(::Type{T}, x) where T
len = Int(length(x) * (sizeof(eltype(x)) / sizeof(T)))
GC.@preserve x begin
return unsafe_wrap(Array, Ptr{T}(pointer(x)), len)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to set own = true for unsafe_wrap.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, that doesn't work on its own, but if I copy the memory then it works

function reinterpret_(::Type{T}, x::Vector{S}) where {T, S}
  length(x) < 1 && return T[]

  len = Int(length(x) * (sizeof(eltype(x)) / sizeof(T)))

  GC.@preserve x begin
    p = Ptr{UInt8}(pointer(x))
    t = Ptr{UInt8}(Base.Libc.calloc(len, sizeof(T)))
    ccall(:memset, Cvoid, (Ptr{UInt8}, Cint, Csize_t), t, 0, len * sizeof(T))
    ccall(:memcpy, Cvoid, (Ptr{UInt8}, Ptr{UInt8}, Csize_t), t, p, sizeof(x))
    Base.unsafe_wrap(Array, Ptr{T}(t), len; own=true)
  end
end

This brings the time down on my fork to 0.479740 seconds from 0.51184. It would be better to copy the bytes directly from the input stream and only memset the trailing bytes.

I also tried creating a second reference to the original memory and wrapping it, but unsafe_wrap just treats the Ref as a pointer AFAICT.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I add mmap and copy directly from the IOBuffers byte vector, then we get the following:

julia> @timev BSONqs.load("../../../julia/serbench/vgg19.bson"; mmap=true);
  0.250118 seconds (15.08 k allocations: 548.829 MiB, 19.26% gc time)

end
end


function lower(x::Array)
ndims(x) == 1 && !isbitstype(eltype(x)) && return Any[x...]
Expand Down