# Creating a sysimage for fast plotting with Plots.jl

A common complaint about Julia is that the "time to first plot" is a bit longer than desired. In this example, we will create a sysimage that is made to improve this.

To get a reference, we measure the time it takes to create the first plot with the default sysimage:

julia> @time using Plots
2.943946 seconds (8.11 M allocations: 567.656 MiB, 6.47% gc time)

julia> @time p = plot(rand(2,2));
1.940742 seconds (3.29 M allocations: 197.525 MiB, 4.65% gc time)

julia> @time display(p);
5.132217 seconds (15.00 M allocations: 847.491 MiB, 2.57% gc time, 45.95%)

This is approximately 19 seconds from the start of Julia to the first plot.

We now create a precompilation file with exactly this workload in a file called precompile_plots.jl in the current directory:

using Plots
p = plot(rand(2,2))
display(p)

The custom sysimage is then created as:

using PackageCompiler
create_sysimage(["Plots"], sysimage_path="sys_plots.so", precompile_execution_file="precompile_plots.jl")

If we now start Julia with the flag --sysimage sys_plots.so and re-time our previous commands:

julia> @time using Plots
0.000159 seconds (1.07 k allocations: 79.797 KiB)

julia> @time p = plot(rand(2,2));
0.037670 seconds (74.09 k allocations: 4.703 MiB)

julia> @time display(p);
0.331869 seconds (278.38 k allocations: 7.900 MiB)

which is a sizeable speedup.

Note that since we have more stuff in our sysimage, Julia is slightly slower to start (0.35 seconds on this machine):

# Default sysimage
➜ time julia -e ''
julia -e ''  0.06s user 0.06s system 130% cpu 0.088 total

# Custom sysimage
➜ time julia --sysimage sys_plots.so -e ''
julia --sysimage sys_plots.so -e ''  0.43s user 0.30s system 347% cpu 0.211 total
{"ft_lang_label":"__label__en","ft_lang_prob":0.7125158,"math_prob":0.84045523,"size":1737,"snap":"2022-40-2023-06","text_gpt3_token_len":525,"char_repetition_ratio":0.14541258,"word_repetition_ratio":0.01509434,"special_character_ratio":0.33275762,"punctuation_ratio":0.17585301,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98624384,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-31T04:23:53Z\",\"WARC-Record-ID\":\"<urn:uuid:5776d8d2-0d3a-484f-ac7b-3effe0186f5e>\",\"Content-Length\":\"8287\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:933c36b6-248c-41f8-8b53-6be8546c4547>\",\"WARC-Concurrent-To\":\"<urn:uuid:fb65894a-36db-4aee-b5b5-173a008a6a9b>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://julialang.github.io/PackageCompiler.jl/dev/examples/plots.html\",\"WARC-Payload-Digest\":\"sha1:EWK2YGELKP6US4R6BETCR7R4CHMRNWLU\",\"WARC-Block-Digest\":\"sha1:XSQLCJVJX5IGSSNMYUURO6Q2JC6ZZBQE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499842.81_warc_CC-MAIN-20230131023947-20230131053947-00244.warc.gz\"}"}