Skip to content

Commit dde2732

Browse files
committed
Fix #388, hcat allocations with Julia master
Give vcat the same treatment.
1 parent e5bd424 commit dde2732

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/linalg.jl

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ end
8888
end
8989
end
9090

91-
@inline vcat(a::Union{StaticVector,StaticMatrix}) = a
92-
@inline vcat(a::Union{StaticVector, StaticMatrix}, b::Union{StaticVector,StaticMatrix}) = _vcat(Size(a), Size(b), a, b)
93-
@generated function _vcat(::Size{Sa}, ::Size{Sb}, a::Union{StaticVector, StaticMatrix}, b::Union{StaticVector,StaticMatrix}) where {Sa, Sb}
91+
@inline vcat(a::StaticVecOrMat) = a
92+
@inline vcat(a::Union{StaticVector, StaticMatrix}, b::StaticVecOrMat) = _vcat(Size(a), Size(b), a, b)
93+
@inline vcat(a::StaticVecOrMat, b::StaticVecOrMat, c::StaticVecOrMat...) =
94+
vcat(vcat(a,b), vcat(c...))
95+
@generated function _vcat(::Size{Sa}, ::Size{Sb}, a::StaticVecOrMat, b::StaticVecOrMat) where {Sa, Sb}
9496
if Size(Sa)[2] != Size(Sb)[2]
9597
throw(DimensionMismatch("Tried to vcat arrays of size $Sa and $Sb"))
9698
end
@@ -112,18 +114,14 @@ end
112114
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
113115
end
114116
end
115-
# TODO make these more efficient
116-
@inline vcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}) =
117-
vcat(vcat(a,b), c)
118-
@inline vcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}...) =
119-
vcat(vcat(a,b), c...)
120-
121117

122118
@inline hcat(a::StaticVector) = similar_type(a, Size(Size(a)[1],1))(a)
123119
@inline hcat(a::StaticMatrix) = a
124-
@inline hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}) = _hcat(Size(a), Size(b), a, b)
120+
@inline hcat(a::StaticVecOrMat, b::StaticVecOrMat) = _hcat(Size(a), Size(b), a, b)
121+
@inline hcat(a::StaticVecOrMat, b::StaticVecOrMat, c::StaticVecOrMat...) =
122+
hcat(hcat(a,b), hcat(c...))
125123

126-
@generated function _hcat(::Size{Sa}, ::Size{Sb}, a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}) where {Sa, Sb}
124+
@generated function _hcat(::Size{Sa}, ::Size{Sb}, a::StaticVecOrMat, b::StaticVecOrMat) where {Sa, Sb}
127125
if Sa[1] != Sb[1]
128126
throw(DimensionMismatch("Tried to hcat arrays of size $Sa and $Sb"))
129127
end
@@ -138,11 +136,6 @@ end
138136
@inbounds return similar_type(a, promote_type(eltype(a), eltype(b)), Size($Snew))(tuple($(exprs...)))
139137
end
140138
end
141-
# TODO make these more efficient
142-
@inline hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}) =
143-
hcat(hcat(a,b), c)
144-
@inline hcat(a::Union{StaticVector,StaticMatrix}, b::Union{StaticVector,StaticMatrix}, c::Union{StaticVector,StaticMatrix}...) =
145-
hcat(hcat(a,b), c...)
146139

147140
@inline Base.zero(a::SA) where {SA <: StaticArray} = zeros(SA)
148141
@inline Base.zero(a::Type{SA}) where {SA <: StaticArray} = zeros(SA)

test/linalg.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ using StaticArrays, Test, LinearAlgebra
161161

162162
vcat(SVector(1.0f0), SVector(1.0)) === SVector(1.0, 1.0)
163163
hcat(SVector(1.0f0), SVector(1.0)) === SMatrix{1,2}(1.0, 1.0)
164+
165+
# issue #388
166+
let x = SVector(1, 2, 3)
167+
# current limit: 34 arguments
168+
hcat(
169+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
170+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x)
171+
allocs = @allocated hcat(
172+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
173+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x)
174+
@test allocs == 0
175+
vcat(
176+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
177+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x)
178+
allocs = @allocated vcat(
179+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
180+
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x)
181+
@test allocs == 0
182+
end
164183
end
165184

166185
@testset "normalization" begin

0 commit comments

Comments
 (0)