Skip to content

Commit bfd1c05

Browse files
authored
Add a work-around on Julia 1.0 for #857 (#859)
* Add a work-around on Julia 1.0 for #857 `StaticVecOrMatLike` is a very large union, and this caused Julia 1.0.x to use a large amount of memory when compiling StaticArrays in the presence of other packages that also extend `LinearAlgebra.mul!` in a similar fashion. As an example, this led to JuMP using more than 4 GB of RAM when running `using JuMP`, causing its CI to crash. Computing the eltypes within the function, rather than specifying them in the type arguments greatly improves the issue, with `using JuMP` going from: julia> @time using JuMP [ Info: Precompiling JuMP [4076af6c-e467-56ae-b986-b466b2749572] Killed to: julia> @time using JuMP [ Info: Precompiling JuMP [4076af6c-e467-56ae-b986-b466b2749572] 97.868305 seconds (12.75 M allocations: 805.413 MiB, 0.30% gc time) * Remove Julia 1.0 branch in favor of single implementation
1 parent 0e43102 commit bfd1c05

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
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.0.0"
3+
version = "1.0.1"
44

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

src/matrix_multiply_add.jl

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,33 @@ const StaticVecOrMatLikeForFiveArgMulDest{T} = Union{
218218
α::Number, β::Number) = _mul!(TSize(dest), mul_parent(dest), Size(A), Size(B), A, B,
219219
AlphaBeta(α,β))
220220

221-
@inline function LinearAlgebra.mul!(dest::StaticVecOrMatLike{TDest}, A::StaticVecOrMatLike{TA},
222-
B::StaticVecOrMatLike{TB}) where {TDest,TA,TB}
221+
# See https://github.com/JuliaArrays/StaticArrays.jl/issues/857.
222+
# `StaticVecOrMatLike` is a very large union, and this caused Julia 1.0.x to
223+
# use a large amount of memory when compiling StaticArrays in the presence
224+
# of other packages that also extend `LinearAlgebra.mul!` in a similar
225+
# fashion. As an example, this led to JuMP using more than 4 GB of RAM when
226+
# running `using JuMP`, causing its CI to crash.
227+
#
228+
# Computing the eltypes within the function, rather than specifying them in
229+
# the type arguments greatly improves the issue, with `using JuMP` going
230+
# from:
231+
# julia> @time using JuMP
232+
# [ Info: Precompiling JuMP [4076af6c-e467-56ae-b986-b466b2749572]
233+
# Killed
234+
# to:
235+
# julia> @time using JuMP
236+
# [ Info: Precompiling JuMP [4076af6c-e467-56ae-b986-b466b2749572]
237+
# 101.723598 seconds (12.85 M allocations: 812.902 MiB, 0.31% gc time)
238+
@inline function LinearAlgebra.mul!(
239+
dest::StaticVecOrMatLike,
240+
A::StaticVecOrMatLike,
241+
B::StaticVecOrMatLike,
242+
)
243+
TDest, TA, TB = eltype(dest), eltype(A), eltype(B)
223244
TMul = promote_op(matprod, TA, TB)
224245
return _mul!(TSize(dest), mul_parent(dest), Size(A), Size(B), A, B, NoMulAdd{TMul, TDest}())
225246
end
226247

227-
228248
"Calculate the product of the dimensions being multiplied. Useful as a heuristic for unrolling."
229249
@inline multiplied_dimension(A::Type{<:StaticVecOrMatLike}, B::Type{<:StaticVecOrMatLike}) =
230250
prod(size(A)) * size(B,2)
@@ -310,7 +330,7 @@ function uplo_access(sa, asym, k, j, uplo)
310330
elseif uplo == :unit_lower_triangular
311331
if k > j
312332
return :($asym[$(LinearIndices(sa)[k, j])])
313-
elseif k == j
333+
elseif k == j
314334
return :(oneunit($TAsym))
315335
else
316336
return :(zero($TAsym))
@@ -439,7 +459,7 @@ end
439459
else
440460
ab = [:(a[$i] * transpose(b[$j])) for i = 1:sa[1], j = 1:sb[2]]
441461
end
442-
462+
443463
exprs = _muladd_expr(lhs, ab, _add)
444464

445465
return quote
@@ -558,7 +578,7 @@ end
558578
@inbounds $(Expr(:block, exprs...))
559579
end
560580
end
561-
581+
562582
return quote
563583
@_inline_meta
564584
α = alpha(_add)

0 commit comments

Comments
 (0)