mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	bpo-31671: re: Convert RegexFlag to int before compile (GH-3862)
sre_compile does bit test (e.g. `flags & SRE_FLAG_IGNORECASE`) in loop. `IntFlag.__and__` and `IntFlag.__new__` made it slower. So this commit convert it to normal int before passing flags to `sre_compile()`.
This commit is contained in:
		
							parent
							
								
									af810b35b4
								
							
						
					
					
						commit
						c1c47c166b
					
				
					 3 changed files with 11 additions and 0 deletions
				
			
		|  | @ -326,6 +326,11 @@ Optimizations | ||||||
|   expressions <re>`.  Searching some patterns can now be up to 20 times faster. |   expressions <re>`.  Searching some patterns can now be up to 20 times faster. | ||||||
|   (Contributed by Serhiy Storchaka in :issue:`30285`.) |   (Contributed by Serhiy Storchaka in :issue:`30285`.) | ||||||
| 
 | 
 | ||||||
|  | * :func:`re.compile` now converts ``flags`` parameter to int object if | ||||||
|  |   it is ``RegexFlag``.  It is now as fast as Python 3.5, and faster than | ||||||
|  |   Python 3.6 about 10% depending on the pattern. | ||||||
|  |   (Contributed by INADA Naoki in :issue:`31671`.) | ||||||
|  | 
 | ||||||
| * :meth:`selectors.EpollSelector.modify`, :meth:`selectors.PollSelector.modify` | * :meth:`selectors.EpollSelector.modify`, :meth:`selectors.PollSelector.modify` | ||||||
|   and :meth:`selectors.DevpollSelector.modify` may be around 10% faster under |   and :meth:`selectors.DevpollSelector.modify` may be around 10% faster under | ||||||
|   heavy loads. (Contributed by Giampaolo Rodola' in :issue:`30014`) |   heavy loads. (Contributed by Giampaolo Rodola' in :issue:`30014`) | ||||||
|  |  | ||||||
|  | @ -275,6 +275,8 @@ def escape(pattern): | ||||||
| _MAXCACHE = 512 | _MAXCACHE = 512 | ||||||
| def _compile(pattern, flags): | def _compile(pattern, flags): | ||||||
|     # internal: compile pattern |     # internal: compile pattern | ||||||
|  |     if isinstance(flags, RegexFlag): | ||||||
|  |         flags = flags.value | ||||||
|     try: |     try: | ||||||
|         return _cache[type(pattern), pattern, flags] |         return _cache[type(pattern), pattern, flags] | ||||||
|     except KeyError: |     except KeyError: | ||||||
|  | @ -331,6 +333,8 @@ def _pickle(p): | ||||||
| class Scanner: | class Scanner: | ||||||
|     def __init__(self, lexicon, flags=0): |     def __init__(self, lexicon, flags=0): | ||||||
|         from sre_constants import BRANCH, SUBPATTERN |         from sre_constants import BRANCH, SUBPATTERN | ||||||
|  |         if isinstance(flags, RegexFlag): | ||||||
|  |             flags = flags.value | ||||||
|         self.lexicon = lexicon |         self.lexicon = lexicon | ||||||
|         # combine phrases into a compound pattern |         # combine phrases into a compound pattern | ||||||
|         p = [] |         p = [] | ||||||
|  |  | ||||||
|  | @ -0,0 +1,2 @@ | ||||||
|  | Now ``re.compile()`` converts passed RegexFlag to normal int object before | ||||||
|  | compiling. bm_regex_compile benchmark shows 14% performance improvements. | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 INADA Naoki
						INADA Naoki