From 88979c613c327d9b38187ce7db7be594415fcb0a Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 5 Apr 2024 23:18:05 +0530 Subject: [PATCH] `LazyString` in `LinearAlgebra.checksquare` error message (#53961) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 d505c8cdafa79387139f6cf3bf3aa43f0118c087) --- stdlib/LinearAlgebra/src/LinearAlgebra.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index 494572b1e8dd8b..7b3ff8f0db53d1 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -238,14 +238,14 @@ julia> LinearAlgebra.checksquare(A, B) """ function checksquare(A) m,n = size(A) - m == n || throw(DimensionMismatch("matrix is not square: dimensions are $(size(A))")) + m == n || throw(DimensionMismatch(lazy"matrix is not square: dimensions are $(size(A))")) m end function checksquare(A...) sizes = Int[] for a in A - size(a,1)==size(a,2) || throw(DimensionMismatch("matrix is not square: dimensions are $(size(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