From c5166f7b5302fc2863e366e490f847b5d9cd1946 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 29 May 2023 20:50:44 -0400 Subject: [PATCH 1/4] Fix _typed_load fast path for Julia 1.10 (currently nightly) --- src/typeconversions.jl | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/typeconversions.jl b/src/typeconversions.jl index fabb9fd68..c740ea823 100644 --- a/src/typeconversions.jl +++ b/src/typeconversions.jl @@ -351,18 +351,36 @@ 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" + 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) From 1e92b19999b62ce44f5dec7ec3a1c2603b3236aa Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 29 May 2023 20:58:43 -0400 Subject: [PATCH 2/4] Fix formatting --- src/typeconversions.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/typeconversions.jl b/src/typeconversions.jl index c740ea823..ea01dbe75 100644 --- a/src/typeconversions.jl +++ b/src/typeconversions.jl @@ -380,7 +380,6 @@ else # 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) From da2c306351ea7e22c7471261ce0514f7b3fec7ba Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 29 May 2023 21:05:37 -0400 Subject: [PATCH 3/4] Narrow VERSION threshold --- src/typeconversions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typeconversions.jl b/src/typeconversions.jl index ea01dbe75..42544d54d 100644 --- a/src/typeconversions.jl +++ b/src/typeconversions.jl @@ -351,7 +351,7 @@ 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) -@static if VERSION < v"1.10" +@static if VERSION ≤ v"1.10.0-DEV.1394" # Maybe a few dev versions earlier function _typed_load( ::Type{T}, buf::V ) where {T,V<:Union{Vector{UInt8},Base.FastContiguousSubArray{UInt8,1}}} From ffa8a1d47057380db2b1b55d3112c9b988b99047 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 29 May 2023 21:43:23 -0400 Subject: [PATCH 4/4] Try earlier DEV version --- .JuliaFormatter.toml | 1 + src/typeconversions.jl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index 91f762908..e387c17d7 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -8,3 +8,4 @@ align_struct_field = true align_conditional = true align_assignment = true align_pair_arrow = true +ignore = [".git"] diff --git a/src/typeconversions.jl b/src/typeconversions.jl index 42544d54d..1e82c6591 100644 --- a/src/typeconversions.jl +++ b/src/typeconversions.jl @@ -351,7 +351,7 @@ 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) -@static if VERSION ≤ v"1.10.0-DEV.1394" # Maybe a few dev versions earlier +@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}}}