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
__slots__ make attribute setting 10x slower #46369
Comments
(On a MacBook Pro 2.33 GHz) $ ./python.exe -m timeit -s 'class Foo(object): pass' -s 'f = Foo()'
'f.num = 3'
10000000 loops, best of 3: 0.13 usec per loop
$ ./python.exe -m timeit -s 'class Foo(object): __slots__ = ["num"]' -s
'f = Foo()' 'f.num = 3'
1000000 loops, best of 3: 1.24 usec per loop Attribute reading isn't affected: |
This slowdown is due to the new implementation of isinstance in python2.6. If I comment most of the code of PyObject_IsInstance in |
I think this is a serious problem (and thanks, amaury, for finding the It is the reason that the comtypes unit tests run between 8% and 25% |
I agree, Thomas |
PyObject_IsSubclass() has the same problem. BTW; calling PyObject_GetAttr() with interned strings for |
Something interesting:
Does this means that many calls to IsInstance could be replaced by |
With this trivial patch, all tests still pass: Index: Objects/descrobject.c --- Objects/descrobject.c (revision 60754)
+++ Objects/descrobject.c (working copy)
@@ -166,7 +166,7 @@
int *pres)
{
assert(obj != NULL);
- if (!PyObject_IsInstance(obj, (PyObject *)(descr->d_type))) {
+ if (!PyObject_TypeCheck(obj, descr->d_type)) {
PyErr_Format(PyExc_TypeError,
"descriptor '%.200s' for '%.100s' objects "
"doesn't apply to '%.100s' object", |
Thomas Heller wrote:
I've implemented the interning as static PyObject* in r60822. It should |
__instancecheck__ is not only slower, it can also cause crashes: import abc
class MyABC:
__metaclass__ = abc.ABCMeta
__slots__ = ["a"]
class Unrelated:
pass
MyABC.register(Unrelated)
u=Unrelated()
assert isinstance(u, MyABC)
MyABC.a.__set__(u, 3) # Boom The patch I proposed above correctly raises the error: |
I think I fixed the issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: