⬆️ ⬇️

Accelerate the launch of Julia with PackageCompiler.jl

Going to the official website of the programming language Julia , you can see the statement: "Julia is fast!". However, in practice new users face the problem of slow loading of modules, especially graphics [ 1 , 2 ]. Dialectically, the reason for slow start is the use of JIT compilation, the use of which ensures high performance of the language. If desired, the features of JIT can be found in other articles . This article will discuss a more practical task - how to speed up the launch of modules in Julia using PackageCompiler.jl .



This article uses julia v1.1.0 . In addition, the operation of the method was tested on julia v1.0.3 .

A trivial script was used to estimate the execution time:



@time using Plots x = 0:0.01:10000 y = @. sin(Ο€*x) @time plot(x,y) # ,   @time plot(x,y) # ,   


Running this script on stock julia, the following values ​​are obtained:



  2.804964 seconds (5.03 M allocations:x 291.243 MiB, 5.35% gc time) 13.546407 seconds (45.64 M allocations: 2.232 GiB, 9.00% gc time) 0.013167 seconds (2.14 k allocations: 7.788 MiB) 


As you can see, the first execution of the plot command lasts an incredible 14 seconds. Following him - already 0.01 s.



Let's try to improve these indicators. First, install the PackageCompiler module:



 import Pkg Pkg.add("PackageCompiler") 


Next, let's test the dependency build of the Plots module:



 import Pkg Pkg.add("Arpack") Pkg.build("Arpack") 


If the execution of the last command was interrupted with an error

ERROR: LoadError: LibraryProduct(...) is not satisfied, cannot generate deps.jl!

then you are a victim of Issue # 5 . The problem is related to the use of external dynamic libraries and will have to be solved after integrating the BinaryProvider . As a temporary solution, on the advice of blegat , we will build the library ourselves:



 git clone https://github.com/opencollab/arpack-ng.git /tmp/arpack && cd /tmp/arpack git checkout 3.5.0 bash bootstrap ./configure --enable-mpi --enable-shared make cp SRC/.libs/libarpack.so.2.0.0 ~/.julia/packages/Arpack/UiiMc/deps/usr/lib/ 


After performing these manipulations, the error should disappear:



 import Pkg Pkg.build("Arpack") 


It is time to compile the Plots package:



 import Pkg Pkg.add("Plots") using PackageCompiler compile_package("Plots") 


Compilation lasts a long time with a lot of text in the console Pay attention to the last line of the log:



 β”Œ Info: Not replacing system image. β”” You can start julia with `julia -J /home/user/.julia/packages/PackageCompiler/oT98U/sysimg/sys.so` at a posix shell to load the compiled files. "/home/user/.julia/packages/PackageCompiler/oT98U/sysimg/sys.so" 


It shows the location of the compiled image and a hint how to run it.

Run the test script using this image:



 julia -J /home/user/.julia/packages/PackageCompiler/oT98U/sysimg/sys.so /tmp/test.jl 


  0.000251 seconds (501 allocations: 26.766 KiB) 0.028105 seconds (22.55 k allocations: 8.847 MiB) 0.017211 seconds (2.14 k allocations: 7.788 MiB, 18.16% gc time) 


Thus, the time of the first launch of the plot function was reduced from 14 s to 0.03 s.



This method can be used to export images of several libraries, modify the system image and compile static programs. To learn more about PackageCompiler.jl, refer to the description in the project repository .



')

Source: https://habr.com/ru/post/451068/



All Articles