Is scalar getindex supposed to be allocating

I am a bit stumped by the behavior I have bumped into. I thought that only slicing allocates and scalar indexing should not. But try this thing:

a = collect(0.0:1.0:10.0)
a[2]

@allocated a[2]

For me, it somehow allocates 16 KB. I get the same result on Julia 1.12.4, 1.11.4 and 1.6.7.

Is it really normal?

It’s an artifact of the REPL

julia> a = collect(0.0:1.0:10.0)
julia> f(a, i) = a[i]
julia> @allocated f(a,2)
0

That’s because a is a (non-constant) global variable there.

julia> const b = collect(0.0:1.0:10.0)
11-element Vector{Float64}:
  0.0
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0

julia> @allocated b[2]
0
3 Likes