-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-55531: Implement normalize_encoding
in C
#136643
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
base: main
Are you sure you want to change the base?
Changes from all commits
92873d6
4bae23a
b5f3df3
2ad72b2
3660160
4e12b9e
1c9e55a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
:mod:`encodings`: Improve :func:`~encodings.normalize_encoding` performance | ||
by implementing the function in C using the private | ||
``_Py_normalize_encoding`` which has been modified to make lowercase | ||
conversion optional. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1022,6 +1022,58 @@ _codecs_lookup_error_impl(PyObject *module, const char *name) | |
return PyCodec_LookupError(name); | ||
} | ||
|
||
extern int _Py_normalize_encoding(const char *, char *, size_t, int); | ||
|
||
/*[clinic input] | ||
_codecs._normalize_encoding | ||
encoding: unicode | ||
|
||
Normalize an encoding name *encoding*. | ||
|
||
Used for encodings.normalize_encoding. Does not convert to lower case. | ||
[clinic start generated code]*/ | ||
|
||
static PyObject * | ||
_codecs__normalize_encoding_impl(PyObject *module, PyObject *encoding) | ||
/*[clinic end generated code: output=d27465d81e361f8e input=3ff3f4d64995b988]*/ | ||
{ | ||
Py_ssize_t len; | ||
const char *cstr = PyUnicode_AsUTF8AndSize(encoding, &len); | ||
if (cstr == NULL) { | ||
return NULL; | ||
} | ||
|
||
if (len > PY_SSIZE_T_MAX) { | ||
PyErr_SetString(PyExc_OverflowError, "encoding is too large"); | ||
return NULL; | ||
} | ||
|
||
PyUnicodeWriter *writer = PyUnicodeWriter_Create(len + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that the Unicode writer API is new and shiny, but this complicated ? A simple call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had it that way originally but I was told to use it by @ZeroIntensity There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. People look to CPython for inspiration/howtos on their own extensions, I think we should be encouraging them to use things like That said, are encoding strings particularly large? If not, I think a simple stack allocation (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
From my knowledge, they should generally not be excessively long (I would estimate an upper bound 50 chars would be safe, 30 would probably be fine too), though there is no standard to refer to. I originally allocated the length of the input string, as it is the maximum length of the normalized string, I think that would be better than hard coding it. So, should I revert the commits to the original state, Marc/Peter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If performance is the motivation here, then I'm not a big fan of the original version. It made some needless copies and recalculations of the string size. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
--- a/Modules/_codecsmodule.c (revision 1c9e55ab8ffafd2bb0e68c688fadab90399cfc16)
+++ b/Modules/_codecsmodule.c (date 1753180784174)
@@ -1048,30 +1048,19 @@
return NULL;
}
- PyUnicodeWriter *writer = PyUnicodeWriter_Create(len + 1);
- if (writer == NULL) {
- return NULL;
- }
-
char *normalized = PyMem_Malloc(len + 1);
if (normalized == NULL) {
- PyUnicodeWriter_Discard(writer);
return PyErr_NoMemory();
}
if (!_Py_normalize_encoding(cstr, normalized, len + 1, 0)) {
PyMem_Free(normalized);
- PyUnicodeWriter_Discard(writer);
return NULL;
}
- if (PyUnicodeWriter_WriteUTF8(writer, normalized, (Py_ssize_t)strlen(normalized)) < 0) {
- PyUnicodeWriter_Discard(writer);
- PyMem_Free(normalized);
- return NULL;
- }
+ PyObject *result = PyUnicode_FromString(normalized);
PyMem_Free(normalized);
- return PyUnicodeWriter_Finish(writer);
+ return result;
}
/* --- Module API --------------------------------------------------------- */ |
||
if (writer == NULL) { | ||
return NULL; | ||
} | ||
|
||
char *normalized = PyMem_Malloc(len + 1); | ||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (normalized == NULL) { | ||
PyUnicodeWriter_Discard(writer); | ||
return PyErr_NoMemory(); | ||
} | ||
|
||
if (!_Py_normalize_encoding(cstr, normalized, len + 1, 0)) { | ||
PyMem_Free(normalized); | ||
PyUnicodeWriter_Discard(writer); | ||
return NULL; | ||
} | ||
|
||
if (PyUnicodeWriter_WriteUTF8(writer, normalized, (Py_ssize_t)strlen(normalized)) < 0) { | ||
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PyUnicodeWriter_Discard(writer); | ||
PyMem_Free(normalized); | ||
return NULL; | ||
} | ||
PyMem_Free(normalized); | ||
return PyUnicodeWriter_Finish(writer); | ||
} | ||
|
||
/* --- Module API --------------------------------------------------------- */ | ||
|
||
static PyMethodDef _codecs_functions[] = { | ||
|
@@ -1071,6 +1123,7 @@ static PyMethodDef _codecs_functions[] = { | |
_CODECS_REGISTER_ERROR_METHODDEF | ||
_CODECS__UNREGISTER_ERROR_METHODDEF | ||
_CODECS_LOOKUP_ERROR_METHODDEF | ||
_CODECS__NORMALIZE_ENCODING_METHODDEF | ||
{NULL, NULL} /* sentinel */ | ||
}; | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3587,13 +3587,14 @@ PyUnicode_FromEncodedObject(PyObject *obj, | |
return v; | ||
} | ||
|
||
/* Normalize an encoding name: similar to encodings.normalize_encoding(), but | ||
also convert to lowercase. Return 1 on success, or 0 on error (encoding is | ||
longer than lower_len-1). */ | ||
/* Normalize an encoding name like encodings.normalize_encoding() | ||
but allow to convert to lowercase if *to_lower* is true. | ||
Return 1 on success, or 0 on error (encoding is longer than lower_len-1). */ | ||
int | ||
_Py_normalize_encoding(const char *encoding, | ||
char *lower, | ||
size_t lower_len) | ||
size_t lower_len, | ||
int to_lower) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having the to_lower conditional in the tight loop is not ideal. It makes the function slower for all other uses. It's better to copy the value into a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OTOH, perhaps compilers are smart enough nowadays to figure this out by themselves 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some benchmarks show that the two cases are equivalent, so I assume my compiler optimizes it to the same thing in the end. It makes the code slightly more complex but I don't mind adding it if you insist. |
||
{ | ||
const char *e; | ||
char *l; | ||
|
@@ -3624,7 +3625,7 @@ _Py_normalize_encoding(const char *encoding, | |
if (l == l_end) { | ||
return 0; | ||
} | ||
*l++ = Py_TOLOWER(c); | ||
*l++ = to_lower ? Py_TOLOWER(c) : c; | ||
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
else { | ||
punct = 1; | ||
|
@@ -3659,7 +3660,7 @@ PyUnicode_Decode(const char *s, | |
} | ||
|
||
/* Shortcuts for common default encodings */ | ||
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower))) { | ||
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower), 1)) { | ||
char *lower = buflower; | ||
|
||
/* Fast paths */ | ||
|
@@ -3916,7 +3917,7 @@ PyUnicode_AsEncodedString(PyObject *unicode, | |
} | ||
|
||
/* Shortcuts for common default encodings */ | ||
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower))) { | ||
if (_Py_normalize_encoding(encoding, buflower, sizeof(buflower), 1)) { | ||
char *lower = buflower; | ||
|
||
/* Fast paths */ | ||
|
Uh oh!
There was an error while loading. Please reload this page.