Skip to content

Commit 466060c

Browse files
committed
Inline tuple ops.
1 parent 630b101 commit 466060c

2 files changed

Lines changed: 37 additions & 44 deletions

File tree

mypyc/lib-rt/CPy.h

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -862,11 +862,44 @@ bool CPySet_Remove(PyObject *set, PyObject *key);
862862

863863
// Tuple operations
864864

865-
866-
PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index);
867865
PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end);
868-
PyObject *CPySequenceTuple_GetItemUnsafe(PyObject *tuple, Py_ssize_t index);
869-
void CPySequenceTuple_SetItemUnsafe(PyObject *tuple, Py_ssize_t index, PyObject *value);
866+
867+
static inline bool CPySequenceTuple_IsValidIndex(Py_ssize_t n, Py_ssize_t size) {
868+
return (0 <= n && n < size) || (-size <= n && n < 0);
869+
}
870+
871+
static inline PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index)
872+
{
873+
if (likely(CPyTagged_CheckShort(index))) {
874+
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
875+
Py_ssize_t size = PyTuple_GET_SIZE(tuple);
876+
if (unlikely(!CPySequenceTuple_IsValidIndex(n, size)))
877+
{
878+
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
879+
return NULL;
880+
}
881+
if (n < 0)
882+
n += size;
883+
PyObject *result = PyTuple_GET_ITEM(tuple, n);
884+
Py_INCREF(result);
885+
return result;
886+
} else {
887+
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
888+
return NULL;
889+
}
890+
}
891+
892+
static inline PyObject *CPySequenceTuple_GetItemUnsafe(PyObject *tuple, Py_ssize_t index)
893+
{
894+
PyObject *result = PyTuple_GET_ITEM(tuple, index);
895+
Py_INCREF(result);
896+
return result;
897+
}
898+
899+
static inline void CPySequenceTuple_SetItemUnsafe(PyObject *tuple, Py_ssize_t index, PyObject *value)
900+
{
901+
PyTuple_SET_ITEM(tuple, index, value);
902+
}
870903

871904

872905
// Exception operations

mypyc/lib-rt/tuple_ops.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,6 @@
55
#include <Python.h>
66
#include "CPy.h"
77

8-
PyObject *CPySequenceTuple_GetItem(PyObject *tuple, CPyTagged index) {
9-
if (CPyTagged_CheckShort(index)) {
10-
Py_ssize_t n = CPyTagged_ShortAsSsize_t(index);
11-
Py_ssize_t size = PyTuple_GET_SIZE(tuple);
12-
if (n >= 0) {
13-
if (n >= size) {
14-
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
15-
return NULL;
16-
}
17-
} else {
18-
n += size;
19-
if (n < 0) {
20-
PyErr_SetString(PyExc_IndexError, "tuple index out of range");
21-
return NULL;
22-
}
23-
}
24-
PyObject *result = PyTuple_GET_ITEM(tuple, n);
25-
Py_INCREF(result);
26-
return result;
27-
} else {
28-
PyErr_SetString(PyExc_OverflowError, CPYTHON_LARGE_INT_ERRMSG);
29-
return NULL;
30-
}
31-
}
32-
338
PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged end) {
349
if (likely(PyTuple_CheckExact(obj)
3510
&& CPyTagged_CheckShort(start) && CPyTagged_CheckShort(end))) {
@@ -45,18 +20,3 @@ PyObject *CPySequenceTuple_GetSlice(PyObject *obj, CPyTagged start, CPyTagged en
4520
}
4621
return CPyObject_GetSlice(obj, start, end);
4722
}
48-
49-
// No error checking
50-
PyObject *CPySequenceTuple_GetItemUnsafe(PyObject *tuple, Py_ssize_t index)
51-
{
52-
PyObject *result = PyTuple_GET_ITEM(tuple, index);
53-
Py_INCREF(result);
54-
return result;
55-
}
56-
57-
// PyTuple_SET_ITEM does no error checking,
58-
// and should only be used to fill in brand new tuples.
59-
void CPySequenceTuple_SetItemUnsafe(PyObject *tuple, Py_ssize_t index, PyObject *value)
60-
{
61-
PyTuple_SET_ITEM(tuple, index, value);
62-
}

0 commit comments

Comments
 (0)