References

Speculator.speculateFunction
speculate(predicate, value; parameters...)
speculate(value; parameters...)

Search for compilable methods.

See also install_speculator.

Tip

Use this in a package to reduce latency.

Note

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 signature predicate(::Module, ::Symbol)::Bool. Returning true specifies to search getproperty(::Module, ::Symbol), whereas returning false specifies to skip the value. This is called when searching the names of a Module if the given module and name satisfy isdefined and !isdeprecated, and is called when searching for types from method parameters. Each component of a Union type is checked individually. Skipped parameter types do not count towards the method specialization limit. The default predicate Returns(true) will search every value, whereas the predicate Returns(false) will not search any value. Some useful predicates include Base.isexported, Base.ispublic, and checking properties of the value itself.
  • value: When given a Module, speculate will recursively search its contents using names(::Module; all = true), for each defined value that is not deprecated, is not an external module, and satisifes the predicate. For other values, each of their generic methods 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 using Threads.nthreads(:default).
  • compile::Bool = true: Specifies whether to run precompile on generated method signatures. Skipping compilation is useful for testing with verbosity = debug ∪ review. Method signatures that are known to be specialized are skipped. Note that compile must be true to save the directives to a file with the path parameter.
  • limit::Int = 1: Specifies the maximum number of concrete method signatures that are generated from a generic method. Values less than 1 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 of DataType and Union. Setting an appropriate value prevents spending too much time precompiling a single generic method.
  • path::String = "": Saves successful precompilation directives to a file if compile = 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 to silent or warn. See also Verbosity.

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)
source

Input Speculator

Speculator.install_speculatorFunction
install_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.

Tip

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.

Note

This function has no effect in non-interactive sessions.

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)`
source

All Modules

Speculator.all_modulesConstant
all_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)
source

Verbosities

Speculator.VerbosityType
Verbosity <: 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
source
Speculator.passConstant
pass::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
source
Speculator.reviewConstant
review::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
source
Speculator.warnConstant
warn::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
source