Skip to content

Commit

Permalink
Optimize pg_Two(Ints/Floats)FromObj
Browse files Browse the repository at this point in the history
pg_IntFromObjIndex doesn't take advantage of the fact that pg_TwoIntsFromObj already knows that obj is a sequence. Since obj is known to be a sequence, and negative indices are not needed, PySequence_ITEM can be used instead of PySequence_GetItem for better performance.
  • Loading branch information
Starbuck5 committed Nov 4, 2024
1 parent de88561 commit d8daeaa
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,10 +541,25 @@ pg_TwoIntsFromObj(PyObject *obj, int *val1, int *val2)
if (!PySequence_Check(obj) || PySequence_Length(obj) != 2) {
return 0;
}
if (!pg_IntFromObjIndex(obj, 0, val1) ||
!pg_IntFromObjIndex(obj, 1, val2)) {

PyObject *item1 = PySequence_ITEM(obj, 0);
PyObject *item2 = PySequence_ITEM(obj, 1);

if (item1 == NULL || item2 == NULL) {
Py_XDECREF(item1);
Py_XDECREF(item2);
PyErr_Clear();
return 0;
}

if (!pg_IntFromObj(item1, val1) || !pg_IntFromObj(item2, val2)) {
Py_DECREF(item1);
Py_DECREF(item2);
return 0;
}

Py_DECREF(item1);
Py_DECREF(item2);
return 1;
}

Expand Down Expand Up @@ -586,10 +601,25 @@ pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2)
if (!PySequence_Check(obj) || PySequence_Length(obj) != 2) {
return 0;
}
if (!pg_FloatFromObjIndex(obj, 0, val1) ||
!pg_FloatFromObjIndex(obj, 1, val2)) {

PyObject *item1 = PySequence_ITEM(obj, 0);
PyObject *item2 = PySequence_ITEM(obj, 1);

if (item1 == NULL || item2 == NULL) {
Py_XDECREF(item1);
Py_XDECREF(item2);
PyErr_Clear();
return 0;
}

if (!pg_FloatFromObj(item1, val1) || !pg_FloatFromObj(item2, val2)) {
Py_DECREF(item1);
Py_DECREF(item2);
return 0;
}

Py_DECREF(item1);
Py_DECREF(item2);
return 1;
}

Expand Down

0 comments on commit d8daeaa

Please sign in to comment.