JuliaArrays / JuliaArrays/StaticArrays.jl

floor, ceil, round called with (::Type{T}, ::SArray) allocates

Open
#992 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
844
Forks
159
Avg merge
3d 21h
Merged PRs (30d)
3

Description

using BenchmarkTools, StaticArrays
x = SVector(0., 1.)
@btime round.(Int, $x) # 11.817 ns (1 allocation: 16 bytes)
@btime floor.(Int, $x) # 11.723 ns (1 allocation: 16 bytes)
@btime ceil.(Int, $x) # 11.754 ns (1 allocation: 16 bytes)

There seems to be no type instability.

julia> @code_warntype round.(Int, x)
MethodInstance for (::var"##dotfunction#439#69")(::Type{Int64}, ::SVector{2, Float64})
  from (::var"##dotfunction#439#69")(x1, x2) in Main
Arguments
  #self#::Core.Const(var"##dotfunction#439#69"())
  x1::Core.Const(Int64)
  x2::SVector{2, Float64}
Body::SVector{2, Int64}
1 ─ %1 = Base.broadcasted(Main.round, x1, x2)::Base.Broadcast.Broadcasted{StaticArrays.StaticArrayStyle{1}, Nothing, typeof(round), Tuple{Base.RefValue{Type{Int64}}, SVector{2, Float64}}}
│   %2 = Base.materialize(%1)::SVector{2, Int64}
└──      return %2
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.3.4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the BenchmarkTools examples for round.(Int, x), floor.(Int, x), and ceil.(Int, x) with the reported StaticArrays setup. Inspect the Base.broadcasted and Base.materialize path using StaticArrayStyle; done means these calls preserve the SVector result without the reported 16-byte allocation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.