# Quirks when benchmarking allocations of StaticArrays in anonymous functions

**URL:** <https://discourse.julialang.org/t/quirks-when-benchmarking-allocations-of-staticarrays-in-anonymous-functions/127695>\
**Category:** General Usage\
**Created:** [April 4, 2025, 9:42am UTC](https://discourse.julialang.org/t/quirks-when-benchmarking-allocations-of-staticarrays-in-anonymous-functions/127695 "2025-04-04T09:42:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mxhbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mxhbl/32/211886_2.png) [@mxhbl](https://discourse.julialang.org/u/mxhbl)\
**Post date:** [April 4, 2025, 9:42am UTC](https://discourse.julialang.org/t/quirks-when-benchmarking-allocations-of-staticarrays-in-anonymous-functions/127695/1 "2025-04-04T09:42:17Z")

</div>

While benchmarking allocations I stumbled upon some strange behavior that I cannot explain. I reduced it to the following example.

```julia
using StaticArrays, BenchmarkTools
vec = [1, 2, 3]
svec = SA[1, 2, 3]

fnamed(x) = 0
fanon = x -> 0

@ballocations fnamed($vec)
# Output: 0
@ballocations fanon($vec)
# Output: 0

@ballocations fnamed($svec)
# Output: 0
@ballocations fanon($svec)
# Output: 1

```

I don’t understand why benchmarking shows that the last call allocates. Is there some reason this would be expected or could this be a bug in BenchmarkTools? The output of `@code_lowered` does not show any allocations

```julia
@code_lowered fanon(svec)
# Output:
# CodeInfo(
# 1 ─ return 0
# )

```

---

<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:** [April 4, 2025, 9:47am UTC](https://discourse.julialang.org/t/quirks-when-benchmarking-allocations-of-staticarrays-in-anonymous-functions/127695/2 "2025-04-04T09:47:44Z")

</div>

`fanon` is a (non-`const`) global variable:

```julia
julia> using StaticArrays, BenchmarkTools

julia> vec = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> svec = SA[1, 2, 3]
3-element SVector{3, Int64} with indices SOneTo(3):
 1
 2
 3

julia> const c_fanon = x-> 0
#5 (generic function with 1 method)

julia> @ballocations cfanon($vec)
0

julia> @ballocations cfanon($svec)
0

```

---

<div class="post-metadata">

**Author:** ![mxhbl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mxhbl/32/211886_2.png) [@mxhbl](https://discourse.julialang.org/u/mxhbl)\
**Post date:** [April 4, 2025, 9:48am UTC](https://discourse.julialang.org/t/quirks-when-benchmarking-allocations-of-staticarrays-in-anonymous-functions/127695/3 "2025-04-04T09:48:50Z")

</div>

Thanks, that explains it.
