Skip to content

Commit 2de60f3

Browse files
Optimized all Rect/FRect methods via pgRect_FromObject (#2908)
* Optimized RectExport_RectFromObject * swapped PG_FORCEINLINE for PG_INLINE --------- Co-authored-by: Dan Lawrence <[email protected]>
1 parent 85967e2 commit 2de60f3

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src_c/rect.c

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
138138
#define RectImport_primitiveType int
139139
#define RectImport_RectCheck pgRect_Check
140140
#define RectImport_OtherRectCheck pgFRect_Check
141+
#define RectImport_OtherRectCheckExact pgFRect_CheckExact
141142
#define RectImport_RectCheckExact pgRect_CheckExact
142143
#define RectImport_innerRectStruct SDL_Rect
143144
#define RectImport_otherInnerRectStruct SDL_FRect
@@ -253,6 +254,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
253254
#define RectImport_primitiveType float
254255
#define RectImport_RectCheck pgFRect_Check
255256
#define RectImport_OtherRectCheck pgRect_Check
257+
#define RectImport_OtherRectCheckExact pgRect_CheckExact
256258
#define RectImport_RectCheckExact pgFRect_CheckExact
257259
#define RectImport_innerRectStruct SDL_FRect
258260
#define RectImport_otherInnerRectStruct SDL_Rect

src_c/rect_impl.h

+24-6
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,9 @@
320320
#ifndef RectImport_RectCheckExact
321321
#error RectImport_RectCheckExact needs to be Defined
322322
#endif
323+
#ifndef RectImport_OtherRectCheckExact
324+
#error RectImport_OtherRectCheckExact needs to be Defined
325+
#endif
323326
#ifndef RectImport_primitiveType
324327
#error RectImport_primitiveType needs to be defined
325328
#endif
@@ -424,7 +427,7 @@ RectExport_do_rects_intresect(InnerRect *A, InnerRect *B)
424427

425428
#define _pg_do_rects_intersect RectExport_do_rects_intresect
426429

427-
static InnerRect *
430+
static PG_INLINE InnerRect *
428431
RectExport_RectFromObject(PyObject *obj, InnerRect *temp);
429432
static InnerRect *
430433
RectExport_RectFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs,
@@ -611,16 +614,16 @@ static RectObject
611614
int RectOptional_Freelist_Num = -1;
612615
#endif
613616

614-
static InnerRect *
617+
static PG_INLINE InnerRect *
615618
RectExport_RectFromObject(PyObject *obj, InnerRect *temp)
616619
{
617620
Py_ssize_t length;
618621

619-
if (RectCheck(obj)) {
622+
/* fast path for exact Rect / FRect class */
623+
if (RectImport_RectCheckExact(obj)) {
620624
return &((RectObject *)obj)->r;
621625
}
622-
623-
if (OtherRectCheck(obj)) {
626+
if (RectImport_OtherRectCheckExact(obj)) {
624627
OtherInnerRect rect = ((OtherRectObject *)obj)->r;
625628
temp->x = (PrimitiveType)rect.x;
626629
temp->y = (PrimitiveType)rect.y;
@@ -629,6 +632,7 @@ RectExport_RectFromObject(PyObject *obj, InnerRect *temp)
629632
return temp;
630633
}
631634

635+
/* fast check for sequences */
632636
if (pgSequenceFast_Check(obj)) {
633637
length = PySequence_Fast_GET_SIZE(obj);
634638
PyObject **items = PySequence_Fast_ITEMS(obj);
@@ -721,7 +725,20 @@ RectExport_RectFromObject(PyObject *obj, InnerRect *temp)
721725
}
722726
}
723727

724-
/* Try to get the rect attribute */
728+
/* path for possible subclasses (these are very slow checks) */
729+
if (RectImport_RectCheck(obj)) {
730+
return &((RectObject *)obj)->r;
731+
}
732+
if (RectImport_OtherRectCheck(obj)) {
733+
OtherInnerRect rect = ((OtherRectObject *)obj)->r;
734+
temp->x = (PrimitiveType)rect.x;
735+
temp->y = (PrimitiveType)rect.y;
736+
temp->w = (PrimitiveType)rect.w;
737+
temp->h = (PrimitiveType)rect.h;
738+
return temp;
739+
}
740+
741+
/* path to get the 'rect' attribute if present */
725742
PyObject *rectattr;
726743
if (!(rectattr = PyObject_GetAttrString(obj, "rect"))) {
727744
PyErr_Clear();
@@ -2933,6 +2950,7 @@ RectExport_iterator(RectObject *self)
29332950
#undef RectImport_RectCheck
29342951
#undef RectImport_OtherRectCheck
29352952
#undef RectImport_RectCheckExact
2953+
#undef RectImport_OtherRectCheckExact
29362954
#undef RectImport_innerRectStruct
29372955
#undef RectImport_otherInnerRectStruct
29382956
#undef RectImport_innerPointStruct

0 commit comments

Comments
 (0)