ArrayLayouts.jl
ArrayLayouts.jl copied to clipboard
overwrite `Base.maybeview`?
motivation is from #153
Taking a view intends to minimize allocation. However, for some lazy arrays, allocating a concrete subarray could be more efficient. The proposal is as follows
maybeview(A::AbstractArray, args...) = maybeview(MemoryLayout(A), A, args...)
maybeview(::MemoryLayout, A, args...) = view(A, args...)
maybeview(::AbstractLazyLayout, A, args...) = A[args...] # at LazyArrays.jl
From the views.jl:
# maybeview is like getindex, but returns a view for slicing operations
# (while remaining equivalent to getindex for scalar indices and non-array types)
So your proposal would be inconsistent with the definition. But we could call it something else.
Btw I've started not using the same function name with extra arguments (following some good changes from @jishnub) so I would call this:
maybeview(A::AbstractArray, args...) = maybeview_layout(MemoryLayout(A), A, args...)
maybeview_layout(::MemoryLayout, A, args...) = view(A, args...)
maybeview_layout(::AbstractLazyLayout, A, args...) = A[args...] # at LazyArrays.jl