Skip to content

Commit

Permalink
Fix _typed_load fast path for Julia 1.10 (currently nightly) (#1075)
Browse files Browse the repository at this point in the history
* Fix _typed_load fast path for Julia 1.10 (currently nightly) by thresholding on `1.10.0-DEV.1390`
* Use `Libc.memcpy` after that threshold
  • Loading branch information
mkitti authored May 30, 2023
1 parent 5ec8e06 commit 4957fb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ align_struct_field = true
align_conditional = true
align_assignment = true
align_pair_arrow = true
ignore = [".git"]
41 changes: 29 additions & 12 deletions src/typeconversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,35 @@ function _typed_load(::Type{T}, buf::AbstractVector{UInt8}) where {T}
return @inbounds reinterpret(T, buf)[1]
end
# fast-path for common concrete types with simple layout (which should be nearly all cases)
function _typed_load(
::Type{T}, buf::V
) where {T,V<:Union{Vector{UInt8},Base.FastContiguousSubArray{UInt8,1}}}
dest = Ref{T}()
GC.@preserve dest buf Base._memcpy!(
unsafe_convert(Ptr{Cvoid}, dest), pointer(buf), sizeof(T)
)
return dest[]
# TODO: The above can maybe be replaced with
# return GC.@preserve buf unsafe_load(convert(Ptr{t}, pointer(buf)))
# dependent on data elements being properly aligned for all datatypes, on all
# platforms.
@static if VERSION v"1.10.0-DEV.1390" # Maybe a few dev versions earlier
function _typed_load(
::Type{T}, buf::V
) where {T,V<:Union{Vector{UInt8},Base.FastContiguousSubArray{UInt8,1}}}
dest = Ref{T}()
GC.@preserve dest buf Base._memcpy!(
unsafe_convert(Ptr{Cvoid}, dest), pointer(buf), sizeof(T)
)
return dest[]
# TODO: The above can maybe be replaced with
# return GC.@preserve buf unsafe_load(convert(Ptr{t}, pointer(buf)))
# dependent on data elements being properly aligned for all datatypes, on all
# platforms.
end
else
# TODO reimplement fast path _typed_load for Julia 1.10, consider refactor
function _typed_load(
::Type{T}, buf::V
) where {T,V<:Union{Vector{UInt8},Base.FastContiguousSubArray{UInt8,1}}}
dest = Ref{T}()
GC.@preserve dest buf Libc.memcpy(
unsafe_convert(Ptr{Cvoid}, dest), pointer(buf), sizeof(T)
)
return dest[]
# TODO: The above can maybe be replaced with
# return GC.@preserve buf unsafe_load(convert(Ptr{t}, pointer(buf)))
# dependent on data elements being properly aligned for all datatypes, on all
# platforms.
end
end

_normalize_types(::Type{T}, buf::AbstractVector{UInt8}) where {T} = _typed_load(T, buf)
Expand Down

0 comments on commit 4957fb8

Please sign in to comment.