Skip to content

Memoize parse_fontconfig_pattern; speeds up test suite by ~1min. #8252

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
Mar 11, 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
23 changes: 18 additions & 5 deletions lib/matplotlib/fontconfig_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@

import six

import re, sys
from pyparsing import Literal, ZeroOrMore, \
Optional, Regex, StringEnd, ParseException, Suppress
import re
import sys
from pyparsing import (Literal, ZeroOrMore, Optional, Regex, StringEnd,
ParseException, Suppress)

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

family_punc = r'\\\-:,'
family_unescape = re.compile(r'\\([%s])' % family_punc).sub
Expand Down Expand Up @@ -166,7 +172,13 @@ def _property(self, s, loc, tokens):
self._properties.setdefault(key, []).extend(val)
return []

parse_fontconfig_pattern = FontconfigPatternParser().parse

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


def generate_fontconfig_pattern(d):
"""
Expand All @@ -180,7 +192,8 @@ def generate_fontconfig_pattern(d):
val = getattr(d, 'get_' + key)()
if val is not None and val != []:
if type(val) == list:
val = [value_escape(r'\\\1', str(x)) for x in val if x is not None]
val = [value_escape(r'\\\1', str(x)) for x in val
if x is not None]
if val != []:
val = ','.join(val)
props.append(":%s=%s" % (key, val))
Expand Down