How to recompile specific methods

I don’t know why, but sometimes the precompilation produces strange results. Typically, a recompilation due to invalidation will fix the problem (even if there is essentially no change in the code). The technique of deleting the “*.ji” file will not change the result, as it will re-run the problematic precompilation.
(see the comment below for an example)

So, is there a way to prompt for a recompilation of specific methods?

Not sure if this answers your question, but if you are using Revise.jl you can ask it to check changes and recompile a whole Module. But you may as well be having some age-of-the-world problems.

1 Like

Yes, I’m using Revise.jl for development, and applying the mechanism that Revise.jl uses may help me achieve my goal. However, it would be going too far into Julia’s internals.

I would like to suggest a workaround to users of packages (e.g. Colors.jl, which has been having the precompilation problem for over a year).

If I understand correctly, you could always use precompile on your required method signatures

help?> precompile
search: precompile __precompile__

  precompile(f, args::Tuple{Vararg{Any}})

  Compile the given function f for the argument tuple (of types) args, but do not execute it.

I think precompile is the right approach in general, but it should not be useful for methods that are already precompiled/compiled.

Can you describe in more detail which problem you encountered and how it can be reproduced?

Here is an example.

julia> using Colors, BenchmarkTools

julia> rgb_f64 = rand(RGB{Float64}, 1000);

julia> @btime convert.(XYZ{Float64}, $rgb_f64);
  130.900 μs (5760 allocations: 113.48 KiB)

julia> precompile(Tuple{typeof(convert), Type{XYZ{Float64}}, RGB{Float64}}) # for example
true

julia> @btime convert.(XYZ{Float64}, $rgb_f64);
  130.900 μs (5760 allocations: 113.48 KiB)

julia> @noinline Colors.pow12_5(x::Float64) = x^2 * exp(0.4 * log(x)) # force invalidation (the body is not changed)

julia> @btime convert.(XYZ{Float64}, $rgb_f64);
  50.700 μs (2 allocations: 23.52 KiB)

julia> versioninfo()
Julia Version 1.7.0-DEV.1133
Commit db8d09609c (2021-05-21 13:48 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-11.0.1 (ORCJIT, skylake)

For the root causes, the following would be appropriate places for discussion, not here.
https://github.com/JuliaLang/julia/issues/35972
https://github.com/JuliaLang/julia/issues/34055