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 = Returns(true)
: This must accept the signaturepredicate(::Module, ::Symbol)::Bool
. Returningtrue
specifies to searchgetproperty(::Module, ::Symbol)
, whereas returningfalse
specifies to skip the value. This is called when searching the names of aModule
if the given module and name satisfyisdefined
and!isdeprecated
, and is called when searching for types from method parameters. Each component of aUnion
type 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
,speculate
will 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 genericmethods
are searched for corresponding compilable methods.
Keyword parameters
background::Bool = false
: Specifies whether to run on a thread in the:default
pool. The number of available threads can be determined usingThreads.nthreads(:default)
.compile::Bool = true
: Specifies whether to runprecompile
on generated method signatures. Skipping compilation is useful for testing withverbosity = debug ∪ review
. Method signatures that are known to be specialized are skipped. Note thatcompile
must betrue
to save the directives to a file with thepath
parameter.limit::Int = 1
: Specifies the maximum number of concrete method signatures that are generated from a generic method. Values less than1
will throw an error. Otherwise, method signatures will be generated from the Cartesian product each parameter type. Concrete types and those marked with@nospecialize
are used directly. Otherwise, concrete types are obtained from the subtypes ofDataType
andUnion
. Setting an appropriate value prevents spending too much time precompiling a single generic method.path::String = ""
: Saves successful precompilation directives to a file ifcompile = true
and!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 tosilent
orwarn
. 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::Module, ::Symbol) -> _module ∉ (Base, Core);
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 = debug)
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
— TypeAllModules
A singleton type whose only value is all_modules
.
Interface
show(::IO, ::AllModules)
Examples
julia> AllModules
AllModules
Speculator.all_modules
— Constantall_modules::AllModules
The 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)::Verbosity
Speculator.compile
— Constantcompile::Verbosity
A flag of Verbosity
which specifies that speculate
will show each compiled method signature.
Examples
julia> compile
compile::Verbosity
Speculator.pass
— Constantpass::Verbosity
A 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::Verbosity
Speculator.review
— Constantreview::Verbosity
A 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::Verbosity
Speculator.silent
— Constantsilent::Verbosity
A flag of Verbosity
which specifies that speculate
will not show any logging statements.
Examples
julia> silent
silent::Verbosity
Speculator.warn
— Constantwarn::Verbosity
A 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