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

Fix _typed_load fast path for Julia 1.10 (currently nightly) #1075

Merged
Merged
Show file tree
Hide file tree
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
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