Skip to content

Commit bea9ad5

Browse files
Use multiple dispatch for verbose processing to eliminate runtime dispatches
Replaced if-else type checking with multiple dispatch methods for processing the verbose parameter. This eliminates runtime dispatches that JET was detecting. Changes: - Created _process_verbose_param helper methods using multiple dispatch - One method for each supported type: LinearVerbosity, AbstractVerbosityPreset, Bool - The verbose processing now compiles to direct method dispatch - Fully type-stable for all supported verbose parameter types This should eliminate the runtime dispatches seen in JET analysis.
1 parent 5abf86b commit bea9ad5

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/common.jl

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,18 @@ default_alias_b(::AbstractSparseFactorization, ::Any, ::Any) = true
232232

233233
DEFAULT_PRECS(A, p) = IdentityOperator(size(A)[1]), IdentityOperator(size(A)[2])
234234

235+
# Helper functions for processing verbose parameter with multiple dispatch (type-stable)
236+
@inline _process_verbose_param(verbose::LinearVerbosity) = (verbose, verbose)
237+
@inline function _process_verbose_param(verbose::SciMLLogging.AbstractVerbosityPreset)
238+
verbose_spec = LinearVerbosity(verbose)
239+
return (verbose_spec, verbose_spec)
240+
end
241+
@inline function _process_verbose_param(verbose::Bool)
242+
# @warn "Using `true` or `false` for `verbose` is being deprecated."
243+
verbose_spec = verbose ? LinearVerbosity() : LinearVerbosity(SciMLLogging.None())
244+
return (verbose_spec, verbose)
245+
end
246+
235247
"""
236248
__init_u0_from_Ab(A, b)
237249
@@ -324,26 +336,7 @@ function __init(prob::LinearProblem, alg::SciMLLinearSolveAlgorithm,
324336
copy(A)
325337
end
326338

327-
if verbose isa LinearVerbosity
328-
verbose_spec = verbose
329-
init_cache_verb = verbose_spec
330-
elseif verbose isa SciMLLogging.AbstractVerbosityPreset
331-
verbose_spec = LinearVerbosity(verbose)
332-
init_cache_verb = verbose_spec
333-
elseif verbose isa Bool
334-
# @warn "Using `true` or `false` for `verbose` is being deprecated. Please use a `LinearVerbosity` type to specify verbosity settings.
335-
# For details see the verbosity section of the common solver options documentation page."
336-
init_cache_verb = verbose
337-
if verbose
338-
verbose_spec = LinearVerbosity()
339-
else
340-
verbose_spec = LinearVerbosity(SciMLLogging.None())
341-
end
342-
else
343-
# Fallback for any other type
344-
verbose_spec = verbose
345-
init_cache_verb = verbose_spec
346-
end
339+
verbose_spec, init_cache_verb = _process_verbose_param(verbose)
347340

348341
b = if issparsematrix(b) && !(A isa Diagonal)
349342
Array(b) # the solution to a linear solve will always be dense!

0 commit comments

Comments
 (0)