1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Pre-compile regexes to speed up target processing.

Without this, changing a large number of files results in target
processing taking a very long time due to repeatedly compiling
the same patterns in a loop over many targets.
This commit is contained in:
Matt Clay 2017-03-06 12:05:28 -08:00
parent 9c20182bdf
commit 3e4be156d7

View file

@ -137,6 +137,7 @@ def filter_targets(targets, patterns, include=True, directories=True, errors=Tru
:rtype: collections.Iterable[CompletionTarget] :rtype: collections.Iterable[CompletionTarget]
""" """
unmatched = set(patterns or ()) unmatched = set(patterns or ())
compiled_patterns = dict((p, re.compile('^%s$' % p)) for p in patterns) if patterns else None
for target in targets: for target in targets:
matched_directories = set() matched_directories = set()
@ -145,7 +146,7 @@ def filter_targets(targets, patterns, include=True, directories=True, errors=Tru
if patterns: if patterns:
for alias in target.aliases: for alias in target.aliases:
for pattern in patterns: for pattern in patterns:
if re.match('^%s$' % pattern, alias): if compiled_patterns[pattern].match(alias):
match = True match = True
try: try: