[issue2115] __slots__ make attribute setting 10x slower

2008-02-15 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc added the comment:

__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:
TypeError: descriptor 'a' for 'MyABC' objects doesn't apply to
'Unrelated' object

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-15 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc added the comment:

I think I fixed the issue.
svn annotate showed that the exact same optimization was applied on
__slots__ read access, five years ago: r28297.

--
resolution:  - fixed
status: open - closed

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Jeffrey Yasskin

New submission from Jeffrey Yasskin:

(On a MacBook Pro 2.33 GHz)

$ ./python.exe -m timeit -s 'class Foo(object): pass' -s 'f = Foo()'
'f.num = 3'
1000 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'
100 loops, best of 3: 1.24 usec per loop

Attribute reading isn't affected:
$ ./python.exe -m timeit -s 'class Foo(object): pass' -s 'f = Foo();
f.num = 3' 'g = f.num'
1000 loops, best of 3: 0.107 usec per loop
$ ./python.exe -m timeit -s 'class Foo(object): __slots__ = [num]' -s
'f = Foo(); f.num = 3' 'g = f.num'
1000 loops, best of 3: 0.101 usec per loop

--
components: Interpreter Core
messages: 62408
nosy: jyasskin
severity: normal
status: open
title: __slots__ make attribute setting 10x slower
type: resource usage
versions: Python 2.6

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Facundo Batista

Changes by Facundo Batista:


--
nosy: +facundobatista

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc added the comment:

This slowdown is due to the new implementation of isinstance in python2.6.

If I comment most of the code of PyObject_IsInstance in
Objects/abstract.c (remove all __instancecheck__ stuff; leave only the
last line), timings are much better, and more similar to 2.5 performance.

--
nosy: +amaury.forgeotdarc

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Thomas Heller

Thomas Heller added the comment:

I think this is a serious problem (and thanks, amaury, for finding the
spot). comtypes, like ctypes, uses quite a bit of isinstance calls.

It is the reason that the comtypes unit tests run between 8% and 25%
slower with trunk than with python 2.5.1.  If I comment out the code
that you mentioned, python trunk is slightly faster than 2.5.1.  I
suggest to raise the severity.

--
nosy: +theller

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Christian Heimes

Christian Heimes added the comment:

I agree, Thomas

--
nosy: +tiran
priority:  - high

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Thomas Heller

Thomas Heller added the comment:

PyObject_IsSubclass() has the same problem.

BTW; calling PyObject_GetAttr() with interned strings for
__instancecheck__ and __subclasscheck__ brings not enough speedup.

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc added the comment:

Something interesting:
- attribute setting uses PyObject_IsInstance, which is slower since the
introduction of ABCs.
- attribute reading uses PyObject_TypeCheck, which only searches the
__mro__.

Does this means that many calls to IsInstance could be replaced by
TypeCheck? Of course this makes sense only for new-style classes, but it
is the case for all built-in objects.

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Amaury Forgeot d'Arc

Amaury Forgeot d'Arc added the comment:

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,

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue2115] __slots__ make attribute setting 10x slower

2008-02-14 Thread Christian Heimes

Christian Heimes added the comment:

Thomas Heller wrote:
 BTW; calling PyObject_GetAttr() with interned strings for
 __instancecheck__ and __subclasscheck__ brings not enough speedup.

I've implemented the interning as static PyObject* in r60822. It should
give a small speedup.

__
Tracker [EMAIL PROTECTED]
http://bugs.python.org/issue2115
__
___
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com