Skip to content

Commit 2ac2667

Browse files
authored
simplify and generalize copyto! (#49)
Instead of reimplementing the `copyto!` between `Memory` values, just forward to the `copyto!` on our backing storage, `fsa.mem`. This also means that `copyto!` between `FixedSizeArray`s and types that might know about `Memory` (but not necessarily about `FixedSizeArray`) could now be more efficient (when the other type has the right specializations). Rename `memory_of` to `underlying_storage`.
1 parent 11ec2b2 commit 2ac2667

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

src/FixedSizeArrays.jl

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ end
9696

9797
parent_type(::Type{<:FixedSizeArray{T}}) where {T} = Memory{T}
9898

99-
memory_of(m::Memory) = m
100-
memory_of(f::FixedSizeArray) = f.mem
101-
102-
first_linear_index(a) = first(eachindex(IndexLinear(), a))
99+
underlying_storage(m) = m
100+
underlying_storage(f::FixedSizeArray) = f.mem
103101

104102
axes_are_one_based(axes) = all(isone first, axes)
105103

@@ -126,38 +124,30 @@ FixedSizeArray(a::AbstractArray{T,N}) where {T,N} = FixedSizeArray{T,N}
126124
Base.convert(::Type{T}, a::T) where {T<:FixedSizeArray} = a
127125
Base.convert(::Type{T}, a::AbstractArray) where {T<:FixedSizeArray} = T(a)::T
128126

129-
# `copyto!` between `FixedSizeArray` and `Memory`
127+
# `copyto!`
130128

131129
Base.@propagate_inbounds function copyto5!(dst, doff, src, soff, n)
132-
if !iszero(n)
133-
if n < false
134-
throw(ArgumentError("the number of elements to copy must be nonnegative"))
135-
end
136-
@boundscheck checkbounds(dst, doff:doff+n-1)
137-
@boundscheck checkbounds(src, soff:soff+n-1)
138-
@inbounds let d, s
139-
d = GenericMemoryRef(memory_of(dst), doff)
140-
s = GenericMemoryRef(memory_of(src), soff)
141-
unsafe_copyto!(d, s, n)
142-
end
143-
end
130+
copyto!(underlying_storage(dst), doff, underlying_storage(src), soff, n)
144131
dst
145132
end
146133

147-
Base.@propagate_inbounds function copyto2!(dst::T, src) where {T}
148-
fd = first_linear_index(dst)
149-
fs = first_linear_index(src)
150-
len = length(src)
151-
copyto5!(dst, fd, src, fs, len)::T
134+
Base.@propagate_inbounds function copyto2!(dst, src)
135+
copyto!(underlying_storage(dst), underlying_storage(src))
136+
dst
152137
end
153138

154139
Base.@propagate_inbounds Base.copyto!(dst::FixedSizeArray, doff::Integer, src::FixedSizeArray, soff::Integer, n::Integer) = copyto5!(dst, doff, src, soff, n)
155-
Base.@propagate_inbounds Base.copyto!(dst::FixedSizeArray, doff::Integer, src::Memory , soff::Integer, n::Integer) = copyto5!(dst, doff, src, soff, n)
156-
Base.@propagate_inbounds Base.copyto!(dst::Memory , doff::Integer, src::FixedSizeArray, soff::Integer, n::Integer) = copyto5!(dst, doff, src, soff, n)
157-
158140
Base.@propagate_inbounds Base.copyto!(dst::FixedSizeArray, src::FixedSizeArray) = copyto2!(dst, src)
159-
Base.@propagate_inbounds Base.copyto!(dst::FixedSizeArray, src::Memory ) = copyto2!(dst, src)
160-
Base.@propagate_inbounds Base.copyto!(dst::Memory , src::FixedSizeArray) = copyto2!(dst, src)
141+
142+
for A (Array, GenericMemory) # Add more? Too bad we have to hardcode to avoid ambiguity.
143+
@eval begin
144+
Base.@propagate_inbounds Base.copyto!(dst::FixedSizeArray, doff::Integer, src::$A, soff::Integer, n::Integer) = copyto5!(dst, doff, src, soff, n)
145+
Base.@propagate_inbounds Base.copyto!(dst::$A, doff::Integer, src::FixedSizeArray, soff::Integer, n::Integer) = copyto5!(dst, doff, src, soff, n)
146+
147+
Base.@propagate_inbounds Base.copyto!(dst::FixedSizeArray, src::$A ) = copyto2!(dst, src)
148+
Base.@propagate_inbounds Base.copyto!(dst::$A, src::FixedSizeArray) = copyto2!(dst, src)
149+
end
150+
end
161151

162152
# unsafe: the native address of the array's storage
163153

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ end
268268

269269
@testset "`copyto!`" begin
270270
for (D, S) (
271+
(Vector, FixedSizeVector),
271272
(Memory, FixedSizeVector),
273+
(FixedSizeVector, Vector),
272274
(FixedSizeVector, Memory),
273275
(FixedSizeVector, FixedSizeVector),
274276
)

0 commit comments

Comments
 (0)