Skip to content

Precompile common regular expressions #1603

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 3 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Barry Warsaw
Bartolome Sanchez Salado
Benoit Pierre
Bernat Gabor
Brett Langdon
Bruno Oliveira
Carl Meyer
Charles Brunet
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/1603.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve config parsing performance by precompiling commonly used regular expressions - by :user:`brettlangdon`
13 changes: 9 additions & 4 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
INTERRUPT_TIMEOUT = 0.3
TERMINATE_TIMEOUT = 0.2

_FACTOR_LINE_PATTERN = re.compile(r"^([\w{}\.!,-]+)\:\s+(.+)")
_ENVSTR_SPLIT_PATTERN = re.compile(r"((?:\{[^}]+\})+)|,")
_ENVSTR_EXPAND_PATTERN = re.compile(r"\{([^}]+)\}")
_WHITESPACE_PATTERN = re.compile(r"\s+")


def get_plugin_manager(plugins=()):
# initialize plugin manager
Expand Down Expand Up @@ -1438,12 +1443,12 @@ def _split_factor_expr_all(expr):

def _expand_envstr(envstr):
# split by commas not in groups
tokens = re.split(r"((?:\{[^}]+\})+)|,", envstr)
tokens = _ENVSTR_SPLIT_PATTERN.split(envstr)
envlist = ["".join(g).strip() for k, g in itertools.groupby(tokens, key=bool) if k]

def expand(env):
tokens = re.split(r"\{([^}]+)\}", env)
parts = [re.sub(r"\s+", "", token).split(",") for token in tokens]
tokens = _ENVSTR_EXPAND_PATTERN.split(env)
parts = [_WHITESPACE_PATTERN.sub("", token).split(",") for token in tokens]
return ["".join(variant) for variant in itertools.product(*parts)]

return mapcat(expand, envlist)
Expand Down Expand Up @@ -1607,7 +1612,7 @@ def _replace_if_needed(self, x, name, replace, crossonly):

def _apply_factors(self, s):
def factor_line(line):
m = re.search(r"^([\w{}\.!,-]+)\:\s+(.+)", line)
m = _FACTOR_LINE_PATTERN.search(line)
if not m:
return line

Expand Down