Commits

Alex Gaynor committed ee04c61

attempt to presize tehse lists

  • Participants
  • Parent commits 77dc5a5

Comments (0)

Files changed (1)

File rpython/rlib/rstring.py

 
 from rpython.annotator.model import (SomeObject, SomeString, s_None, SomeChar,
     SomeInteger, SomeUnicodeCodePoint, SomeUnicodeString, SomePtr, SomePBC)
+from rpython.rlib.objectmodel import newlist_hint
 from rpython.rlib.rarithmetic import ovfcheck
-from rpython.tool.pairtype import pair, pairtype
 from rpython.rtyper.extregistry import ExtRegistryEntry
+from rpython.tool.pairtype import pairtype
 
 
 # -------------- public API for string functions -----------------------
     if bylen == 0:
         raise ValueError("empty separator")
 
-    res = []
+    if maxsplit > 0:
+        res = newlist_hint(maxsplit)
+    else:
+        res = []
     start = 0
     while maxsplit != 0:
         next = value.find(by, start)
     res.append(value[start:len(value)])
     return res
 
+
 def rsplit(value, by, maxsplit=-1):
-    res = []
+    if maxsplit > 0:
+        res = newlist_hint(maxsplit)
+    else:
+        res = []
     end = len(value)
     bylen = len(by)
     if bylen == 0:
         next = value.rfind(by, 0, end)
         if next < 0:
             break
-        res.append(value[next+bylen:end])
+        res.append(value[next + bylen:end])
         end = next
         maxsplit -= 1   # NB. if it's already < 0, it stays < 0
 
 
 INIT_SIZE = 100 # XXX tweak
 
+
 class AbstractStringBuilder(object):
     def __init__(self, init_size=INIT_SIZE):
         self.l = []
     def getlength(self):
         return len(self.build())
 
+
 class StringBuilder(AbstractStringBuilder):
     tp = str
 
+
 class UnicodeBuilder(AbstractStringBuilder):
     tp = unicode
 
 
     def specialize_call(self, hop):
         hop.exception_cannot_occur()
-