Skip to content

Instantly share code, notes, and snippets.

@RyanGreenup
Created January 23, 2023 04:16
Show Gist options
  • Save RyanGreenup/7c58cf12387b27a8acf1e3dc44ed4286 to your computer and use it in GitHub Desktop.
Save RyanGreenup/7c58cf12387b27a8acf1e3dc44ed4286 to your computer and use it in GitHub Desktop.
Cache Julia SysImages for faster run time

Using SysImages to Speed up Julia

Install Fezzik

First install Fezzik, this has been around since 2019 and actively maintained, but it's only available from source:

using Pkg
pkg"add https://github.com/TsurHerman/Fezzik"

Write Some Code

Now create a script with some code in it that typically takes a while to run, e.g.:

cd `mktemp -d`
nvim file.jl
using Random
using Plots
using FFTW

N = 99
x = rand(Float32, N)
X = fft(x)
plot(abs.(X))
sleep(0.5)

Compile The Image

Use Auto Trace

Open a Julia REPL, and run the following code:

using Fezzik
Fezzik.auto_trace()

This inserts Fezzik into Julia's startup so it can trace what runs.

Run Some Code

Close the REPL and source that file:

julia < ./file.jl
________________________________________________________
Executed in    9.11 secs    fish           external
   usr time    8.47 secs  226.00 micros    8.47 secs
   sys time    0.84 secs  147.00 micros    0.84 secs

Write the Image

Open Julia and save the sys image that was traced by running the following (approx 5 min):

julia --threads=auto
import Fezzik

Fezzik.brute_build_local()

# Optionally stop it from tracing
# It may be desirable to keep building up used packages though.
Fezzik.auto_trace(false)

Load Julia with the Image

Close Julia and note the presence of the .so file in the directory:

ls *.so
JuliaSysimage.so*

This can be loaded as an image to save Julia from recompiling:

julia -J JuliaSysimage.so < ./file.jl
________________________________________________________
Executed in    2.07 secs    fish           external
   usr time    1.53 secs  251.00 micros    1.53 secs
   sys time    0.75 secs  166.00 micros    0.75 secs

Notes

Updates

This SysImage will need to be regenerated between updates, so disable auto_trace before an update and re-enable thereafter.

LSP

A similar thing can be implemented for the LSP with this Makefile and this config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment