JuliaArrays / JuliaArrays/StaticArrays.jl
Slow in-place broadcasted assignment (.=) of mutable FieldVectors
Nobody has claimed this yet.
- Dominant language
- Julia
- Stars
- 844
- Forks
- 159
- Avg merge
- 3d 21h
- Merged PRs (30d)
- 3
Description
As noted on Discourse (https://discourse.julialang.org/t/julia-style-arrays-matrix-vs-structs/32424/10?u=stillyslalom), broadcasted assignment seems to be many times slower for mutable FieldVectors than for MVectors.
using StaticArrays
struct Point3D{T} <: FieldVector{3, T}
x::T
y::T
z::T
end
mutable struct MPoint3D{T} <: FieldVector{3, T}
x::T
y::T
z::T
end
function f1!(v)
for i in eachindex(v)
x, y, z = v[i]
v[i] = Point3D(2x, x*y, x*z)
end
end
function f2!(v)
for i in eachindex(v)
x, y, z = v[i]
v[i] .= (2x, x*y, x*z)
end
end
using BenchmarkTools
v1 = [@SVector(rand(3)) for i = 1:1000]
v2 = [@MVector(rand(3)) for i = 1:1000]
v3 = [Point3D(rand(3)) for i = 1:1000]
v4 = [MPoint3D(rand(3)) for i = 1:1000]
@btime f1!($v1) # 853.985 ns (0 allocations: 0 bytes)
@btime f2!($v2) # 1.775 μs (0 allocations: 0 bytes)
@btime f1!($v3) # 852.917 ns (0 allocations: 0 bytes)
@btime f2!($v4) # 60.091 μs (3000 allocations: 46.88 KiB)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the issue's StaticArrays and BenchmarkTools reproducer, comparing f1! and f2! for mutable FieldVectors and MVectors. Trace the broadcasted assignment path used by v[i] .= (...) and measure allocations and runtime. Done means mutable Point3D-style assignment no longer incurs the reported allocation and large performance penalty.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100