Author: Justin Peel <[email protected]>
Branch:
Changeset: r58349:aa1ab56ff708
Date: 2012-10-21 19:27 -0600
http://bitbucket.org/pypy/pypy/changeset/aa1ab56ff708/
Log: speed up deleting a simple slice (step=1) of an array.array by a
generous order of magnitude. Also add more tests for deleting slices
of an array.array.
diff --git a/pypy/module/array/interp_array.py
b/pypy/module/array/interp_array.py
--- a/pypy/module/array/interp_array.py
+++ b/pypy/module/array/interp_array.py
@@ -467,7 +467,35 @@
self.fromsequence(w_lst)
def delslice__Array_ANY_ANY(space, self, w_i, w_j):
- return space.delitem(self, space.newslice(w_i, w_j, space.w_None))
+ i = space.int_w(w_i)
+ if i < 0:
+ i += self.len
+ if i < 0:
+ i = 0
+ j = space.int_w(w_j)
+ if j < 0:
+ j += self.len
+ if j < 0:
+ j = 0
+ if j > self.len:
+ j = self.len
+ if i >= j:
+ return None
+ oldbuffer = self.buffer
+ self.buffer = lltype.malloc(mytype.arraytype,
+ max(self.len - (j - i), 0), flavor='raw',
+ add_memory_pressure=True)
+ for k in range(0, i):
+ self.buffer[k] = oldbuffer[k]
+ m = i
+ for k in range(j, self.len):
+ self.buffer[m] = oldbuffer[k]
+ m += 1
+ self.len -= j - i
+ self.allocated = self.len
+ if oldbuffer:
+ lltype.free(oldbuffer, flavor='raw')
+
# Add and mul methods
diff --git a/pypy/module/array/test/test_array.py
b/pypy/module/array/test/test_array.py
--- a/pypy/module/array/test/test_array.py
+++ b/pypy/module/array/test/test_array.py
@@ -681,6 +681,22 @@
a.__delslice__(0, 2)
assert repr(a) == "array('i', [5])"
+ a = self.array('i', [1, 2, 3, 4, 5])
+ del a[3:1]
+ assert repr(a) == "array('i', [1, 2, 3, 4, 5])"
+
+ del a[-100:1]
+ assert repr(a) == "array('i', [2, 3, 4, 5])"
+
+ del a[3:]
+ assert repr(a) == "array('i', [2, 3, 4])"
+
+ del a[-1:]
+ assert repr(a) == "array('i', [2, 3])"
+
+ del a[1:100]
+ assert repr(a) == "array('i', [2])"
+
def test_iter(self):
a = self.array('i', [1, 2, 3])
assert 1 in a
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit