# Boxing of integer when looping vector over 512 index

**URL:** <https://discourse.julialang.org/t/boxing-of-integer-when-looping-vector-over-512-index/64259>\
**Category:** Performance\
**Created:** [July 8, 2021, 8:37am UTC](https://discourse.julialang.org/t/boxing-of-integer-when-looping-vector-over-512-index/64259 "2021-07-08T08:37:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpinol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpinol/32/26550_2.png) [@dpinol](https://discourse.julialang.org/u/dpinol)\
**Post date:** [July 8, 2021, 8:37am UTC](https://discourse.julialang.org/t/boxing-of-integer-when-looping-vector-over-512-index/64259/1 "2021-07-08T08:37:07Z")

</div>

[Weird allocation that depends on array contents · Issue #36751 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/36751#issuecomment-661925102) & [memory allocation increases unexpectedly in sparse getindex at a threshold · Issue #18051 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/18051#issuecomment-240136404) mention llvm caching of integer boxing between -512 & 512.  
However, I don’t understand why we need boxing. Is there any way to avoid it? It looks like they managed it here [memory allocation increases unexpectedly in sparse getindex at a threshold · Issue #18051 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/18051#issuecomment-240176079)

```julia
v=Vector{Int64}(undef, 1000)

julia> @time for i in 510:511 v[i]=3 end
  0.000002 seconds

julia> @time for i in 511:512 v[i]=3 end
  0.000002 seconds (1 allocation: 16 bytes)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 8, 2021, 8:42am UTC](https://discourse.julialang.org/t/boxing-of-integer-when-looping-vector-over-512-index/64259/2 "2021-07-08T08:42:58Z")

</div>

Because you are in global scope. Put it in a function and it will not allocate.

---

<div class="post-metadata">

**Author:** ![dpinol](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpinol/32/26550_2.png) [@dpinol](https://discourse.julialang.org/u/dpinol)\
**Post date:** [July 8, 2021, 10:40am UTC](https://discourse.julialang.org/t/boxing-of-integer-when-looping-vector-over-512-index/64259/3 "2021-07-08T10:40:06Z")

</div>

🤦‍♂️ you’re right, noob question. thanks!
