##// END OF EJS Templates
match: avoid translating glob to matcher multiple times for large sets...
Boris Feld -
r40813:3c842749 default
parent child Browse files
Show More
@@ -1185,6 +1185,7 b' def _buildmatch(kindpats, globsuffix, li'
1185 return regex, lambda f: any(mf(f) for mf in matchfuncs)
1185 return regex, lambda f: any(mf(f) for mf in matchfuncs)
1186
1186
1187 MAX_RE_SIZE = 20000
1187 MAX_RE_SIZE = 20000
1188 _BASE_SIZE = len('(?:)') - 1
1188
1189
1189 def _joinregexes(regexps):
1190 def _joinregexes(regexps):
1190 """gather multiple regular expressions into a single one"""
1191 """gather multiple regular expressions into a single one"""
@@ -1203,20 +1204,31 b' def _buildregexmatch(kindpats, globsuffi'
1203 OverflowError
1204 OverflowError
1204 """
1205 """
1205 try:
1206 try:
1206 regex = _joinregexes([_regex(k, p, globsuffix)
1207 allgroups = []
1207 for (k, p, s) in kindpats])
1208 regexps = [_regex(k, p, globsuffix) for (k, p, s) in kindpats]
1208 if len(regex) <= MAX_RE_SIZE:
1209 fullregexp = _joinregexes(regexps)
1209 return regex, _rematcher(regex)
1210
1210 # We're using a Python with a tiny regex engine and we
1211 startidx = 0
1211 # made it explode, so we'll divide the pattern list in two
1212 groupsize = _BASE_SIZE
1212 # until it works
1213 for idx, r in enumerate(regexps):
1213 l = len(kindpats)
1214 piecesize = len(r)
1214 if l < 2:
1215 if (piecesize + 4) > MAX_RE_SIZE:
1215 # TODO: raise error.Abort here
1216 raise OverflowError
1216 raise OverflowError
1217 elif (groupsize + 1 + piecesize) > MAX_RE_SIZE:
1217 regexa, a = _buildregexmatch(kindpats[:l//2], globsuffix)
1218 group = regexps[startidx:idx]
1218 regexb, b = _buildregexmatch(kindpats[l//2:], globsuffix)
1219 allgroups.append(_joinregexes(group))
1219 return regex, lambda s: a(s) or b(s)
1220 startidx = idx
1221 groupsize = _BASE_SIZE
1222 groupsize += piecesize + 1
1223
1224 if startidx == 0:
1225 func = _rematcher(fullregexp)
1226 else:
1227 group = regexps[startidx:]
1228 allgroups.append(_joinregexes(group))
1229 allmatchers = [_rematcher(g) for g in allgroups]
1230 func = lambda s: any(m(s) for m in allmatchers)
1231 return fullregexp, func
1220 except re.error:
1232 except re.error:
1221 for k, p, s in kindpats:
1233 for k, p, s in kindpats:
1222 try:
1234 try:
General Comments 0
You need to be logged in to leave comments. Login now