From 0cc27af4fce98b6e9fcb928932eb5eef1b266ade Mon Sep 17 00:00:00 2001 From: damusss Date: Sun, 19 Jan 2025 12:46:52 +0100 Subject: [PATCH 1/3] optimize correct colordict entries --- src_c/color.c | 54 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src_c/color.c b/src_c/color.c index 97abed4c45..499dbe3fdb 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -598,29 +598,42 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) /* We assume the caller handled this check for us. */ assert(PyUnicode_Check(str_obj)); - name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", ""); - if (!name1) { - return -1; - } - name2 = PyObject_CallMethod(name1, "lower", NULL); - Py_DECREF(name1); - if (!name2) { - return -1; - } - color = PyDict_GetItem(_COLORDICT, name2); - Py_DECREF(name2); + color = PyDict_GetItem(_COLORDICT, + str_obj); // optimize for correct color names if (!color) { - switch (_hexcolor(str_obj, rgba)) { - case TRISTATE_FAIL: - PyErr_SetString(PyExc_ValueError, "invalid color name"); - return -1; - case TRISTATE_ERROR: - return -1; - default: - break; + name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", ""); + if (!name1) { + return -1; } + name2 = PyObject_CallMethod(name1, "lower", NULL); + Py_DECREF(name1); + if (!name2) { + return -1; + } + color = PyDict_GetItem(_COLORDICT, name2); + Py_DECREF(name2); + if (!color) { + switch (_hexcolor(str_obj, rgba)) { + case TRISTATE_FAIL: + PyErr_SetString(PyExc_ValueError, "invalid color name"); + return -1; + case TRISTATE_ERROR: + return -1; + default: + goto success; + break; + } + } + else { + goto found_color; + } + } + else { + goto found_color; } - else if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) { + +found_color: + if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) { PyErr_Format(PyExc_RuntimeError, "internal pygame error - colordict is supposed to " "only have tuple values, but there is an object of " @@ -628,6 +641,7 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) Py_TYPE(color)->tp_name); return -1; } +success: return 0; } From be4ff3f1564e58a849abbd08738dc97bfa56fc1d Mon Sep 17 00:00:00 2001 From: damusss Date: Sun, 19 Jan 2025 13:14:23 +0100 Subject: [PATCH 2/3] Remove redundant label --- src_c/color.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src_c/color.c b/src_c/color.c index 499dbe3fdb..b5d7d9acd2 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -624,15 +624,8 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) break; } } - else { - goto found_color; - } - } - else { - goto found_color; } -found_color: if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) { PyErr_Format(PyExc_RuntimeError, "internal pygame error - colordict is supposed to " From 3b697908f88c24dbc37c706402bf1e865c24aee5 Mon Sep 17 00:00:00 2001 From: damusss Date: Sun, 19 Jan 2025 13:15:24 +0100 Subject: [PATCH 3/3] Remove additional unnecessary label (lol) --- src_c/color.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src_c/color.c b/src_c/color.c index b5d7d9acd2..f5eddcc0e5 100644 --- a/src_c/color.c +++ b/src_c/color.c @@ -620,8 +620,7 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) case TRISTATE_ERROR: return -1; default: - goto success; - break; + return 0; } } } @@ -634,7 +633,6 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba) Py_TYPE(color)->tp_name); return -1; } -success: return 0; }