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

SVG savefig + LaTeX extremely slow on macOS #9653

Closed
tdegeus opened this issue Nov 1, 2017 · 13 comments
Closed

SVG savefig + LaTeX extremely slow on macOS #9653

tdegeus opened this issue Nov 1, 2017 · 13 comments
Labels
backend: svg Difficulty: Hard https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature OS: Apple Performance topic: text/usetex
Milestone

Comments

@tdegeus
Copy link

tdegeus commented Nov 1, 2017

When I saving a SVG figure is slowed down significantly (10-100x) when I use LaTeX, enabled by the following rc-setting:

text.usetex : true

Note that rendering is somewhat slower, but not much. It is really the saving. I am on macOS 10.12.6 with matplotlib (2.1.0).

@dstansby
Copy link
Member

dstansby commented Nov 1, 2017

Do you have a minimal example of code that when run causes the slowdown?

@jklymak jklymak added the status: needs clarification Issues that need more information to resolve. label Nov 7, 2017
@tdegeus
Copy link
Author

tdegeus commented Nov 14, 2017

I have created two extremely simple examples, one without LaTeX and one with LaTeX (see below). The result:

$ time python3 without_latex.py
python3 without_latex.py  0.47s user 0.08s system 96% cpu 0.568 total
$ time python3 with_latex.py
python3 with_latex.py  6.40s user 0.62s system 96% cpu 7.251 total

The use of LaTeX makes saving the figure ~13x slower.

Rendening is a bit more tricky to time. What I have done is simply suppressed the saving (i.e. nothing is shown neither). The result:

$ time python3 without_latex.py
python3 without_latex.py  0.39s user 0.07s system 96% cpu 0.479 total
$ time python3 with_latex.py
python3 with_latex.py  0.38s user 0.07s system 96% cpu 0.470 total

The scripts:

  • without_latex.py

    import matplotlib.pyplot as plt
    
    plt.figure()
    plt.plot([0,1],[0,1])
    plt.savefig('without_latex.svg')
  • with_latex.py

    import matplotlib.pyplot as plt
    import matplotlib        as mpl
    
    mpl.rcParams['text.usetex'] = True
    
    plt.figure()
    plt.plot([0,1],[0,1])
    plt.savefig('with_latex.svg')

@tdegeus
Copy link
Author

tdegeus commented Dec 19, 2017

@jklymak Can you please verify the current description, and possibly lift the "needs clarification" label?

@jklymak jklymak removed the status: needs clarification Issues that need more information to resolve. label Dec 19, 2017
@jklymak
Copy link
Member

jklymak commented Dec 19, 2017

I get about 3x slower.

Not sure what the issue is. It is a bit slower to plt.show() for me with latex, but not that much. File sizes are the same, so its not like a font file is being dumped in there.

@tacaswell
Copy link
Member

Using latex means we may be making (many) subprocess calls to latex which is my knee-jerk guess as to the source of the slow down. We should be caching the results in ~/.cache/matplotlib/tex.cache so repeated runs should get faster. Are you seeing files accumulating there?

Do you see the slow down with other files types (png, pdf) or just svg?

@jklymak
Copy link
Member

jklymak commented Dec 19, 2017

With Master on a mac.

The cache directory is not being written to on my machine.

eps, png, pdf take about 2.8s.
svg takes 7.0s

w/ usetex False they all take about 1s.

I assume that the cache is not being written, and maybe svg is calling the subprocess twice for some reason. ping @anntzer because I know he has/had a PR in to homogenize the backends. Maybe that PR fixes the extra calls. Not sure about the caching issue.

@jklymak
Copy link
Member

jklymak commented Dec 19, 2017

OK. There appears to be a permissions issue on macOSX - the cache should be at ~/.matplotlib/tex.cache. But there is a test in _get_config_or_cache_dir that tests if ~/.matplotlib is writable. Which its not, I assume because someone decided to create the directory that way (not me). So the cache was being written in /var/tmp in a new directory each time.

New timing is 5.2s for the second call to writing an svg. 1.8s for a pdf.

So the cache saves 2s for the svg, 1s for the PDF.

So, two problems:

  1. ~/.matplotlibrc directory should be created with the user writebit enabled (or the check in _get_config_or_cache_dir should be changed)
  2. SVG still seems quite slow and probably makes excess calls to TeX.

EDIT: Maybe an old version setup the ~/.matplotlib directory incorrectly. I moved the old one and created a new one and it permissions are fine

@jklymak
Copy link
Member

jklymak commented Dec 19, 2017

A quick look at the backend code: It looks like svg draws tex characters as paths. It seems like the PDF gets the font info and places the characters. Obviously drawing the paths will be a lot slower than just placing the characters. Someone would have to spend some time on this I think.

@jklymak jklymak added Difficulty: Hard https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature labels Dec 19, 2017
@tacaswell tacaswell added this to the v3.0 milestone Dec 19, 2017
@anntzer
Copy link
Contributor

anntzer commented Dec 19, 2017

You can do rcParams["svg.fonttype"] = "none" to include text as text in SVG files.
Other than that, I don't think I have anything specific to help this case right now.

@tdegeus
Copy link
Author

tdegeus commented Jan 5, 2018

Possible duplicate: #4880

@tdegeus
Copy link
Author

tdegeus commented Mar 28, 2018

(sorry, clicked close instead of cancel)

@dstansby dstansby modified the milestones: v3.0, needs sorting Jul 26, 2018
@anntzer
Copy link
Contributor

anntzer commented Feb 22, 2021

I think this will be closed by #19558 (or one of its variants), which appears to make svg+usetex essentially as fast as pdf+usetex (and much faster than either previous +usetex versions).

@anntzer
Copy link
Contributor

anntzer commented May 26, 2021

Mostly closed by #19538 and #19558. At least another possibility remaining for speeding up usetex is discussed at #4880 (comment), but that can be tracked by #4880.

@anntzer anntzer closed this as completed May 26, 2021
@anntzer anntzer modified the milestones: needs sorting, v3.5.0 May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: svg Difficulty: Hard https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues New feature OS: Apple Performance topic: text/usetex
Projects
None yet
Development

No branches or pull requests

6 participants