Static Compilation in Julia

Here’s an example of static compilation, adapted from the instructions here. A small program requiring GC compiles to 18 MB on my Linux machine (libc x86_64).

First, I install the Julia nightly channel:

#> juliaup add nightly

Then I cloned the Julia git repo to get the juliac scripts:

#> git clone https://github.com/JuliaLang/julia.git

Here’s a small program try.jl which stores the squares of the numbers 1, 2, … ,100 into a dictionary (GC is needed for this data structure). Then it prints out the value for the key 10.

module Try

Base.@ccallable function main()::Cint
    square_dict = Dict(i => i^2 for i in 1:100)
    println(Core.stdout, square_dict[10])
    return 0
end

end # End of module Try

I compile the program with

#> julia +nightly julia/contrib/juliac.jl --output-exe try --experimental --trim try.jl

This took about 22 seconds and produced the binary try with a size 18 MB. Executing the produced binary took about 37 milliseconds.

#> time ./try
100

real	0m0.037s
user	0m0.014s
sys     0m0.027s

P.S. since this is experimental stuff, it goes without saying that the information could become outdated very quickly.

9 Likes