helpers: use a small compilation cache to not re-compile same patterns over and over.

This commit is contained in:
RhodeCode Admin 2022-10-13 13:24:37 +02:00
parent dc3192df43
commit 747411a097

View file

@ -1666,22 +1666,26 @@ pr_pattern_re = regex.compile(r'(?:(?:^!)|(?: !))(\d+)')
allowed_link_formats = [
'html', 'rst', 'markdown', 'html+hovercard', 'rst+hovercard', 'markdown+hovercard']
compile_cache = {
}
def process_patterns(text_string, repo_name, link_format='html', active_entries=None):
if link_format not in allowed_link_formats:
raise ValueError('Link format can be only one of:{} got {}'.format(
allowed_link_formats, link_format))
issues_data = []
errors = []
new_text = text_string
if active_entries is None:
log.debug('Fetch active issue tracker patterns for repo: %s', repo_name)
active_entries = get_active_pattern_entries(repo_name)
issues_data = []
errors = []
new_text = text_string
log.debug('Got %s pattern entries to process', len(active_entries))
for uid, entry in active_entries.items():
if not (entry['pat'] and entry['url']):
@ -1693,6 +1697,8 @@ def process_patterns(text_string, repo_name, link_format='html', active_entries=
if entry.get('pat_compiled'):
pattern = entry['pat_compiled']
elif entry['pat'] in compile_cache:
pattern = compile_cache[entry['pat']]
else:
try:
pattern = regex.compile(r'%s' % entry['pat'])
@ -1701,6 +1707,7 @@ def process_patterns(text_string, repo_name, link_format='html', active_entries=
log.exception('issue tracker pattern: `%s` failed to compile', regex_err)
errors.append(regex_err)
continue
compile_cache[entry['pat']] = pattern
data_func = partial(
_process_url_func, repo_name=repo_name, entry=entry, uid=uid,