Skip to content

Reduce branches in PyUnicode_MAX_CHAR_VALUE #92142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,17 +436,19 @@ static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
if (PyUnicode_IS_ASCII(op)) {
return 0x7fU;
}

static const Py_UCS4 max_char_values[] = {
0, /* PyUnicode_WCHAR_KIND */
0xffU, /* PyUnicode_1BYTE_KIND */
0xffffU, /* PyUnicode_2BYTE_KIND */
0,
0x10ffffU, /* PyUnicode_4BYTE_KIND */
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since kind is 3 bits, maybe add extra zero entries for a total of 8?

unsigned int kind = PyUnicode_KIND(op);
if (kind == PyUnicode_1BYTE_KIND) {
return 0xffU;
}
if (kind == PyUnicode_2BYTE_KIND) {
return 0xffffU;
}
assert(kind == PyUnicode_4BYTE_KIND);
return 0x10ffffU;
const Py_UCS4 value = max_char_values[kind];
assert(value);
return value;
}

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
# define PyUnicode_MAX_CHAR_VALUE(op) \
PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
Expand Down