What about loading SVectors directly from your vector?
using StaticArrays
struct SView{L}
start::Int
end
function Base.view(x::Vector{T}, v::SView{L}) where {T,L}
reinterpret(SVector{L,T}, view(x, v.start:(v.start + L - 1)))
end
x = collect(1:10)
v = SView{4}(3)
view(x, v)
function svload(x::Vector{T}, v::SView{L}) where {T,L}
ptr_x = Base.unsafe_convert(Ptr{SVector{L,T}}, pointer(x))
unsafe_load(ptr_x + (v.start-1) * sizeof(T))
end
svload(x, v)
For comparison,
julia> view(x, v)
1-element reinterpret(SArray{Tuple{4},Int64,1,4}, view(::Array{Int64,1}, 3:6)):
[3, 4, 5, 6]
julia> svload(x, v)'
1Γ4 LinearAlgebra.Adjoint{Int64,SArray{Tuple{4},Int64,1,4}}:
3 4 5 6
julia> @code_native svload(x, v)
.text
; β @ REPL[12]:2 within `svload'
; ββ @ abstractarray.jl:882 within `pointer'
; βββ @ REPL[12]:2 within `unsafe_convert'
movq (%rsi), %rax
; βββ
; β @ REPL[12]:3 within `svload'
; ββ @ int.jl:52 within `-'
movq (%rdx), %rcx
; ββ
; ββ @ pointer.jl:105 within `unsafe_load' @ pointer.jl:105
vmovups -8(%rax,%rcx,8), %ymm0
; ββ
movq %rdi, %rax
vmovups %ymm0, (%rdi)
vzeroupper
retq
nopw (%rax,%rax)
; β
Youβd probably want to add an @boundscheck
to svload. Right now it isnβt safe.