Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 40 Β· Performance

How Julia Compiles Code

See how Julia's just-in-time compiler specializes methods on argument types, why the first call is slow, and how to inspect the result with @code_warntype.

Julia feels like a scripting language: you type code into a REPL and it runs. Yet well-written Julia is often within a small factor of C. The two facts are connected by one design decision, just-in-time (JIT) compilation. The first time a function runs with a particular kind of arguments, Julia compiles a version of it into machine code for exactly those types, and every later call with the same types reuses that fast version.

This lesson explains the pipeline from source text to machine code, what specialization and type inference mean, why the very first call of anything is slow, and which tools show you what the compiler did. Julia itself cannot run on this site, so the Julia code is read-only and the outputs you see in comments are illustrative. The Python stand-ins model the key ideas, a cache of specialized methods and a tiny type inferencer, in a way you can run and change.

From source to machine code

When you call a function, Julia works through several stages. Understanding them tells you where time goes.

flowchart LR
    A["Source code"] --> B["Parse<br/>(syntax tree)"]
    B --> C["Lower<br/>(simplified form)"]
    C --> D["Type inference<br/>(for these argument types)"]
    D --> E["Optimize"]
    E --> F["LLVM<br/>(machine code)"]
    F --> G["Run"]

The compiler runs the middle stages once per method and set of argument types, then caches the result.

  1. Parse and lower. Your text becomes an expression tree, which is then rewritten into a smaller set of simple operations. This is independent of types.
  2. Type inference. Starting from the concrete types of the arguments, Julia works out the type of every intermediate value, as far as it can.
  3. Optimization. Knowing the types, it can resolve which methods will be called, inline small functions and remove redundant work.
  4. LLVM. The optimized code is passed to the LLVM compiler library, which generates efficient native code for your CPU.

The result is stored in a cache keyed by the method and the argument types. Julia calls each cached version a method instance.

Specialization on types

Take a trivial function:

JULIA
add_one(x) = x + 1

add_one(3)       # compiles add_one(::Int64), result 4
add_one(5)       # reuses the Int64 version
add_one(2.5)     # compiles add_one(::Float64), result 3.5
add_one(3)       # still the Int64 version, no new work

Nothing in the definition mentions Int64 or Float64. Julia compiles a separate, tight version for each combination the program actually uses. For Int64 it emits one integer add; for Float64, one floating-point add. There is no runtime check "what type is x?" inside the compiled code, because the type was fixed when it was compiled. This is why you do not need to write type annotations on function arguments to make code fast: annotations are for dispatch and documentation, as described in Multiple Dispatch.

One definition, many fast versions

You write f(x) One generic definition.
Julia sees Int64 First call with an integer.
Compiles for Int64 Machine code just for integers.
Caches it The next Int64 call is instant.

A Float64 call triggers its own compile, once.

1 / 2

A cache of specialized methods

The next stand-in does not compile anything, but it models the bookkeeping. A function object holds a cache keyed by the tuple of argument types, and the first call with new types has to "compile" (here, we only count). The counts are deterministic, unlike timings.

Python A cache of method instances

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only two "compilations" happen for six calls. In real Julia those compilations take milliseconds each, which is why the first call of a function feels slow and the next ones do not.

First-call latency

You will notice this in practice. The first time you call a plotting function or load a large package, there is a pause. Part of it is compiling your own code, but much comes from the package's code, which must be compiled for the types you use. Time is split between compilation and running, and @time reports the compilation share in recent versions:

JULIA
function sumsq(v)
    s = 0.0
    for x in v
        s += x * x
    end
    return s
end

v = rand(1_000_000)
@time sumsq(v)   # 0.04 seconds (… allocations, 80% compilation time)  <- first call
@time sumsq(v)   # 0.001 seconds (0 allocations)                        <- second call

The numbers are only illustrative; yours will differ. The pattern matters: the first measurement mixes compile time with run time, so always run a function once before timing it, or use @btime from BenchmarkTools, which repeats the call. We use it in Performance Tips.

Julia reduces the delay in several ways:

  • Precompilation. When you install or update a package, Julia caches its parsed and, increasingly, compiled code. That is the work behind the "Precompiling…" message after Pkg.add.
  • Package authors include precompile workloads, small example calls recorded in the cache, so common calls are already compiled.
  • Keep one session open. Julia's cache lives in the process. Tools such as Revise let you edit code without restarting.
  • System images. The PackageCompiler package can bake a set of packages into a custom Julia image so they start fast, which is useful for command-line tools and servers.

The cost is not a flaw you must live with; it is the price paid once so that a hot loop, run a billion times, costs nothing extra.

Type inference and type stability

The compiler is only as fast as its knowledge of types. A function is type stable when the type of its result can be predicted from the types of its arguments. Compare:

JULIA
stable(x)   = x > 0 ? x : zero(x)    # always the same type as x
unstable(x) = x > 0 ? x : 0.0        # Int input: sometimes Int64, sometimes Float64

For stable(5) the inferred return type is Int64. For unstable(5) the compiler can only conclude Union{Float64, Int64}: it has to carry a small tag with each result and branch on it later. Small unions are handled well, but when inference degrades all the way to Any, the compiled code must box values and look methods up at run time, which is often tens of times slower.

To make that visible, a stand-in can infer return types from a table of "type rules":

Python A toy type inferencer

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real compiler does the same kind of reasoning across thousands of operations, and it "gives up" (infers Any) when it cannot decide.

Looking at what the compiler did

Julia lets you view every stage. These macros live in the InteractiveUtils standard library, which the REPL loads for you.

JULIA
@code_lowered unstable(5)    # the lowered form, before types
@code_typed   unstable(5)    # after inference, with the inferred types
@code_warntype unstable(5)   # like code_typed, with problem types highlighted
@code_llvm    stable(5)      # the LLVM intermediate representation
@code_native  stable(5)      # the CPU assembly

@code_warntype is the one you will use most. In a terminal it colors problems: red marks Any, yellow marks small unions. A shortened, illustrative output looks like this:

JULIA
julia> @code_warntype unstable(5)
MethodInstance for unstable(::Int64)
  from unstable(x) @ Main REPL[2]:1
Arguments
  #self#::Core.Const(unstable)
  x::Int64
Body::Union{Float64, Int64}
1 ─ %1 = (x > 0)::Bool
└──      goto #3 if not %1
2 ─      return x
3 ─      return 0.0

The line starting with Body:: states the inferred return type, and any variable annotated ::Any deserves your attention. The exact layout changes from version to version; what matters is reading the types. @code_llvm and @code_native are for advanced tuning: seeing that stable(5) becomes a handful of instructions with no calls is a satisfying confirmation that the abstractions cost nothing.

Rather than reading assembly, ask three practical questions: is the function type stable (@code_warntype)? does it allocate memory it does not need (@time or @allocated)? does it touch non-constant globals? Most speed problems are one of these three.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. What does Julia do the first time you call f(2.5) for a function f(x) defined without type annotations?

    Julia specializes each method on the concrete types of the arguments at the first call and reuses that machine code afterwards.
  2. 2. Why is the first call of a function usually slower than the second?

    Type inference, optimization and LLVM code generation happen once per method instance. The second call finds the compiled code in the cache.
  3. 3. g(x) = x > 0 ? x : 0.0 is called with an Int64. What is its inferred return type?

    One branch returns the integer, the other a float, so the compiler can only say the result is one of the two. That is type instability, though only a mild case.
  4. 4. Which macro shows inferred types with problem spots highlighted?

    @code_warntype prints the inferred types of a call. Variables or return values typed Any or large unions are red flags.

Summary

  • Julia compiles each method just in time, specialized on the concrete types of its arguments, and caches the machine code.
  • Pipeline: parse, lower, type inference, optimization, LLVM, native code.
  • The first call includes compile time; precompilation, long-lived sessions and system images reduce the wait.
  • Type-stable code lets the compiler know every type, which is the foundation of Julia's speed.
  • @code_warntype, @code_typed, @code_llvm and @code_native show what the compiler produced.

Now put this knowledge to work in Performance Tips.