Skip to content

Commit 9f49997

Browse files
authored
Precompile common regular expressions (#1603)
1 parent e320b19 commit 9f49997

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Barry Warsaw
1616
Bartolome Sanchez Salado
1717
Benoit Pierre
1818
Bernat Gabor
19+
Brett Langdon
1920
Bruno Oliveira
2021
Carl Meyer
2122
Charles Brunet

docs/changelog/1603.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve config parsing performance by precompiling commonly used regular expressions - by :user:`brettlangdon`

src/tox/config/__init__.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
INTERRUPT_TIMEOUT = 0.3
5858
TERMINATE_TIMEOUT = 0.2
5959

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

6166
def get_plugin_manager(plugins=()):
6267
# initialize plugin manager
@@ -1438,12 +1443,12 @@ def _split_factor_expr_all(expr):
14381443

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

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

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

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

0 commit comments

Comments
 (0)