Skip to content

Commit d1e595a

Browse files
authored
Avoid allocation when computing norm of MVector (#1131)
* Avoid allocation when computing norm of MVector * fix test on Julia nightly
1 parent 791a901 commit d1e595a

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StaticArrays"
22
uuid = "90137ffa-7385-5640-81b9-e52037218182"
3-
version = "1.5.15"
3+
version = "1.5.16"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/linalg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ _inner_eltype(x::Number) = typeof(x)
226226
end
227227

228228
@inline maxabs_nested(a::Number) = abs(a)
229-
function maxabs_nested(a::AbstractArray)
229+
@inline function maxabs_nested(a::AbstractArray)
230230
prod(size(a)) == 0 && (return _init_zero(a))
231231

232232
m = maxabs_nested(a[1])

test/broadcast.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,7 @@ end
236236
@test @inferred(add_bc!(MMatrix(SA[10 20; 30 40]), (1,2))) ::MMatrix{2,2,Int} == SA[11 21; 32 42]
237237

238238
# Tuples of SA
239-
@test SA[1,2,3] .* (SA[1,0],) === SVector{3,SVector{2,Int}}(((1,0), (2,0), (3,0)))
240-
# Unfortunately this case of nested broadcasting is not inferred
241-
@test_broken @inferred(SA[1,2,3] .* (SA[1,0],))
239+
@test (@inferred SA[1,2,3] .* (SA[1,0],)) === SVector{3,SVector{2,Int}}(((1,0), (2,0), (3,0)))
242240
end
243241

244242
@testset "SDiagonal" begin

test/linalg.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Base.getindex(m::RotMat2, i::Int) = getindex(m.elements, i)
1010
# Rotation matrices must be unitary so `similar_type` has to return an SMatrix.
1111
StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,4}
1212

13+
Base.@kwdef mutable struct KPS4{S, T, P}
14+
v_apparent::T = zeros(S, 3)
15+
end
16+
1317
@testset "Linear algebra" begin
1418

1519
@testset "SArray as a (mathematical) vector space" begin
@@ -313,6 +317,25 @@ StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,
313317
@test norm(SVector{0,Float64}()) isa Float64
314318
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) isa float(Int)
315319
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) == norm([Int[], Int[]])
320+
321+
# no allocation for MArray -- issue #1126
322+
323+
@inline function calc_particle_forces!(s, pos1, pos2)
324+
segment = pos1 - pos2
325+
norm1 = norm(segment)
326+
unit_vector = segment / norm1
327+
328+
v_app_perp = s.v_apparent - s.v_apparent unit_vector * unit_vector
329+
half_drag_force = norm(v_app_perp)
330+
nothing
331+
end
332+
kps4 = KPS4{Float64, MVector{3, Float64}, 6+4+1}()
333+
334+
pos1 = MVector{3, Float64}(1.0, 2.0, 3.0)
335+
pos2 = MVector{3, Float64}(2.0, 3.0, 4.0)
336+
calc_particle_forces!(kps4, pos1, pos2)
337+
calc_particle_forces!(kps4, pos1, pos2)
338+
@test (@allocated calc_particle_forces!(kps4, pos1, pos2)) == 0
316339
end
317340

318341
@testset "trace" begin

0 commit comments

Comments
 (0)