Skip to content

iterate on SubArray uses slow default fallback #43295

Closed
@jakobnissen

Description

@jakobnissen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]help wantedIndicates that a maintainer wants help on an issue or pull requestperformanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions