Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce time to first plot by precompiling functions #2250

Merged
merged 1 commit into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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