Specifying which lines I want to be fast

I want most of my code to be interpreter-slow in order to avoid compiler latency, but one or two lines need to be compiled-fast. Is it possible to get both of those in the same session?

You can use the (experimental) Base.Experimental.@optlevel to set the optimization level for a given module, e.g.:

module SlowCode

@Base.Experimental.optlevel 0

<slow code here>

end

module FastCode

@Base.Experimental.optlevel 2 # this is the default

<fast code here>

end
4 Likes

There’s no “line based” interpreter switch - functions are compiled wholesale. Julia is compiled by default, though there is JuliaInterpreter which is mostly used in debugging.

May I ask which code is sensitive to compile latency/your usecase for this?

When I start a session it takes a minute or two to get going, which reduces productivity in exchange for performance I mostly don’t need. It’s the time-to-first-result issue but with a twist. For example, I do a bunch of imports (slow is fine), some data manipulation (slow is fine), run an estimator (needs to be fast or it’ll take all day), plot the result (slow is fine).

Ideally the compiler could look at my program and figure out how to minimize the time-to-result by trading off compilation time against run time.

You may want to look into Revise.jl (smart reloading of code) or Pluto.jl (reactive notebooks).

2 Likes