Skip to content

Commit

Permalink
Fix and test memoryview indexing performance_hint
Browse files Browse the repository at this point in the history
  • Loading branch information
da-woods committed Oct 18, 2023
1 parent 575641c commit 4b959b7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cython/Compiler/ExprNodes.py
Expand Up @@ -4965,7 +4965,7 @@ def analyse_types(self, env, getting=True):

elif index.type.is_int or index.type.is_pyobject:
if index.type.is_pyobject and not self.warned_untyped_idx:
performance_hint(index.pos, "Index should be typed for more efficient access")
performance_hint(index.pos, "Index should be typed for more efficient access", env)
MemoryViewIndexNode.warned_untyped_idx = True

self.is_memview_index = True
Expand Down
19 changes: 19 additions & 0 deletions tests/memoryview/memoryview.pyx
@@ -1,4 +1,5 @@
# mode: run
# tag: perf_hints

# Test declarations, behaviour and coercions of the memoryview type itself.

Expand Down Expand Up @@ -1298,3 +1299,21 @@ def test_assignment_typedef():
y = x
for v in y:
print(v)

def test_untyped_index(i):
"""
>>> test_untyped_index(2)
3
>>> test_untyped_index(0)
5
>>> test_untyped_index(-1)
0
"""
cdef int[6] arr
arr = [5, 4, 3, 2, 1, 0]
cdef int[:] mview_arr = arr
return mview_arr[i] # should generate a performance hint

_PERFORMANCE_HINTS = """
1315:21: Index should be typed for more efficient access
"""

0 comments on commit 4b959b7

Please sign in to comment.