Skip to content

Commit

Permalink
precompile functions to reduce compilation time of first plot
Browse files Browse the repository at this point in the history
  • Loading branch information
yhls committed Nov 13, 2019
1 parent eab5091 commit 05eed0f
Show file tree
Hide file tree
Showing 4 changed files with 891 additions and 3 deletions.
27 changes: 27 additions & 0 deletions deps/generateprecompiles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# To figure out what should be precompiled, run this script, then move
# precompile_Plots.jl in precompiles_path (see below) to src/precompile.jl

using SnoopCompile

log_path = joinpath(tempdir(), "compiles.log")
precompiles_path = joinpath(tempdir(), "precompile")

# run examples with GR backend, logging what needs to be compiled
SnoopCompile.@snoopc log_path begin
using Plots
Plots.test_examples(:gr, disp=true)
end

# precompile calls containing the following strings are dropped
blacklist = [
# functions defined in examples
"PlotExampleModule",
# the following are not visible to Plots, only its dependencies
"CategoricalArrays",
"FixedPointNumbers",
"SparseArrays"
]

data = SnoopCompile.read(log_path)
pc = SnoopCompile.parcel(reverse!(data[2]), blacklist=blacklist)
SnoopCompile.write(precompiles_path, pc)
3 changes: 3 additions & 0 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,7 @@ end

const CURRENT_BACKEND = CurrentBackend(:none)

include("precompile.jl")
_precompile_()

end # module
13 changes: 10 additions & 3 deletions src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ PlotExample("3D",

PlotExample("DataFrames",
"Plot using DataFrame column symbols.",
[:(begin
[:(using StatsPlots), # can't be inside begin block because @df gets expanded first
:(begin
import RDatasets
iris = RDatasets.dataset("datasets", "iris")
@df iris scatter(:SepalLength, :SepalWidth, group=:Species,
Expand Down Expand Up @@ -354,7 +355,8 @@ PlotExample("Layouts, margins, label rotation, title location",

PlotExample("Boxplot and Violin series recipes",
"",
[:(begin
[:(using StatsPlots), # can't be inside begin block because @df gets expanded first
:(begin
import RDatasets
singers = RDatasets.dataset("lattice", "singer")
@df singers violin(:VoicePart, :Height, line = 0, fill = (0.2, :blue))
Expand Down Expand Up @@ -518,7 +520,12 @@ function test_examples(pkgname::Symbol, idx::Int; debug = false, disp = true)
@info("Testing plot: $pkgname:$idx:$(_examples[idx].header)")
backend(pkgname)
backend()
map(eval, _examples[idx].exprs)

# prevent leaking variables (esp. functions) directly into Plots namespace
m = Module(:PlotExampleModule)
Base.eval(m, :(using Plots))
map(exprs -> Base.eval(m, exprs), _examples[idx].exprs)

plt = current()
if disp
gui(plt)
Expand Down
Loading

0 comments on commit 05eed0f

Please sign in to comment.