ArrayLayouts.jl icon indicating copy to clipboard operation
ArrayLayouts.jl copied to clipboard

overwrite `Base.maybeview`?

Open putianyi889 opened this issue 1 year ago • 1 comments

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

putianyi889 avatar Jul 20 '23 21:07 putianyi889

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

dlfivefifty avatar Jul 21 '23 09:07 dlfivefifty