andreasvarga / MatrixEquations.jl

Solution of Lyapunov, Sylvester and Riccati matrix equations using Julia
MIT License
79 stars 8 forks source link

add extra precompilation #18

Open baggepinnen opened 2 years ago

baggepinnen commented 2 years ago

The PR adds a file with statements to execute to trigger precompilation. Julia v1.8 has gotten a number of improvements to the ability of caching precompiled code, making this potentially worthwhile. The benchmark below shows that the snippet

A = [-0.9 0; 0.01 -0.9]
B = randn(2,2)
plyapd(A,B)
lyapd(A,B)

plyapc(A,B)
lyapc(A,B)

goes from taking 11.7 seconds to run the first time to taking 6.7 seconds with precompilation. The price to pay for this is that the precompilation itself takes longer time, indicated below. Precompilation only happens once though, and does then not happen again until MatrixEquations.jl is updated. This is thus likely a good trade-off for end users. Developers of this package, however, might have to pay the precompilation time repeatedly, making the trade-off significantly worse. This PR thus only performs the precompilation if the package is not checked out for development. Developers will thus not see any difference from this PR.

# Without precompilation
julia> @time using MatrixEquations
[ Info: Precompiling MatrixEquations [99c1a7ee-ab34-5fd5-8076-27c950a045f4]
  1.624666 seconds (324.93 k allocations: 23.586 MiB, 0.84% compilation time)

julia> @time include("/home/fredrikb/.julia/dev/MatrixEquations/src/precompilation.jl");
 11.741624 seconds (61.61 M allocations: 2.683 GiB, 12.51% gc time, 99.94% compilation time)

# with extra precmopilation
 julia> @time using MatrixEquations
[ Info: Precompiling MatrixEquations [99c1a7ee-ab34-5fd5-8076-27c950a045f4]
  9.721895 seconds (437.76 k allocations: 43.554 MiB, 0.13% compilation time)

julia> @time include("/home/fredrikb/.julia/dev/MatrixEquations/src/precompilation.jl");
  6.673593 seconds (2.43 M allocations: 86.612 MiB, 0.19% gc time, 99.77% compilation time)

I only added a small number of statements to the precompilation file, more statements can be added at the expense of longer precompilation times. The best trade-off is likely achieved by including calls to core routines that are used by many other functions.