Skip to content

Commit

Permalink
Avoid allocation when computing norm of MVector (#1131)
Browse files Browse the repository at this point in the history
* Avoid allocation when computing norm of MVector

* fix test on Julia nightly
  • Loading branch information
mateuszbaran authored Feb 18, 2023
1 parent 791a901 commit d1e595a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticArrays"
uuid = "90137ffa-7385-5640-81b9-e52037218182"
version = "1.5.15"
version = "1.5.16"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
2 changes: 1 addition & 1 deletion src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ _inner_eltype(x::Number) = typeof(x)
end

@inline maxabs_nested(a::Number) = abs(a)
function maxabs_nested(a::AbstractArray)
@inline function maxabs_nested(a::AbstractArray)
prod(size(a)) == 0 && (return _init_zero(a))

m = maxabs_nested(a[1])
Expand Down
4 changes: 1 addition & 3 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,7 @@ end
@test @inferred(add_bc!(MMatrix(SA[10 20; 30 40]), (1,2))) ::MMatrix{2,2,Int} == SA[11 21; 32 42]

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

@testset "SDiagonal" begin
Expand Down
23 changes: 23 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Base.getindex(m::RotMat2, i::Int) = getindex(m.elements, i)
# Rotation matrices must be unitary so `similar_type` has to return an SMatrix.
StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,4}

Base.@kwdef mutable struct KPS4{S, T, P}
v_apparent::T = zeros(S, 3)
end

@testset "Linear algebra" begin

@testset "SArray as a (mathematical) vector space" begin
Expand Down Expand Up @@ -313,6 +317,25 @@ StaticArrays.similar_type(::Union{RotMat2,Type{RotMat2}}) = SMatrix{2,2,Float64,
@test norm(SVector{0,Float64}()) isa Float64
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) isa float(Int)
@test norm(SA[SVector{0,Int}(),SVector{0,Int}()]) == norm([Int[], Int[]])

# no allocation for MArray -- issue #1126

@inline function calc_particle_forces!(s, pos1, pos2)
segment = pos1 - pos2
norm1 = norm(segment)
unit_vector = segment / norm1

v_app_perp = s.v_apparent - s.v_apparent unit_vector * unit_vector
half_drag_force = norm(v_app_perp)
nothing
end
kps4 = KPS4{Float64, MVector{3, Float64}, 6+4+1}()

pos1 = MVector{3, Float64}(1.0, 2.0, 3.0)
pos2 = MVector{3, Float64}(2.0, 3.0, 4.0)
calc_particle_forces!(kps4, pos1, pos2)
calc_particle_forces!(kps4, pos1, pos2)
@test (@allocated calc_particle_forces!(kps4, pos1, pos2)) == 0
end

@testset "trace" begin
Expand Down

2 comments on commit d1e595a

@mateuszbaran
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/77997

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.5.16 -m "<description of version>" d1e595a6a73e4f161e8f5d743c673261cd3fb2c1
git push origin v1.5.16

Please sign in to comment.