Skip to content

Convert dviread to use lru_cache. #9646

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

Merged
merged 1 commit into from
Nov 1, 2017
Merged
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
34 changes: 11 additions & 23 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
import textwrap
import os

try:
from functools import lru_cache
except ImportError: # Py2
from backports.functools_lru_cache import lru_cache

if six.PY3:
def ord(x):
return x
Expand Down Expand Up @@ -1056,36 +1061,19 @@ def find_tex_file(filename, format=None):
'debug')
return result.decode('ascii')


# With multiple text objects per figure (e.g., tick labels) we may end
# up reading the same tfm and vf files many times, so we implement a
# simple cache. TODO: is this worth making persistent?

_tfmcache = {}
_vfcache = {}


def _fontfile(texname, class_, suffix, cache):
try:
return cache[texname]
except KeyError:
pass

@lru_cache()
def _fontfile(cls, suffix, texname):
filename = find_tex_file(texname + suffix)
if filename:
result = class_(filename)
else:
result = None

cache[texname] = result
return result


def _tfmfile(texname):
return _fontfile(texname, Tfm, '.tfm', _tfmcache)
return cls(filename) if filename else None


def _vffile(texname):
return _fontfile(texname, Vf, '.vf', _vfcache)
_tfmfile = partial(_fontfile, Tfm, ".tfm")
_vffile = partial(_fontfile, Vf, ".vf")


if __name__ == '__main__':
Expand Down