Allocation in broadcasting of round for staticarrays

Why does the round.(Int, x::SVector) allocate?

using BenchmarkTools, StaticArrays
x = SVector{2}([0., 1.])

@btime round.(Int, $x) # 21.180 ns (1 allocation: 16 bytes)
@btime round.(Int, $(x.data)) # 6.451 ns (0 allocations: 0 bytes)
julia> versioninfo()
Julia Version 1.7.0
Commit 3bf9d17731 (2021-11-30 12:12 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2630 v3 @ 2.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, haswell)
Environment:
  JULIA = /home/jmlim/appl/julia-1.7.0/bin/julia

(@v1.7) pkg> status StaticArrays
      Status `~/.julia/environments/v1.7/Project.toml`
  [90137ffa] StaticArrays v1.2.13

and also with StaticArrays v1.3.4

I’m seeing the same thing. Odd.

BTW, this

creates a regular Vector which is then converted to an SVector, so that’s an additional allocation. Instead, just create the SVector directly, like one of these:

SVector{2}(0.0, 1.0)
SVector(0.0, 1.0)  # the {2} isn't necessary
SA[0.0, 1.0]

Doesn’t help with the other allocation, though.

3 Likes

I opened an issue: https://github.com/JuliaArrays/StaticArrays.jl/issues/992

3 Likes

Here this is a regression relative to 1.6.3, where that didn’t allocate. I commented on the issue.