[pypy-commit] pypy default: Optimize mmap slicing with step != 1

alex_gaynor noreply at buildbot.pypy.org
Sat Nov 15 01:43:50 CET 2014


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: 
Changeset: r74528:9e48410d1e31
Date: 2014-11-14 16:43 -0800
http://bitbucket.org/pypy/pypy/changeset/9e48410d1e31/

Log:	Optimize mmap slicing with step != 1

diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -5,6 +5,7 @@
 from rpython.rlib import rmmap, rarithmetic
 from rpython.rlib.buffer import Buffer
 from rpython.rlib.rmmap import RValueError, RTypeError, RMMapError
+from rpython.rlib.rstring import StringBuilder
 
 if rmmap.HAVE_LARGEFILE_SUPPORT:
     OFF_T = rarithmetic.r_longlong
@@ -163,17 +164,18 @@
         self.check_valid()
 
         space = self.space
-        start, stop, step = space.decode_index(w_index, self.mmap.size)
+        start, stop, step, length = space.decode_index4(w_index, self.mmap.size)
         if step == 0:  # index only
             return space.wrap(self.mmap.getitem(start))
         elif step == 1:
             if stop - start < 0:
                 return space.wrap("")
-            return space.wrap(self.mmap.getslice(start, stop - start))
+            return space.wrap(self.mmap.getslice(start, length))
         else:
-            res = "".join([self.mmap.getitem(i)
-                           for i in range(start, stop, step)])
-            return space.wrap(res)
+            b = StringBuilder(length)
+            for i in range(start, stop, step):
+                b.append(self.mmap.getitem(i))
+            return space.wrap(b.build())
 
     def descr_setitem(self, w_index, w_value):
         space = self.space


More information about the pypy-commit mailing list