[tor-commits] [stem/master] stem/descriptor/extrainfo_descriptor.py: Compile regexps outside of loops.

atagar at torproject.org atagar at torproject.org
Sun Nov 23 00:13:20 UTC 2014


commit 4669c89a4b3b36712dbf879287312dd4221b18da
Author: Ossi Herrala <oherrala at gmail.com>
Date:   Wed Nov 19 20:58:16 2014 +0200

    stem/descriptor/extrainfo_descriptor.py: Compile regexps outside of loops.
    
    With 24MB cached-extrainfo file this reduces function calls from
    7457706 down to 6003241.
    
    before (lines sorted):
             7457706 function calls (7450757 primitive calls) in 14.153 seconds
             7457706 function calls (7450757 primitive calls) in 14.163 seconds
             7457706 function calls (7450757 primitive calls) in 14.274 seconds
             7457706 function calls (7450757 primitive calls) in 14.293 seconds
             7457706 function calls (7450757 primitive calls) in 14.311 seconds
             7457706 function calls (7450757 primitive calls) in 14.356 seconds
             7457706 function calls (7450757 primitive calls) in 14.418 seconds
             7457706 function calls (7450757 primitive calls) in 14.487 seconds
             7457706 function calls (7450757 primitive calls) in 14.517 seconds
             7457706 function calls (7450757 primitive calls) in 14.682 seconds
    
    after (lines sorted):
             6003241 function calls (5996319 primitive calls) in 12.607 seconds
             6003241 function calls (5996319 primitive calls) in 12.619 seconds
             6003241 function calls (5996319 primitive calls) in 12.717 seconds
             6003241 function calls (5996319 primitive calls) in 12.718 seconds
             6003241 function calls (5996319 primitive calls) in 12.727 seconds
             6003241 function calls (5996319 primitive calls) in 12.834 seconds
             6003241 function calls (5996319 primitive calls) in 12.840 seconds
             6003241 function calls (5996319 primitive calls) in 12.882 seconds
             6003241 function calls (5996319 primitive calls) in 13.027 seconds
             6003241 function calls (5996319 primitive calls) in 13.277 seconds
---
 stem/descriptor/extrainfo_descriptor.py |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/stem/descriptor/extrainfo_descriptor.py b/stem/descriptor/extrainfo_descriptor.py
index 0aa7724..31f0e20 100644
--- a/stem/descriptor/extrainfo_descriptor.py
+++ b/stem/descriptor/extrainfo_descriptor.py
@@ -151,6 +151,10 @@ SINGLE_FIELDS = (
 )
 
 
+_timestamp_re = re.compile('^(.*) \(([0-9]+) s\)( .*)?$')
+_locale_re = re.compile('^[a-zA-Z0-9\?]{2}$')
+
+
 def _parse_file(descriptor_file, is_bridge = False, validate = True, **kwargs):
   """
   Iterates over the extra-info descriptors in a file.
@@ -202,7 +206,7 @@ def _parse_timestamp_and_interval(keyword, content):
   """
 
   line = '%s %s' % (keyword, content)
-  content_match = re.match('^(.*) \(([0-9]+) s\)( .*)?$', content)
+  content_match = _timestamp_re.match(content)
 
   if not content_match:
     raise ValueError('Malformed %s line: %s' % (keyword, line))
@@ -783,7 +787,7 @@ class ExtraInfoDescriptor(Descriptor):
 
             locale, count = entry.split('=', 1)
 
-            if re.match('^[a-zA-Z0-9\?]{2}$', locale) and count.isdigit():
+            if _locale_re.match(locale) and count.isdigit():
               locale_usage[locale] = int(count)
             elif validate:
               raise ValueError(error_msg)





More information about the tor-commits mailing list