reduce overhead a bit when many string constants have the same cname
authorStefan Behnel <stefan_ml@behnel.de>
Sat, 14 Sep 2013 08:04:31 +0000 (10:04 +0200)
committerStefan Behnel <stefan_ml@behnel.de>
Sat, 14 Sep 2013 08:04:31 +0000 (10:04 +0200)
Cython/Compiler/Code.py
tests/run/strliterals.pyx

index 14cf22d..1f51f1f 100644 (file)
@@ -847,7 +847,7 @@ class GlobalState(object):
     #                                  In time, hopefully the literals etc. will be
     #                                  supplied directly instead.
     #
-    # const_cnames_used  set           global index of unique constant identifiers
+    # const_cnames_used  dict          global counter for unique constant identifiers
     #
 
     # parts            {string:CCodeWriter}
@@ -903,7 +903,7 @@ class GlobalState(object):
         self.module_node = module_node # because some utility code generation needs it
                                        # (generating backwards-compatible Get/ReleaseBuffer
 
-        self.const_cnames_used = set()
+        self.const_cnames_used = {}
         self.string_const_index = {}
         self.pyunicode_ptr_const_index = {}
         self.int_const_index = {}
@@ -1108,12 +1108,11 @@ class GlobalState(object):
     def new_const_cname(self, prefix='', value=''):
         value = replace_identifier('_', value)[:32].strip('_')
         used = self.const_cnames_used
-        counter = 1
         name_suffix = value
         while name_suffix in used:
-            counter += 1
+            counter = used[value] = used[value] + 1
             name_suffix = '%s_%d' % (value, counter)
-        used.add(name_suffix)
+        used[name_suffix] = 1
         return "%s%s%s" % (Naming.const_prefix, prefix, name_suffix)
 
     def add_cached_builtin_decl(self, entry):
index cbf760b..517a45a 100644 (file)
@@ -149,7 +149,7 @@ __doc__ = ur"""
     True
 
     >>> same_cname
-    [b'abc\xf0', b'abc\xf1', b'abc\xf2', b'abc\xf3', b'abc_2', b'abc_3']
+    [b'abc\xf0_2', b'abc\xf0', b'abc\xf1', b'abc\xf2', b'abc\xf3', b'abc_2', b'abc_3']
 
     >>> newlines
     'Aaa\n'
@@ -194,7 +194,7 @@ uresc = ur'\12\'\"\\'
 bytes_uescape = b'\u1234\U12345678\u\u1\u12\uX'
 str_uescape = '\u0063\U00012345\N{SNOWMAN}\x42'
 
-same_cname = [b'abc\xf0', b'abc\xf1', b'abc\xf2', b'abc\xf3', b'abc_2', b'abc_3']
+same_cname = [b'abc\xf0_2', b'abc\xf0', b'abc\xf1', b'abc\xf2', b'abc\xf3', b'abc_2', b'abc_3']
 
 newlines = "Aaa\n"