Skip to content

Commit d8daeaa

Browse files
committed
Optimize pg_Two(Ints/Floats)FromObj
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.
1 parent de88561 commit d8daeaa

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src_c/base.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,25 @@ pg_TwoIntsFromObj(PyObject *obj, int *val1, int *val2)
541541
if (!PySequence_Check(obj) || PySequence_Length(obj) != 2) {
542542
return 0;
543543
}
544-
if (!pg_IntFromObjIndex(obj, 0, val1) ||
545-
!pg_IntFromObjIndex(obj, 1, val2)) {
544+
545+
PyObject *item1 = PySequence_ITEM(obj, 0);
546+
PyObject *item2 = PySequence_ITEM(obj, 1);
547+
548+
if (item1 == NULL || item2 == NULL) {
549+
Py_XDECREF(item1);
550+
Py_XDECREF(item2);
551+
PyErr_Clear();
546552
return 0;
547553
}
554+
555+
if (!pg_IntFromObj(item1, val1) || !pg_IntFromObj(item2, val2)) {
556+
Py_DECREF(item1);
557+
Py_DECREF(item2);
558+
return 0;
559+
}
560+
561+
Py_DECREF(item1);
562+
Py_DECREF(item2);
548563
return 1;
549564
}
550565

@@ -586,10 +601,25 @@ pg_TwoFloatsFromObj(PyObject *obj, float *val1, float *val2)
586601
if (!PySequence_Check(obj) || PySequence_Length(obj) != 2) {
587602
return 0;
588603
}
589-
if (!pg_FloatFromObjIndex(obj, 0, val1) ||
590-
!pg_FloatFromObjIndex(obj, 1, val2)) {
604+
605+
PyObject *item1 = PySequence_ITEM(obj, 0);
606+
PyObject *item2 = PySequence_ITEM(obj, 1);
607+
608+
if (item1 == NULL || item2 == NULL) {
609+
Py_XDECREF(item1);
610+
Py_XDECREF(item2);
611+
PyErr_Clear();
591612
return 0;
592613
}
614+
615+
if (!pg_FloatFromObj(item1, val1) || !pg_FloatFromObj(item2, val2)) {
616+
Py_DECREF(item1);
617+
Py_DECREF(item2);
618+
return 0;
619+
}
620+
621+
Py_DECREF(item1);
622+
Py_DECREF(item2);
593623
return 1;
594624
}
595625

0 commit comments

Comments
 (0)