Closed
Description
It turns out the default fallback is actually rather slow for SubArray
:
julia> using BenchmarkTools
julia> f(v) = sum((i for i in v), init=0);
julia> v = rand(Int, 2^12);
julia> @btime f(v);
248.161 ns (1 allocation: 16 bytes)
julia> @btime f($(view(v, 1:2^12)));
2.109 μs (0 allocations: 0 bytes)
julia> g(v) = sum((@inbounds v[i] for i in eachindex(v)), init=0);
julia> @btime g($(view(v, 1:2^12)));
228.657 ns (0 allocations: 0 bytes)
I think this is rather bad as SubArray
is a fairly fundamental type, and people tend to use it specifically when they are looking for performance.
It appears to be the boundschecking that is particularly expensive for SubArray
. #42030 is related to this, but even if it is decided that default implementations should not use @inbounds
, this should still be solved by creating a specialized method for SubArray
.
This occurs on Julia 1.6.3, 1.7.0 and master as of 2021-12-02.