Commits

Philip Jenvey committed 8f5beaa

preallocate the bytearraydata via length_hint

Comments (0)

Files changed (1)

pypy/objspace/std/bytearraytype.py

     str_splitlines, str_translate)
 from pypy.objspace.std.listtype import (
     list_append, list_extend)
+from rpython.rlib.objectmodel import newlist_hint, resizelist_hint
 
 
 bytearray_insert  = SMM('insert', 3,
         return [c for c in string]
 
     # sequence of bytes
-    data = []
     w_iter = space.iter(w_source)
+    length_hint = space.length_hint(w_source, 0)
+    data = newlist_hint(length_hint)
+    extended = 0
     while True:
         try:
             w_item = space.next(w_iter)
             break
         value = getbytevalue(space, w_item)
         data.append(value)
+        extended += 1
+    if extended < length_hint:
+        resizelist_hint(data, extended)
     return data
 
 def descr_bytearray__reduce__(space, w_self):