Bug 1134800 - Properly cache file matching patterns; r=glandium

The regular expression cache for mozpack.path.match was keyed off the
original pattern. However, that variable was mutated as part of the
function and the mutated result was subsequently stored as the cache
key. This effectively resulted in a 0% cache hit rate.

On some tests being written for bug 1132111 which involve a full
filesystem traversal for moz.build files and subsequent execution of
those files, the following timings are indicative of the impact of this
patch.

Before:
real 16.082s
user 14.760s
sys   1.318s

After:
real 6.345s
user 5.085s
sys  1.257s
This commit is contained in:
Gregory Szorc 2015-02-24 10:19:41 -08:00
parent 0dcaf1fc16
commit 193771a104

View File

@ -99,12 +99,12 @@ def match(path, pattern):
'''
if not pattern:
return True
if not pattern in re_cache:
pattern = re.escape(pattern)
pattern = re.sub(r'(^|\\\/)\\\*\\\*\\\/', r'\1(?:.+/)?', pattern)
pattern = re.sub(r'(^|\\\/)\\\*\\\*$', r'(?:\1.+)?', pattern)
pattern = pattern.replace(r'\*', '[^/]*') + '(?:/.*)?$'
re_cache[pattern] = re.compile(pattern)
if pattern not in re_cache:
p = re.escape(pattern)
p = re.sub(r'(^|\\\/)\\\*\\\*\\\/', r'\1(?:.+/)?', p)
p = re.sub(r'(^|\\\/)\\\*\\\*$', r'(?:\1.+)?', p)
p = p.replace(r'\*', '[^/]*') + '(?:/.*)?$'
re_cache[pattern] = re.compile(p)
return re_cache[pattern].match(path) is not None