8000 Merge pull request #9646 from anntzer/lru_cache · matplotlib/matplotlib@2ed9013 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ed9013

Browse files
authored
Merge pull request #9646 from anntzer/lru_cache
Convert dviread to use lru_cache.
2 parents 663e30f + 139a55a commit 2ed9013

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

lib/matplotlib/dviread.py

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
import textwrap
3838
import os
3939

40+
try:
41+
from functools import lru_cache
42+
except ImportError: # Py2
43+
from backports.functools_lru_cache import lru_cache
44+
4045
if six.PY3:
4146
def ord(x):
4247
return x
@@ -1056,36 +1061,19 @@ def find_tex_file(filename, format=None):
10561061
'debug')
10571062
return result.decode('ascii')
10581063

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

1063-
_tfmcache = {}
1064-
_vfcache = {}
1065-
1066-
1067-
def _fontfile(texname, class_, suffix, cache):
1068-
try:
1069-
return cache[texname]
1070-
except KeyError:
1071-
pass
1072-
1069+
@lru_cache()
1070+
def _fontfile(cls, suffix, texname):
10731071
filename = find_tex_file(texname + suffix)
1074-
if filename:
1075-
result = class_(filename)
1076-
else:
1077-
result = None
1078-
1079-
cache[texname] = result
1080-
return result
1081-
1082-
1083-
def _tfmfile(texname):
1084-
return _fontfile(texname, Tfm, '.tfm', _tfmcache)
1072+
return cls(filename) if filename else None
10851073

10861074

1087-
def _vffile(texname):
1088-
return _fontfile(texname, Vf, '.vf', _vfcache)
1075+
_tfmfile = partial(_fontfile, Tfm, ".tfm")
1076+
_vffile = partial(_fontfile, Vf, ".vf")
10891077

10901078

10911079
if __name__ == '__main__':

0 commit comments

Comments
 (0)
0