From f7b8db1312ac3863f2f946908fda7f7fada332d0 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Sat, 14 Sep 2013 10:04:31 +0200 Subject: [PATCH] reduce overhead a bit when many string constants have the same cname --- Cython/Compiler/Code.py | 9 ++++----- tests/run/strliterals.pyx | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Cython/Compiler/Code.py b/Cython/Compiler/Code.py index 14cf22d..1f51f1f 100644 --- a/Cython/Compiler/Code.py +++ b/Cython/Compiler/Code.py @@ -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): diff --git a/tests/run/strliterals.pyx b/tests/run/strliterals.pyx index cbf760b..517a45a 100644 --- a/tests/run/strliterals.pyx +++ b/tests/run/strliterals.pyx @@ -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" -- 2.7.4