Skip to content

Commit aac3b96

Browse files
authored
Merge pull request #3297 from damusss/optimize-colordict
Optimize correct colordict entries
2 parents 645cda1 + 3b69790 commit aac3b96

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

src_c/color.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -598,29 +598,34 @@ _parse_color_from_text(PyObject *str_obj, Uint8 *rgba)
598598
/* We assume the caller handled this check for us. */
599599
assert(PyUnicode_Check(str_obj));
600600

601-
name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", "");
602-
if (!name1) {
603-
return -1;
604-
}
605-
name2 = PyObject_CallMethod(name1, "lower", NULL);
606-
Py_DECREF(name1);
607-
if (!name2) {
608-
return -1;
609-
}
610-
color = PyDict_GetItem(_COLORDICT, name2);
611-
Py_DECREF(name2);
601+
color = PyDict_GetItem(_COLORDICT,
602+
str_obj); // optimize for correct color names
612603
if (!color) {
613-
switch (_hexcolor(str_obj, rgba)) {
614-
case TRISTATE_FAIL:
615-
PyErr_SetString(PyExc_ValueError, "invalid color name");
616-
return -1;
617-
case TRISTATE_ERROR:
618-
return -1;
619-
default:
620-
break;
604+
name1 = PyObject_CallMethod(str_obj, "replace", "(ss)", " ", "");
605+
if (!name1) {
606+
return -1;
607+
}
608+
name2 = PyObject_CallMethod(name1, "lower", NULL);
609+
Py_DECREF(name1);
610+
if (!name2) {
611+
return -1;
612+
}
613+
color = PyDict_GetItem(_COLORDICT, name2);
614+
Py_DECREF(name2);
615+
if (!color) {
616+
switch (_hexcolor(str_obj, rgba)) {
617+
case TRISTATE_FAIL:
618+
PyErr_SetString(PyExc_ValueError, "invalid color name");
619+
return -1;
620+
case TRISTATE_ERROR:
621+
return -1;
622+
default:
623+
return 0;
624+
}
621625
}
622626
}
623-
else if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) {
627+
628+
if (!pg_RGBAFromObjEx(color, rgba, PG_COLOR_HANDLE_RESTRICT_SEQ)) {
624629
PyErr_Format(PyExc_RuntimeError,
625630
"internal pygame error - colordict is supposed to "
626631
"only have tuple values, but there is an object of "

0 commit comments

Comments
 (0)