Skip to content

Indexing a SubArray without @inbounds prevents its allocation from being elided #29860

@KristofferC

Description

@KristofferC

For example:

julia> f(x) = view(x, :)[3]
f (generic function with 1 method)

julia> f2(x) = @inbounds view(x, :)[3]
f2 (generic function with 1 method)

julia> x = rand(3);

julia> @btime f($x);
  8.618 ns (1 allocation: 48 bytes)

julia> @btime f2($x);
  1.503 ns (0 allocations: 0 bytes)

Presumably, this is because it appears in a call to Base.throw_boundserror. Not surprising, but it means that many SubArrays hang around even though their only non-inlined usage is in an error branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions