summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2020-05-01 14:01:23 +0200
committerStefan Behnel <stefan_ml@behnel.de>2020-05-01 14:01:23 +0200
commit5f6369ae1d49baafce48442940ba7c42d3363222 (patch)
tree9a1c7bad2c9f99e28c0f6996b1b284566c501683
parent7491c23594d1f54fe06c42e3c09487956bdc6d32 (diff)
downloadcython-5f6369ae1d49baafce48442940ba7c42d3363222.tar.gz
Rewrite _unellipsify() helper function in memoryview code to speed it up and reduce the generated C code.
-rw-r--r--Cython/Utility/MemoryView.pyx29
1 files changed, 12 insertions, 17 deletions
diff --git a/Cython/Utility/MemoryView.pyx b/Cython/Utility/MemoryView.pyx
index 12f2113a2..8d7848726 100644
--- a/Cython/Utility/MemoryView.pyx
+++ b/Cython/Utility/MemoryView.pyx
@@ -669,33 +669,28 @@ cdef tuple _unellipsify(object index, int ndim):
Replace all ellipses with full slices and fill incomplete indices with
full slices.
"""
- if not isinstance(index, tuple):
- tup = (index,)
- else:
- tup = index
+ cdef Py_ssize_t idx
+ tup = <tuple>index if isinstance(index, tuple) else (index,)
- result = []
+ result = [slice(None)] * ndim
have_slices = False
seen_ellipsis = False
- for idx, item in enumerate(tup):
+ idx = 0
+ for item in tup:
if item is Ellipsis:
if not seen_ellipsis:
- result.extend([slice(None)] * (ndim - len(tup) + 1))
+ idx += ndim - len(tup)
seen_ellipsis = True
- else:
- result.append(slice(None))
have_slices = True
else:
- if not isinstance(item, slice) and not PyIndex_Check(item):
+ if isinstance(item, slice):
+ have_slices = True
+ elif not PyIndex_Check(item):
raise TypeError(f"Cannot index with type '{type(item)}'")
+ result[idx] = item
+ idx += 1
- have_slices = have_slices or isinstance(item, slice)
- result.append(item)
-
- nslices = ndim - len(result)
- if nslices:
- result.extend([slice(None)] * nslices)
-
+ nslices = ndim - idx
return have_slices or nslices, tuple(result)
cdef assert_direct_dimensions(Py_ssize_t *suboffsets, int ndim):