@@ -229,6 +229,7 @@ def compile(pattern, flags=0):
229229def purge ():
230230 "Clear the regular expression caches"
231231 _cache .clear ()
232+ _cache2 .clear ()
232233 _compile_repl .cache_clear ()
233234
234235def template (pattern , flags = 0 ):
@@ -267,39 +268,55 @@ def escape(pattern):
267268# internals
268269
269270_cache = {} # ordered!
271+ _cache2 = {} # ordered!
270272
271273_MAXCACHE = 512
274+ _MAXCACHE2 = 256
272275def _compile (pattern , flags ):
273276 # internal: compile pattern
274277 if isinstance (flags , RegexFlag ):
275278 flags = flags .value
276279 try :
277- return _cache [type (pattern ), pattern , flags ]
280+ return _cache2 [type (pattern ), pattern , flags ]
278281 except KeyError :
279282 pass
280- if isinstance (pattern , Pattern ):
281- if flags :
282- raise ValueError (
283- "cannot process flags argument with a compiled pattern" )
284- return pattern
285- if not _compiler .isstring (pattern ):
286- raise TypeError ("first argument must be string or compiled pattern" )
287- if flags & T :
288- import warnings
289- warnings .warn ("The re.TEMPLATE/re.T flag is deprecated "
290- "as it is an undocumented flag "
291- "without an obvious purpose. "
292- "Don't use it." ,
293- DeprecationWarning )
294- p = _compiler .compile (pattern , flags )
295- if not (flags & DEBUG ):
283+
284+ key = (type (pattern ), pattern , flags )
285+ p = _cache .pop (key , None )
286+ if p is None :
287+ if isinstance (pattern , Pattern ):
288+ if flags :
289+ raise ValueError (
290+ "cannot process flags argument with a compiled pattern" )
291+ return pattern
292+ if not _compiler .isstring (pattern ):
293+ raise TypeError ("first argument must be string or compiled pattern" )
294+ if flags & T :
295+ import warnings
296+ warnings .warn ("The re.TEMPLATE/re.T flag is deprecated "
297+ "as it is an undocumented flag "
298+ "without an obvious purpose. "
299+ "Don't use it." ,
300+ DeprecationWarning )
301+ p = _compiler .compile (pattern , flags )
302+ if flags & DEBUG :
303+ return p
296304 if len (_cache ) >= _MAXCACHE :
297- # Drop the oldest item
305+ # Drop the least used item
298306 try :
299307 del _cache [next (iter (_cache ))]
300308 except (StopIteration , RuntimeError , KeyError ):
301309 pass
302- _cache [type (pattern ), pattern , flags ] = p
310+ # Append to the end
311+ _cache [key ] = p
312+
313+ if len (_cache2 ) >= _MAXCACHE2 :
314+ # Drop the oldest item
315+ try :
316+ del _cache2 [next (iter (_cache2 ))]
317+ except (StopIteration , RuntimeError , KeyError ):
318+ pass
319+ _cache2 [key ] = p
303320 return p
304321
305322@functools .lru_cache (_MAXCACHE )
0 commit comments