bpo-32544: Speed up hasattr() and getattr() (GH-5173)

AttributeError was raised always when attribute is not found.
This commit skip raising AttributeError when `tp_getattro` is `PyObject_GenericGetAttr`.
It makes hasattr() and getattr() about 4x faster when attribute is not found.
This commit is contained in:
INADA Naoki 2018-01-16 20:52:41 +09:00 committed by GitHub
parent b44c5169f6
commit 378edee0a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 71 additions and 19 deletions

View file

@ -925,13 +925,15 @@ local_getattro(localobject *self, PyObject *name)
if (Py_TYPE(self) != &localtype)
/* use generic lookup for subtypes */
return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict);
return _PyObject_GenericGetAttrWithDict(
(PyObject *)self, name, ldict, 0);
/* Optimization: just look in dict ourselves */
value = PyDict_GetItem(ldict, name);
if (value == NULL)
/* Fall back on generic to get __class__ and __dict__ */
return _PyObject_GenericGetAttrWithDict((PyObject *)self, name, ldict);
return _PyObject_GenericGetAttrWithDict(
(PyObject *)self, name, ldict, 0);
Py_INCREF(value);
return value;