Skip to content

Commit ec01673

Browse files
jishnubDrvi
authored andcommitted
LazyString in LinearAlgebra.checksquare error message (JuliaLang#53961)
This reduces dynamic dispatch and makes JET happier. Testing on v1.11: ```julia julia> import LinearAlgebra: checksquare julia> using JET julia> @report_opt checksquare(rand(2,2), rand(2,2)) ═════ 4 possible errors found ═════ ┌ checksquare(::Matrix{Float64}, ::Matrix{Float64}) @ LinearAlgebra /cache/build/builder-amdci4-1/julialang/julia-release-1-dot-11/usr/share/julia/stdlib/v1.11/LinearAlgebra/src/LinearAlgebra.jl:307 │┌ string(::String, ::Tuple{Int64, Int64}) @ Base ./strings/io.jl:189 ││┌ print_to_string(::String, ::Tuple{Int64, Int64}) @ Base ./strings/io.jl:150 │││┌ _unsafe_take!(io::IOBuffer) @ Base ./iobuffer.jl:494 ││││┌ wrap(::Type{Array}, m::MemoryRef{UInt8}, l::Int64) @ Base ./array.jl:3101 │││││ failed to optimize due to recursion: wrap(::Type{Array}, ::MemoryRef{UInt8}, ::Int64) ││││└──────────────────── │││┌ print_to_string(::String, ::Vararg{Any}) @ Base ./strings/io.jl:143 ││││ runtime dispatch detected: Base._str_sizehint(%17::Any)::Int64 │││└──────────────────── │││┌ print_to_string(::String, ::Vararg{Any}) @ Base ./strings/io.jl:148 ││││ runtime dispatch detected: print(%59::IOBuffer, %97::Any)::Any │││└──────────────────── │││┌ string(::String, ::Int64, ::String, ::Tuple{Int64}, ::String, ::Int64, ::String, ::Int64, ::String) @ Base ./strings/io.jl:189 ││││ failed to optimize due to recursion: string(::String, ::Int64, ::String, ::Tuple{Int64}, ::String, ::Int64, ::String, ::Int64, ::String) │││└──────────────────── julia> function checksquare(A...) # This PR sizes = Int[] for a in A size(a,1)==size(a,2) || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(a))")) push!(sizes, size(a,1)) end return sizes end checksquare (generic function with 2 methods) julia> @report_opt checksquare(rand(2,2), rand(2,2)) No errors detected ``` (cherry picked from commit d505c8c)
1 parent dd84a2d commit ec01673

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

stdlib/LinearAlgebra/src/LinearAlgebra.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,14 @@ julia> LinearAlgebra.checksquare(A, B)
238238
"""
239239
function checksquare(A)
240240
m,n = size(A)
241-
m == n || throw(DimensionMismatch("matrix is not square: dimensions are $(size(A))"))
241+
m == n || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(A))"))
242242
m
243243
end
244244

245245
function checksquare(A...)
246246
sizes = Int[]
247247
for a in A
248-
size(a,1)==size(a,2) || throw(DimensionMismatch("matrix is not square: dimensions are $(size(a))"))
248+
size(a,1)==size(a,2) || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(a))"))
249249
push!(sizes, size(a,1))
250250
end
251251
return sizes

0 commit comments

Comments
 (0)