Skip to content

Commit 263ac2a

Browse files
committed
Memoize parse_fontconfig_pattern; speeds up test suite by ~1min.
On my laptop this patch drops the duration of the test suite from 604s to 554s (with the inkscape patch on as well).
1 parent 8a77cfb commit 263ac2a

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

lib/matplotlib/fontconfig_pattern.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@
2121

2222
import six
2323

24-
import re, sys
25-
from pyparsing import Literal, ZeroOrMore, \
26-
Optional, Regex, StringEnd, ParseException, Suppress
24+
import re
25+
import sys
26+
from pyparsing import (Literal, ZeroOrMore, Optional, Regex, StringEnd,
27+
ParseException, Suppress)
28+
29+
try:
30+
from functools import lru_cache
31+
except ImportError:
32+
from backports.functools_lru_cache import lru_cache
2733

2834
family_punc = r'\\\-:,'
2935
family_unescape = re.compile(r'\\([%s])' % family_punc).sub
@@ -166,7 +172,11 @@ def _property(self, s, loc, tokens):
166172
self._properties.setdefault(key, []).extend(val)
167173
return []
168174

169-
parse_fontconfig_pattern = FontconfigPatternParser().parse
175+
# `parse_fontconfig_pattern` is a bottleneck during the tests because it is
176+
# repeatedly called when the rcParams are reset (to validate the default
177+
# fonts). In practice, the cache size doesn't grow beyond a few dozen entries
178+
# during the test suite.
179+
parse_fontconfig_pattern = lru_cache()(FontconfigPatternParser().parse)
170180

171181
def generate_fontconfig_pattern(d):
172182
"""

0 commit comments

Comments
 (0)