[pypy-commit] pypy py3.5-str-opt: start refactoring to not create a list whenever descr_new of a bytesobject is called.

plan_rich pypy.commits at gmail.com
Thu Oct 13 06:34:46 EDT 2016


Author: Richard Plangger <planrichi at gmail.com>
Branch: py3.5-str-opt
Changeset: r87750:d055ef6c7dce
Date: 2016-10-13 12:33 +0200
http://bitbucket.org/pypy/pypy/changeset/d055ef6c7dce/

Log:	start refactoring to not create a list whenever descr_new of a
	bytesobject is called.

diff --git a/pypy/objspace/std/bytearrayobject.py b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -187,7 +187,7 @@
     @unwrap_spec(encoding='str_or_None', errors='str_or_None')
     def descr_init(self, space, w_source=None, encoding=None, errors=None):
         assert isinstance(self, W_BytearrayObject)
-        data = newbytesdata_w(space, w_source, encoding, errors)
+        data = [c for c in newbytesdata_w(space, w_source, encoding, errors)]
         self.data = resizable_list_supporting_raw_ptr(data)
 
     def descr_repr(self, space):
diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -533,7 +533,7 @@
         if (w_source and space.is_w(space.type(w_source), space.w_bytes) and
             space.is_w(w_stringtype, space.w_bytes)):
             return w_source
-        value = ''.join(newbytesdata_w(space, w_source, encoding, errors))
+        value = newbytesdata_w(space, w_source, encoding, errors)
         w_obj = space.allocate_instance(W_BytesObject, w_stringtype)
         W_BytesObject.__init__(w_obj, value)
         return w_obj
@@ -699,7 +699,7 @@
         if encoding is not None or errors is not None:
             raise oefmt(space.w_TypeError,
                         "encoding or errors without string argument")
-        return []
+        return b""
     # Some object with __bytes__ special method
     w_bytes_method = space.lookup(w_source, "__bytes__")
     if w_bytes_method is not None:
@@ -707,7 +707,7 @@
         if not space.isinstance_w(w_bytes, space.w_bytes):
             raise oefmt(space.w_TypeError,
                         "__bytes__ returned non-bytes (type '%T')", w_bytes)
-        return [c for c in space.bytes_w(w_bytes)]
+        return space.bytes_w(w_bytes)
     # Is it an integer?
     # Note that we're calling space.getindex_w() instead of space.int_w().
     try:
@@ -721,7 +721,7 @@
         if encoding is not None or errors is not None:
             raise oefmt(space.w_TypeError,
                         "encoding or errors without string argument")
-        return ['\0'] * count
+        return '\0' * count
     # Unicode with encoding
     if space.isinstance_w(w_source, space.w_unicode):
         if encoding is None:
@@ -740,7 +740,7 @@
         if not space.isinstance_w(w_bytes, space.w_bytes):
             raise oefmt(space.w_TypeError,
                         "__bytes__ returned non-bytes (type '%T')", w_bytes)
-        return [c for c in space.bytes_w(w_bytes)]
+        return space.bytes_w(w_bytes)
     return _convert_from_buffer_or_iterable(space, w_source)
 
 def _convert_from_buffer_or_iterable(space, w_source):
@@ -751,7 +751,7 @@
         if not e.match(space, space.w_TypeError):
             raise
     else:
-        return [c for c in buf.as_str()]
+        return buf.as_str()
 
     if space.isinstance_w(w_source, space.w_unicode):
         raise oefmt(space.w_TypeError,
@@ -760,8 +760,7 @@
     # sequence of bytes
     w_iter = space.iter(w_source)
     length_hint = space.length_hint(w_source, 0)
-    data = newlist_hint(length_hint)
-    extended = 0
+    builder = StringBuilder(length_hint)
     while True:
         try:
             w_item = space.next(w_iter)
@@ -770,11 +769,8 @@
                 raise
             break
         value = getbytevalue(space, w_item)
-        data.append(value)
-        extended += 1
-    if extended < length_hint:
-        resizelist_hint(data, extended)
-    return data
+        builder.append(value)
+    return builder.build()
 
 
 W_BytesObject.typedef = TypeDef(


More information about the pypy-commit mailing list