Skip to content

Commit

Permalink
Optimized all Rect/FRect methods via pgRect_FromObject (pygame-co…
Browse files Browse the repository at this point in the history
…mmunity#2908)

* Optimized RectExport_RectFromObject

* swapped PG_FORCEINLINE for PG_INLINE

---------

Co-authored-by: Dan Lawrence <[email protected]>
  • Loading branch information
itzpr3d4t0r and MyreMylar authored Jul 10, 2024
1 parent 85967e2 commit 2de60f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src_c/rect.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
#define RectImport_primitiveType int
#define RectImport_RectCheck pgRect_Check
#define RectImport_OtherRectCheck pgFRect_Check
#define RectImport_OtherRectCheckExact pgFRect_CheckExact
#define RectImport_RectCheckExact pgRect_CheckExact
#define RectImport_innerRectStruct SDL_Rect
#define RectImport_otherInnerRectStruct SDL_FRect
Expand Down Expand Up @@ -253,6 +254,7 @@ four_floats_from_obj(PyObject *obj, float *val1, float *val2, float *val3,
#define RectImport_primitiveType float
#define RectImport_RectCheck pgFRect_Check
#define RectImport_OtherRectCheck pgRect_Check
#define RectImport_OtherRectCheckExact pgRect_CheckExact
#define RectImport_RectCheckExact pgFRect_CheckExact
#define RectImport_innerRectStruct SDL_FRect
#define RectImport_otherInnerRectStruct SDL_Rect
Expand Down
30 changes: 24 additions & 6 deletions src_c/rect_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@
#ifndef RectImport_RectCheckExact
#error RectImport_RectCheckExact needs to be Defined
#endif
#ifndef RectImport_OtherRectCheckExact
#error RectImport_OtherRectCheckExact needs to be Defined
#endif
#ifndef RectImport_primitiveType
#error RectImport_primitiveType needs to be defined
#endif
Expand Down Expand Up @@ -424,7 +427,7 @@ RectExport_do_rects_intresect(InnerRect *A, InnerRect *B)

#define _pg_do_rects_intersect RectExport_do_rects_intresect

static InnerRect *
static PG_INLINE InnerRect *
RectExport_RectFromObject(PyObject *obj, InnerRect *temp);
static InnerRect *
RectExport_RectFromFastcallArgs(PyObject *const *args, Py_ssize_t nargs,
Expand Down Expand Up @@ -611,16 +614,16 @@ static RectObject
int RectOptional_Freelist_Num = -1;
#endif

static InnerRect *
static PG_INLINE InnerRect *
RectExport_RectFromObject(PyObject *obj, InnerRect *temp)
{
Py_ssize_t length;

if (RectCheck(obj)) {
/* fast path for exact Rect / FRect class */
if (RectImport_RectCheckExact(obj)) {
return &((RectObject *)obj)->r;
}

if (OtherRectCheck(obj)) {
if (RectImport_OtherRectCheckExact(obj)) {
OtherInnerRect rect = ((OtherRectObject *)obj)->r;
temp->x = (PrimitiveType)rect.x;
temp->y = (PrimitiveType)rect.y;
Expand All @@ -629,6 +632,7 @@ RectExport_RectFromObject(PyObject *obj, InnerRect *temp)
return temp;
}

/* fast check for sequences */
if (pgSequenceFast_Check(obj)) {
length = PySequence_Fast_GET_SIZE(obj);
PyObject **items = PySequence_Fast_ITEMS(obj);
Expand Down Expand Up @@ -721,7 +725,20 @@ RectExport_RectFromObject(PyObject *obj, InnerRect *temp)
}
}

/* Try to get the rect attribute */
/* path for possible subclasses (these are very slow checks) */
if (RectImport_RectCheck(obj)) {
return &((RectObject *)obj)->r;
}
if (RectImport_OtherRectCheck(obj)) {
OtherInnerRect rect = ((OtherRectObject *)obj)->r;
temp->x = (PrimitiveType)rect.x;
temp->y = (PrimitiveType)rect.y;
temp->w = (PrimitiveType)rect.w;
temp->h = (PrimitiveType)rect.h;
return temp;
}

/* path to get the 'rect' attribute if present */
PyObject *rectattr;
if (!(rectattr = PyObject_GetAttrString(obj, "rect"))) {
PyErr_Clear();
Expand Down Expand Up @@ -2933,6 +2950,7 @@ RectExport_iterator(RectObject *self)
#undef RectImport_RectCheck
#undef RectImport_OtherRectCheck
#undef RectImport_RectCheckExact
#undef RectImport_OtherRectCheckExact
#undef RectImport_innerRectStruct
#undef RectImport_otherInnerRectStruct
#undef RectImport_innerPointStruct
Expand Down

0 comments on commit 2de60f3

Please sign in to comment.