JuliaTeX / TikzPictures.jl

Creating PGF/TikZ pictures and saving them in various formats
Other
89 stars 28 forks source link

Provide multiple lazy-loaded backends for SVGs #57

Closed mirkobunse closed 4 years ago

mirkobunse commented 4 years ago

SVGs can now be produced with multiple backends:

The original code for the dvisvgm fallback was somehow broken; I fixed it when implementing this PR.

Providing different backends has the advantage of (potentially) faster loading times that do not loose compatibility across platforms. We have already discussed the slow loading times and potential solutions: https://github.com/JuliaTeX/PGFPlots.jl/issues/130

using TikzPictures now checks whether pdf2svg is installed. If so, it uses the corresponding backend and the package loads quickly. If pdf2svg is not installed, the package uses Poppler_jll instead, which results in a longer loading time. The dvisvgm backend is only used if explicitly set by the user. An open point of discussion is whether Poppler_jll should have precedence over dvisvgmin this sense.

using TikzPictures # loads a default backend

# set any of the backends explicitly
svgBackend(PdfToSvgBackend()) # default if pdf2svg is installed
svgBackend(PopplerBackend()) # default otherwise
svgBackend(DVIBackend()) # only used if set explicitly this way

Loading times

pdf2svg is already installed on my local machine, so loading TikzPictures.jl will be fast there. I can set the backend to poppler manually, afterwards, to see what loading Poppler_jll does. I tried this with julia v1.3 and v1.5, the last of which has improved loading *_jll packages a lot.

command v1.3 v1.5
@time using TikzPictures 0.4 seconds 0.3 seconds
@time svgBackend(PopplerBackend()) > 9 seconds 3.9 seconds

Testing

Travis CI is testing PR currently on julia v1.0 (yes, it works again), v1.3, and v1.5. The only platform, however, is Linux. Poppler_jll should work everywhere, but I have not yet figured out how to tell Travis to install texlive etc on Mac OS and Windows. Any improvement of the .travis.yml in this direction is warmly welcome!

mirkobunse commented 4 years ago

Update: I also checked @time using PGFPlots:

julia loading time without this PR and with this PR
v1.3 15.9 seconds 9.7 seconds
v1.5 10.4 seconds 8.9 seconds
mirkobunse commented 4 years ago

An alternative to this PR could be to set up a MinimalPoppler_jll which only offers pdftocairo and omits the other functionality that slows down loading Poppler_jll. Would make multiple backends completely unnecessary. Might be worth a discussion in the Yggdrasil repository.

mykelk commented 4 years ago

This looks great! Thank you for your contribution!

mirkobunse commented 4 years ago

Sure, you're welcome!

Still, I think extending the .travis.yml to test on Mac and Windows would be great. Do you know someone who can do this?

mykelk commented 4 years ago

I don't. I wanted to be able to do this for a while, but hadn't had luck. Fortunately, my PhD students use a mixture of Windows/Mac/Linux, and so they should be able to identify a potential issue---but it would be nice to have this part of travis.

mirkobunse commented 4 years ago

Beautiful, feel free to mark me in any such issue.

In the meantime, I found out that my other suggestion, to spin up a MinimalPoppler_jll, would likely not improve the loading time much. Fortunately, we can hope for a massive improvement in julia-1.6. See https://github.com/JuliaPackaging/Yggdrasil/pull/908#issuecomment-676492382

If Poppler_jll is fast in 1.6, we can easily change the default SVG backend accordingly.

mykelk commented 4 years ago

I'm so looking forward to 1.6. ;-)

PallHaraldsson commented 3 years ago

You mean this bit fast?

julia> @time using Poppler_jll
  0.000157 seconds (102 allocations: 11.688 KiB)

while, still not optimal (maybe you need to check on brand-new master and/or check/fix invalidations)
julia> @time using PGFPlots
  5.708749 seconds (7.53 M allocations: 504.851 MiB, 3.49% gc time, 21.08% compilation time)
mykelk commented 3 years ago

That looks pretty fast to me! ;-)