References
Speculator.speculate — Functionspeculate(predicate, value; parameters...)
speculate(value; parameters...)Search for compilable methods.
See also install_speculator.
This only runs when called during precompilation or an interactive session, or when writing precompilation directives to a file.
Parameters
predicate = (::Module, ::Symbol) -> true: This must accept the signaturepredicate(::Module, ::Symbol)::Bool. Returningtruespecifies to searchgetproperty(::Module, ::Symbol), whereas returningfalsespecifies to skip the value. This is called when searching the names of aModuleif the given module and name satisfyisdefinedand!isdeprecated, and is called when searching for types from method parameters. Each component of aUniontype is checked individually. Skipped parameter types do not count towards the method specializationlimit. The default predicateReturns(true)will search every value, whereas the predicateReturns(false)will not search any value. Some useful predicates includeBase.isexported,Base.ispublic, and checking properties of the value itself.value: When given aModule,speculatewill recursively search its contents usingnames(::Module; all = true), for each defined value that is not deprecated, is not an external module, and satisifes thepredicate. For other values, each of their genericmethodsare searched for corresponding compilable methods.
Keyword parameters
background::Bool = false: Specifies whether to run on a thread in the:defaultpool. The number of available threads can be determined usingThreads.nthreads(:default).compile::Bool = true: Specifies whether to runprecompileon generated method signatures. Skipping compilation is useful for testing withverbosity = pass ∪ review. Method signatures that are known to be specialized are skipped. Note thatcompilemust betrueto save the directives to a file with thepathparameter.limit::Int = 1: Specifies the maximum number of compilable method signatures that are generated from a generic method. Values less than1will throw an error. Otherwise, method signatures will be generated from the Cartesian product each parameter type. Concrete types and those marked with@nospecializeare used directly. Otherwise, concrete types are obtained from the subtypes ofDataTypeandUnion. Setting an appropriate value prevents spending too much time precompiling a single generic method.path::String = "": Saves successful precompilation directives to a file ifcompile = trueand!isempty(path). Generated method signatures that are known to have been compiled are skipped. The resulting directives may require loading additional modules to run. CompileTraces.jl may be useful in such cases.verbosity::Verbosity = warn: Specifies what logging statements to show. If this function is used to precompile a package, this should be set tosilentorwarn. See alsoVerbosity.
Examples
julia> module Showcase
export g, h
f() = nothing
g(::Int) = nothing
h(::Union{String, Symbol}) = nothing
end;
julia> speculate(Showcase; verbosity = compile)
compile: Main.Showcase.g(::Int64)
compile: Main.Showcase.f()
julia> speculate(Showcase; verbosity = pass)
pass: Main.Showcase.g(::Int64)
julia> speculate(Showcase.h; verbosity = compile) do m, n
!(m == Core && n == :String)
end
compile: Main.Showcase.h(::Symbol)
julia> speculate(Showcase.h; limit = 2, verbosity = compile ∪ pass)
pass: Main.Showcase.h(::Symbol)
compile: Main.Showcase.h(::String)Input Speculator
Speculator.install_speculator — Functioninstall_speculator(
predicate = (::Module, ::Symbol) -> true;
background::Bool = true,
parameters...
)Install a hook that calls speculate(predicate, value; background, parameters...) on each input value in the REPL.
Subsequent calls to this function may be used to replace the hook. The hook may be removed using uninstall_speculator.
See also speculate.
Use this in a startup.jl file to reduce latency in the REPL. Since it relies on the REPL being initialized, it should be placed at the end of the file.
julia> install_speculator(; limit = 2, verbosity = compile)
julia> f() = nothing;
[ Info: Compiled `Main.f()`
julia> g(::Union{String, Symbol}) = nothing;
[ Info: Compiled `Main.g(::Symbol)`
[ Info: Compiled `Main.g(::String)`Speculator.uninstall_speculator — Functionuninstall_speculator()Uninstall the hook that may have previously been installed by install_speculator.
julia> uninstall_speculator()All Modules
Speculator.AllModules — TypeAllModulesA singleton type whose only value is all_modules.
Interface
show(::IO, ::AllModules)
Examples
julia> AllModules
AllModulesSpeculator.all_modules — Constantall_modules::AllModulesThe singleton constant of AllModules used with speculate to search for compilable methods from every loaded module.
Examples
julia> all_modules
all_modules::AllModules
julia> speculate(all_modules; compile = false, verbosity = review)Verbosities
Speculator.Verbosity — TypeVerbosity <: AbstractSet{Verbosity}A flag that determine what logging statements are shown during speculate.
This is modelled as a set, where silent is the empty set. The component sets are compile, pass, review, and warn.
Interface
This type implements the iteration and part of the AbstractSet interface.
hash(::Verbosity, ::UInt)instances(::Type{Verbosity})intersect(::Verbosity, ::Verbosity...)issubset(::Verbosity, ::Verbosity)iterate(::Verbosity, ::Vector{Verbosity})iterate(::Verbosity)length(::Verbosity)setdiff(::Verbosity, ::Verboosity...)show(::IO, MIME"text/plain", ::Verbosity)show(::IO, ::Verbosity)symdiff(::Verbosity, ::Verbosity...)union(::Verbosity, ::Verbosity...)
Examples
julia> instances(Verbosity)
(silent, compile, pass, review, warn)
julia> silent ∪ compile ∪ pass ∪ review ∪ warn
(compile ∪ pass ∪ review ∪ warn)::VerbositySpeculator.compile — Constantcompile::VerbosityA flag of Verbosity which specifies that speculate will show each compiled method signature.
Examples
julia> compile
compile::VerbositySpeculator.pass — Constantpass::VerbosityA flag of Verbosity which specifies that speculate will show each method signature that was either previously compiled or unchecked due to compile = false.
Examples
julia> pass
pass::VerbositySpeculator.review — Constantreview::VerbosityA flag of Verbosity which specifies that speculate will show a summary of the number of generated concrete method signatures, the number of generic methods found, and the search duration. If compile = true, this also shows the number of method signatures that were compiled, skipped, and warned.
Examples
julia> review
review::VerbositySpeculator.silent — Constantsilent::VerbosityA flag of Verbosity which specifies that speculate will not show any logging statements.
Examples
julia> silent
silent::VerbositySpeculator.warn — Constantwarn::VerbosityA flag of Verbosity which specifies that speculate will show warnings for failed calls to precompile. All warnings are considered a bug, and should be filed as an issue in Speculator.jl.
Examples
julia> warn
warn::Verbosity