# Where does this allocation pattern come from?

**URL:** <https://discourse.julialang.org/t/where-does-this-allocation-pattern-come-from/105606>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 31, 2023, 5:28am UTC](https://discourse.julialang.org/t/where-does-this-allocation-pattern-come-from/105606 "2023-10-31T05:28:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 31, 2023, 5:28am UTC](https://discourse.julialang.org/t/where-does-this-allocation-pattern-come-from/105606/1 "2023-10-31T05:28:55Z")

</div>

When I execute the following code, I get some interesting pattern:

```julia
x = collect(1:1000)
x[1]
@showtime x[511]
@showtime x[511]
@showtime x[511]
@showtime x[512]
@showtime x[512]
@showtime x[512]
@showtime x[511]
@showtime x[512]

```

```julia
x[511]: 0.000005 seconds
x[511]: 0.000001 seconds
x[511]: 0.000001 seconds
x[512]: 0.000001 seconds (1 allocation: 16 bytes)
x[512]: 0.000001 seconds (1 allocation: 16 bytes)
x[512]: 0.000001 seconds (1 allocation: 16 bytes)
x[511]: 0.000001 seconds
x[512]: 0.000001 seconds (1 allocation: 16 bytes)

```

Namely `x[511]` does not allocate, while `x[512]` does.  
Why is this the case, what is going on under the hood?

PS: I am aware of the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-tips) and everything becomes fast if put into a function.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 31, 2023, 8:20am UTC](https://discourse.julialang.org/t/where-does-this-allocation-pattern-come-from/105606/2 "2023-10-31T08:20:42Z")

</div>

I looked into and it calls sprintf so I thought it causing it, and seems confirmed but even stranger (how are 5+ allocations swallowed if that’s done implicitly?):

```julia
julia> @time Base.sprint(Base.show_unquoted, x[512])
  0.000024 seconds (7 allocations: 280 bytes)
"512"

julia> @time Base.sprint(Base.show_unquoted, x[511])
  0.000022 seconds (5 allocations: 248 bytes)
"511"

```

It does implicitly:

```julia
julia> @time s = IOBuffer(sizehint=0)
  0.000004 seconds (2 allocations: 128 bytes)

```

but I’m guessing in some cases the compiler is clever and can optimize it out?

and then for me:

```julia
julia> @time Base.show_unquoted(s, x[512])
  0.000021 seconds (3 allocations: 104 bytes)

note, if repeated, not consistent, but seemed to be in your case:

julia> @time Base.show_unquoted(s, x[512])
  0.000017 seconds (4 allocations: 168 bytes)

julia> @time Base.show_unquoted(s, x[512])
  0.000016 seconds (3 allocations: 104 bytes)

```

In full:

```julia
function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
    s = IOBuffer(sizehint=sizehint)
    if context isa Tuple
        f(IOContext(s, context...), args...)
    elseif context !== nothing
        f(IOContext(s, context), args...)
    else
        f(s, args...)
    end
    String(_unsafe_take!(s))
end

```

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [October 31, 2023, 8:43am UTC](https://discourse.julialang.org/t/where-does-this-allocation-pattern-come-from/105606/3 "2023-10-31T08:43:55Z")

</div>

According to `@macroexpand` the printing happens after performance stats are taken, so we should not see any print related allocs? Here is a benchmark where it is more clear, that printing should not affect the benchmark:

```julia
x = collect(1:1000)
x[1]
a_511_1 = @allocated x[511]
a_511_2 = @allocated x[511]
a_511_3 = @allocated x[511]
a_512_1 = @allocated x[512]
a_512_2 = @allocated x[512]
a_512_3 = @allocated x[512]
a_511_4 = @allocated x[511]
a_512_4 = @allocated x[512]
@show a_511_1
@show a_511_2
@show a_511_3
@show a_512_1
@show a_512_2
@show a_512_3
@show a_511_4
@show a_512_4

# a_511_1 = 0
# a_511_2 = 0
# a_511_3 = 0
# a_512_1 = 16
# a_512_2 = 16
# a_512_3 = 16
# a_511_4 = 0
# a_512_4 = 16

```

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [October 31, 2023, 9:07am UTC](https://discourse.julialang.org/t/where-does-this-allocation-pattern-come-from/105606/4 "2023-10-31T09:07:45Z")

</div>

I do not know what the source of allocations is but defining the global variable `x` as `const` should remove the allocations.
