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

Saving figures via Plots.savefig and its format-specific siblings like Plots.pdf in Plots v1.20.0 is extremely slow. #3748

Closed
SeSodesa opened this issue Aug 6, 2021 · 3 comments
Labels
performance speedups and slowdowns

Comments

@SeSodesa
Copy link

SeSodesa commented Aug 6, 2021

The culprit code in question goes as follows:

function draw_scatter_and_line(
    xdata,
    ydata,
    gradient,
    constant_term,
    scatter_title,
    line_title,
    xlabel,
    ylabel,
    filename
)
    img_path = joinpath(@__DIR__, filename)
    
    # Generate plot object
    img = Plots.plot(
        xlabel=xlabel,
        ylabel=ylabel,
        legend=:bottomright)

    # Measurements
    Plots.scatter!(
        img,
        xdata,
        ydata,
        label=scatter_title,
        color=:steelblue)

    # Generate fit
    fit_x = [xdata[begin] - abs(xdata[end] / 10), xdata[end] + xdata[end] / 10]
    fit_y = gradient .* fit_x .+ constant_term

    # Draw fitted line:
    Plots.plot!(
        img,
        fit_x,
        fit_y,
        linestyle=:dash,
        label=line_title,
        color=:steelblue)

    # The bottleneck    
    @time Plots.pdf(img, img_path)
end

When called with

xdata = [i for i in 1:10]
nA = maximum(xdata) / 5
noise = rand(-nA:nA/5:nA, length(xdata))
ydata = xdata + noise

the results are abysmal, especially regarding the number of allocations made:

8.972615 seconds (20.30 M allocations: 1.104 GiB, 4.26% gc time, 2.74% compilation time)

This is with just 10 data points! The issue occurs on Ubuntu 20.04 and Julia version 1.6.1. The used back-end is GR.

@t-bltg t-bltg added the performance speedups and slowdowns label Aug 9, 2021
@BeastyBlacksmith
Copy link
Member

This example isn't actually self contained, so I can't run, but is the timing the same if you run the function again?

@mdkeehan
Copy link
Contributor

julia> draw_scatter_and_line( xdata, ydata, 1.0, 2.0, "scaddle", "lime", "xl", "yl", "bob.png")
  6.001535 seconds (16.79 M allocations: 941.686 MiB, 2.67% gc time, 45.99% compilation time)

julia> @time draw_scatter_and_line( xdata, ydata, 1.0, 2.0, "scaddle", "lime", "xl", "yl", "bob.png")
  0.014293 seconds (237.87 k allocations: 4.500 MiB)
  0.015777 seconds (243.70 k allocations: 4.830 MiB)

julia> @time draw_scatter_and_line( xdata, ydata, 1.0, 2.0, "scaddle", "lime", "xl", "yl", "bob.png")
  0.014303 seconds (237.87 k allocations: 4.500 MiB)
  0.015630 seconds (243.70 k allocations: 4.830 MiB)

julia> @time draw_scatter_and_line( xdata, ydata, 1.0, 2.0, "scattere", "line", "xl", "yl", "bob.png")
  0.015115 seconds (237.87 k allocations: 4.500 MiB)
  0.016502 seconds (243.70 k allocations: 4.831 MiB)

The first call is really slow and once it compiles it gets better...

@t-bltg
Copy link
Member

t-bltg commented Nov 27, 2022

This should be better now with SnoopPrecompile.
But overall, there might be a misunderstanding of the julia mechanism (jit compilation).

@t-bltg t-bltg closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance speedups and slowdowns
Projects
None yet
Development

No branches or pull requests

4 participants