Skip to content

Commit

Permalink
Address Stefan's feedback
Browse files Browse the repository at this point in the history
Co-authored-by: scoder <[email protected]>
  • Loading branch information
lysnikolaou and scoder committed Jul 18, 2024
1 parent b4badde commit a0a5a99
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 65 deletions.
50 changes: 22 additions & 28 deletions Cython/Compiler/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3120,31 +3120,25 @@ def generate_next_sequence_item(self, test_name, result_name, code):
else:
inc_dec = '++'
if test_name == 'List':
code.putln("#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS")
code.putln(
"%s = __Pyx_PyList_GetItemRef(%s, %s); __Pyx_GOTREF(%s); %s%s; %s" % (
result_name,
self.py_result(),
self.counter_cname,
result_name,
self.counter_cname,
inc_dec,
# use the error label to avoid C compiler warnings if we only use it below
code.error_goto_if_neg('0', self.pos)
))
getitem = "__Pyx_PyList_GetItemRef"
incref = "__Pyx_GOTREF"
else: # Tuple
code.putln("#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS")
code.putln(
"%s = PyTuple_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s%s; %s" % (
result_name,
self.py_result(),
self.counter_cname,
result_name,
self.counter_cname,
inc_dec,
# use the error label to avoid C compiler warnings if we only use it below
code.error_goto_if_neg('0', self.pos)
))
getitem = "PyTuple_GET_ITEM"
incref = "__Pyx_INCREF"
code.putln("#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS")
code.putln(
"%s = %s(%s, %s); %s(%s); %s%s; %s" % (
result_name,
getitem,
self.py_result(),
self.counter_cname,
incref,
result_name,
self.counter_cname,
inc_dec,
# use the error label to avoid C compiler warnings if we only use it below
code.error_goto_if_neg('0', self.pos)
))
code.putln("#else")
code.putln(
"%s = __Pyx_PySequence_ITEM(%s, %s); %s%s; %s" % (
Expand Down Expand Up @@ -8388,22 +8382,22 @@ def generate_special_parallel_unpacking_code(self, code, rhs, use_loop):
code.putln("if (likely(Py%s_CheckExact(sequence))) {" % sequence_types[0])
for i, item in enumerate(self.unpacked_items):
if sequence_types[0] == "List":
code.putln("%s = __Pyx_PyList_GetItemRef(sequence, %d); " % (
code.putln("%s = __Pyx_PyList_GetItemRef(sequence, %d);" % (
item.result(), i))
code.put_xgotref(item.result(), item.ctype())
else: # Tuple
code.putln("%s = PyTuple_GET_ITEM(sequence, %d); " % (
code.putln("%s = PyTuple_GET_ITEM(sequence, %d);" % (
item.result(), i))
code.put_incref(item.result(), item.ctype())
if len(sequence_types) == 2:
code.putln("} else {")
for i, item in enumerate(self.unpacked_items):
if sequence_types[1] == "List":
code.putln("%s = __Pyx_PyList_GetItemRef(sequence, %d); " % (
code.putln("%s = __Pyx_PyList_GetItemRef(sequence, %d);" % (
item.result(), i))
code.put_xgotref(item.result(), item.ctype())
else: # Tuple
code.putln("%s = PyTuple_GET_ITEM(sequence, %d); " % (
code.putln("%s = PyTuple_GET_ITEM(sequence, %d);" % (
item.result(), i))
code.put_incref(item.result(), item.ctype())
code.putln("}")
Expand Down
13 changes: 9 additions & 4 deletions Cython/Utility/ModuleSetupCode.c
Original file line number Diff line number Diff line change
Expand Up @@ -2239,10 +2239,14 @@ static PyObject* __Pyx_PyCode_New(
varnames_tuple_dedup = varnames_tuple;
}

#if CYTHON_AVOID_BORROWED_REFS || (CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS && __PYX_LIMITED_VERSION_HEX >= 0x030d00b1)
Py_XDECREF(varnames_tuple_dedup);
#endif

#else // !CYTHON_COMPILING_IN_LIMITED_API

#if CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS && PY_VERSION_HEX >= 0x030d00b1
if(unlikely(PyDict_SetDefaultRef(tuple_dedup_map, varnames_tuple, varnames_tuple, &varnames_tuple_dedup) < 0)) goto done;
if (unlikely(PyDict_SetDefaultRef(tuple_dedup_map, varnames_tuple, varnames_tuple, &varnames_tuple_dedup) < 0)) goto done;
#else // !(CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS && PY_VERSION_HEX >= 0x030d00b1)
varnames_tuple_dedup = PyDict_SetDefault(tuple_dedup_map, varnames_tuple, varnames_tuple);
if (unlikely(!varnames_tuple_dedup)) goto done;
Expand All @@ -2251,6 +2255,10 @@ static PyObject* __Pyx_PyCode_New(
#endif // CYTHON_AVOID_BORROWED_REFS
#endif // CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS && PY_VERSION_HEX >= 0x030d00b1

#if CYTHON_AVOID_BORROWED_REFS || (CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS && PY_VERSION_HEX >= 0x030d00b1)
Py_XDECREF(varnames_tuple_dedup);
#endif

#endif // CYTHON_COMPILING_IN_LIMITED_API

code_obj = (PyObject*) __Pyx__PyCode_New(
Expand All @@ -2273,9 +2281,6 @@ static PyObject* __Pyx_PyCode_New(
);

done:
#if CYTHON_AVOID_BORROWED_REFS || CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS
Py_XDECREF(varnames_tuple_dedup);
#endif
Py_DECREF(varnames_tuple);
return code_obj;
}
Expand Down
33 changes: 8 additions & 25 deletions Cython/Utility/ObjectHandling.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,17 @@ static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
return r;
}

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i,
{{for type in ['List', 'Tuple']}}
static CYTHON_INLINE PyObject *__Pyx_GetItemInt_{{type}}_Fast(PyObject *o, Py_ssize_t i,
CYTHON_NCP_UNUSED int wraparound,
CYTHON_NCP_UNUSED int boundscheck) {
#if CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE && !CYTHON_AVOID_BORROWED_REFS
#if CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE && !CYTHON_AVOID_BORROWED_REFS{{if type == 'List'}} && !CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS{{endif}}
Py_ssize_t wrapped_i = i;
if (wraparound & unlikely(i < 0)) {
wrapped_i += PyTuple_GET_SIZE(o);
wrapped_i += Py{{type}}_GET_SIZE(o);
}
if ((!boundscheck) || likely(__Pyx_is_valid_index(wrapped_i, PyTuple_GET_SIZE(o)))) {
PyObject *r = PyTuple_GET_ITEM(o, wrapped_i);
if ((!boundscheck) || likely(__Pyx_is_valid_index(wrapped_i, Py{{type}}_GET_SIZE(o)))) {
PyObject *r = Py{{type}}_GET_ITEM(o, wrapped_i);
Py_INCREF(r);
return r;
}
Expand All @@ -442,25 +443,7 @@ static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize
return PySequence_GetItem(o, i);
#endif
}

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i,
CYTHON_NCP_UNUSED int wraparound,
CYTHON_NCP_UNUSED int boundscheck) {
#if CYTHON_ASSUME_SAFE_MACROS && CYTHON_ASSUME_SAFE_SIZE && !CYTHON_AVOID_BORROWED_REFS && !CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS
Py_ssize_t wrapped_i = i;
if (wraparound & unlikely(i < 0)) {
wrapped_i += PyList_GET_SIZE(o);
}
if ((!boundscheck) || likely(__Pyx_is_valid_index(wrapped_i, PyList_GET_SIZE(o)))) {
PyObject *r = PyList_GET_ITEM(o, wrapped_i);
Py_INCREF(r);
return r;
}
return __Pyx_GetItemInt_Generic(o, PyLong_FromSsize_t(i));
#else
return PySequence_GetItem(o, i);
#endif
}
{{endfor}}

static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, int is_list,
CYTHON_NCP_UNUSED int wraparound,
Expand Down Expand Up @@ -543,14 +526,14 @@ static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObje
if (is_list || PyList_CheckExact(o)) {
Py_ssize_t n = (!wraparound) ? i : ((likely(i >= 0)) ? i : i + PyList_GET_SIZE(o));
if ((!boundscheck) || likely(__Pyx_is_valid_index(n, PyList_GET_SIZE(o)))) {
Py_INCREF(v);
#if !CYTHON_AVOID_THREAD_UNSAFE_BORROWED_REFS
PyObject* old = PyList_GET_ITEM(o, n);
PyList_SET_ITEM(o, n, v);
Py_DECREF(old);
#else
PyList_SetItem(o, n, v);
#endif
Py_INCREF(v);
return 1;
}
} else {
Expand Down
12 changes: 4 additions & 8 deletions Cython/Utility/TypeConversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,23 @@ static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*);
#define __Pyx_PyUnicode_FromOrdinal(o) PyUnicode_FromOrdinal((int)o)
#define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode

#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030a0000
static CYTHON_INLINE PyObject *__Pyx_NewRef(PyObject *obj) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030a0000 || defined(Py_NewRef)
return Py_NewRef(obj);
}
#else
static CYTHON_INLINE PyObject *__Pyx_NewRef(PyObject *obj) {
Py_INCREF(obj);
return obj;
}
#endif
}

#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030a0000
static CYTHON_INLINE PyObject *__Pyx_XNewRef(PyObject *obj) {
#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030a0000 || defined(Py_XNewRef)
return Py_XNewRef(obj);
}
#else
static CYTHON_INLINE PyObject *__Pyx_XNewRef(PyObject *obj) {
Py_XINCREF(obj);
return obj;
}
#endif
}

#define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None)
static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b);
Expand Down

0 comments on commit a0a5a99

Please sign in to comment.