[pypy-commit] pypy default: make sure that ByteBuffer.getslice is optimized

cfbolz pypy.commits at gmail.com
Wed Sep 11 09:11:25 EDT 2019


Author: Carl Friedrich Bolz-Tereick <cfbolz at gmx.de>
Branch: 
Changeset: r97440:5a6cb24984c1
Date: 2019-09-11 15:01 +0200
http://bitbucket.org/pypy/pypy/changeset/5a6cb24984c1/

Log:	make sure that ByteBuffer.getslice is optimized

diff --git a/rpython/rlib/buffer.py b/rpython/rlib/buffer.py
--- a/rpython/rlib/buffer.py
+++ b/rpython/rlib/buffer.py
@@ -123,7 +123,7 @@
 
 class RawBuffer(Buffer):
     """
-    A buffer which is baked by a raw, non-movable memory area. It implementes
+    A buffer which is backed by a raw, non-movable memory area. It implementes
     typed_read and typed_write in terms of get_raw_address(), llop.raw_load,
     llop.raw_store.
 
@@ -157,7 +157,7 @@
 
 class GCBuffer(Buffer):
     """
-    Base class for a buffer which is baked by a GC-managed memory area. You
+    Base class for a buffer which is backed by a GC-managed memory area. You
     MUST also decorate the class with @GCBuffer.decorate: it implements
     typed_read and typed_write in terms of llop.gc_load_indexed and
     llop.gc_store_indexed.
@@ -250,6 +250,14 @@
     def setitem(self, index, char):
         self.data[index] = char
 
+    def getslice(self, start, stop, step, size):
+        if step == 1:
+            assert 0 <= start <= stop
+            if start == 0 and stop == len(self.data):
+                return "".join(self.data)
+            return "".join(self.data[start:stop])
+        return Buffer.getslice(self, start, stop, step, size)
+
     def get_raw_address(self):
         return nonmoving_raw_ptr_for_resizable_list(self.data)
 
diff --git a/rpython/rlib/test/test_buffer.py b/rpython/rlib/test/test_buffer.py
--- a/rpython/rlib/test/test_buffer.py
+++ b/rpython/rlib/test/test_buffer.py
@@ -197,6 +197,12 @@
         assert buf.typed_read(rffi.USHORT, 0) == 0x1234
         assert buf.typed_read(rffi.USHORT, 2) == 0x5678
 
+    def test_getslice_shortcut(self):
+        buf = ByteBuffer(4)
+        buf.setslice(0, b"data")
+        buf.getitem = None
+        assert buf.getslice(0, 2, 1, 2) == b"da" # no crash!
+
 
 class TestJIT(LLJitMixin):
 


More information about the pypy-commit mailing list