Commit e1882146 authored by Victor Stinner's avatar Victor Stinner

Issue #19512: add _PyUnicode_CompareWithId() function

_PyUnicode_CompareWithId() is faster than PyUnicode_CompareWithASCIIString()
when both strings are equal and interned.

Add also _PyId_builtins identifier for "builtins" common string.
parent c0cb1969
...@@ -147,9 +147,10 @@ typedef struct _Py_Identifier { ...@@ -147,9 +147,10 @@ typedef struct _Py_Identifier {
#define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value) #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value)
#define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
/* Common identifiers */ /* Common identifiers (ex: _PyId_path is the string "path") */
PyAPI_DATA(_Py_Identifier) _PyId_path;
PyAPI_DATA(_Py_Identifier) _PyId_argv; PyAPI_DATA(_Py_Identifier) _PyId_argv;
PyAPI_DATA(_Py_Identifier) _PyId_builtins;
PyAPI_DATA(_Py_Identifier) _PyId_path;
PyAPI_DATA(_Py_Identifier) _PyId_stdin; PyAPI_DATA(_Py_Identifier) _PyId_stdin;
PyAPI_DATA(_Py_Identifier) _PyId_stdout; PyAPI_DATA(_Py_Identifier) _PyId_stdout;
PyAPI_DATA(_Py_Identifier) _PyId_stderr; PyAPI_DATA(_Py_Identifier) _PyId_stderr;
......
...@@ -1996,6 +1996,11 @@ PyAPI_FUNC(int) PyUnicode_Compare( ...@@ -1996,6 +1996,11 @@ PyAPI_FUNC(int) PyUnicode_Compare(
PyObject *right /* Right string */ PyObject *right /* Right string */
); );
PyAPI_FUNC(int) _PyUnicode_CompareWithId(
PyObject *left, /* Left string */
_Py_Identifier *right /* Right identifier */
);
PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString( PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
PyObject *left, PyObject *left,
const char *right /* ASCII-encoded string */ const char *right /* ASCII-encoded string */
......
...@@ -345,11 +345,10 @@ type_set_qualname(PyTypeObject *type, PyObject *value, void *context) ...@@ -345,11 +345,10 @@ type_set_qualname(PyTypeObject *type, PyObject *value, void *context)
static PyObject * static PyObject *
type_module(PyTypeObject *type, void *context) type_module(PyTypeObject *type, void *context)
{ {
PyObject *mod;
char *s; char *s;
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__); PyObject *mod = _PyDict_GetItemId(type->tp_dict, &PyId___module__);
if (!mod) { if (!mod) {
PyErr_Format(PyExc_AttributeError, "__module__"); PyErr_Format(PyExc_AttributeError, "__module__");
return 0; return 0;
...@@ -358,11 +357,14 @@ type_module(PyTypeObject *type, void *context) ...@@ -358,11 +357,14 @@ type_module(PyTypeObject *type, void *context)
return mod; return mod;
} }
else { else {
PyObject *name;
s = strrchr(type->tp_name, '.'); s = strrchr(type->tp_name, '.');
if (s != NULL) if (s != NULL)
return PyUnicode_FromStringAndSize( return PyUnicode_FromStringAndSize(
type->tp_name, (Py_ssize_t)(s - type->tp_name)); type->tp_name, (Py_ssize_t)(s - type->tp_name));
return PyUnicode_FromString("builtins"); name = _PyUnicode_FromId(&_PyId_builtins);
Py_XINCREF(name);
return name;
} }
} }
...@@ -712,7 +714,7 @@ type_repr(PyTypeObject *type) ...@@ -712,7 +714,7 @@ type_repr(PyTypeObject *type)
return NULL; return NULL;
} }
if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) if (mod != NULL && _PyUnicode_CompareWithId(mod, &_PyId_builtins))
rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name); rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
else else
rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name); rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
...@@ -2143,7 +2145,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) ...@@ -2143,7 +2145,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
if (!valid_identifier(tmp)) if (!valid_identifier(tmp))
goto error; goto error;
assert(PyUnicode_Check(tmp)); assert(PyUnicode_Check(tmp));
if (PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) { if (_PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) {
if (!may_add_dict || add_dict) { if (!may_add_dict || add_dict) {
PyErr_SetString(PyExc_TypeError, PyErr_SetString(PyExc_TypeError,
"__dict__ slot disallowed: " "__dict__ slot disallowed: "
...@@ -2174,7 +2176,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) ...@@ -2174,7 +2176,7 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
for (i = j = 0; i < nslots; i++) { for (i = j = 0; i < nslots; i++) {
tmp = PyTuple_GET_ITEM(slots, i); tmp = PyTuple_GET_ITEM(slots, i);
if ((add_dict && if ((add_dict &&
PyUnicode_CompareWithASCIIString(tmp, "__dict__") == 0) || _PyUnicode_CompareWithId(tmp, &PyId___dict__) == 0) ||
(add_weak && (add_weak &&
PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0)) PyUnicode_CompareWithASCIIString(tmp, "__weakref__") == 0))
continue; continue;
...@@ -3183,7 +3185,7 @@ object_repr(PyObject *self) ...@@ -3183,7 +3185,7 @@ object_repr(PyObject *self)
Py_XDECREF(mod); Py_XDECREF(mod);
return NULL; return NULL;
} }
if (mod != NULL && PyUnicode_CompareWithASCIIString(mod, "builtins")) if (mod != NULL && _PyUnicode_CompareWithId(mod, &_PyId_builtins))
rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self); rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
else else
rtn = PyUnicode_FromFormat("<%s object at %p>", rtn = PyUnicode_FromFormat("<%s object at %p>",
...@@ -6337,7 +6339,7 @@ super_getattro(PyObject *self, PyObject *name) ...@@ -6337,7 +6339,7 @@ super_getattro(PyObject *self, PyObject *name)
(i.e. super, or a subclass), not the class of su->obj. */ (i.e. super, or a subclass), not the class of su->obj. */
skip = (PyUnicode_Check(name) && skip = (PyUnicode_Check(name) &&
PyUnicode_GET_LENGTH(name) == 9 && PyUnicode_GET_LENGTH(name) == 9 &&
PyUnicode_CompareWithASCIIString(name, "__class__") == 0); _PyUnicode_CompareWithId(name, &PyId___class__) == 0);
} }
if (!skip) { if (!skip) {
...@@ -6543,8 +6545,7 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds) ...@@ -6543,8 +6545,7 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds)
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
assert(PyUnicode_Check(name)); assert(PyUnicode_Check(name));
if (!PyUnicode_CompareWithASCIIString(name, if (!_PyUnicode_CompareWithId(name, &PyId___class__)) {
"__class__")) {
Py_ssize_t index = co->co_nlocals + Py_ssize_t index = co->co_nlocals +
PyTuple_GET_SIZE(co->co_cellvars) + i; PyTuple_GET_SIZE(co->co_cellvars) + i;
PyObject *cell = f->f_localsplus[index]; PyObject *cell = f->f_localsplus[index];
......
...@@ -10565,6 +10565,15 @@ PyUnicode_Compare(PyObject *left, PyObject *right) ...@@ -10565,6 +10565,15 @@ PyUnicode_Compare(PyObject *left, PyObject *right)
return -1; return -1;
} }
int
_PyUnicode_CompareWithId(PyObject *left, _Py_Identifier *right)
{
PyObject *right_str = _PyUnicode_FromId(right); /* borrowed */
if (right_str == NULL)
return -1;
return PyUnicode_Compare(left, right_str);
}
int int
PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str) PyUnicode_CompareWithASCIIString(PyObject* uni, const char* str)
{ {
......
...@@ -878,7 +878,7 @@ PyErr_WriteUnraisable(PyObject *obj) ...@@ -878,7 +878,7 @@ PyErr_WriteUnraisable(PyObject *obj)
goto done; goto done;
} }
else { else {
if (PyUnicode_CompareWithASCIIString(moduleName, "builtins") != 0) { if (_PyUnicode_CompareWithId(moduleName, &_PyId_builtins) != 0) {
if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0) if (PyFile_WriteObject(moduleName, f, Py_PRINT_RAW) < 0)
goto done; goto done;
if (PyFile_WriteString(".", f) < 0) if (PyFile_WriteString(".", f) < 0)
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
/* Common identifiers */ /* Common identifiers */
_Py_Identifier _PyId_argv = _Py_static_string_init("argv"); _Py_Identifier _PyId_argv = _Py_static_string_init("argv");
_Py_Identifier _PyId_builtins = _Py_static_string_init("builtins");
_Py_Identifier _PyId_path = _Py_static_string_init("path"); _Py_Identifier _PyId_path = _Py_static_string_init("path");
_Py_Identifier _PyId_stdin = _Py_static_string_init("stdin"); _Py_Identifier _PyId_stdin = _Py_static_string_init("stdin");
_Py_Identifier _PyId_stdout = _Py_static_string_init("stdout"); _Py_Identifier _PyId_stdout = _Py_static_string_init("stdout");
...@@ -1928,7 +1929,7 @@ print_exception(PyObject *f, PyObject *value) ...@@ -1928,7 +1929,7 @@ print_exception(PyObject *f, PyObject *value)
err = PyFile_WriteString("<unknown>", f); err = PyFile_WriteString("<unknown>", f);
} }
else { else {
if (PyUnicode_CompareWithASCIIString(moduleName, "builtins") != 0) if (_PyUnicode_CompareWithId(moduleName, &_PyId_builtins) != 0)
{ {
err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW); err = PyFile_WriteObject(moduleName, f, Py_PRINT_RAW);
err += PyFile_WriteString(".", f); err += PyFile_WriteString(".", f);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment