New submission from INADA Naoki <songofaca...@gmail.com>:

ABCMeta.__new__ calls `getattr(value, "__isabstractmethod__", False)` many 
times.
https://github.com/python/cpython/blob/0f31c74fcfdec8f9e6157de2c366f2273de81677/Lib/abc.py#L135-L142

Since metaclass is used by subclasses, it checks all members of all subclasses 
(including concrete class).

But getattr() and hasattr() is inefficient when attribution is not found,
because it raises AttributeError and clear it internally.

I tried to bypass AttributeError when tp_getattro == PyObject_GenericGetAttr.
Here is quick micro benchmark:

$ ./python-patched -m perf timeit --compare-to=`pwd`/python-master -s 'def 
foo(): pass' 'hasattr(foo, "__isabstractmethod__")'python-master: 
..................... 385 ns +- 2 ns
python-patched: ..................... 87.6 ns +- 1.6 ns

Mean +- std dev: [python-master] 385 ns +- 2 ns -> [python-patched] 87.6 ns +- 
1.6 ns: 4.40x faster (-77%)

(I added Ivan to nosy list because he may implement C version of ABC.)

----------
messages: 309896
nosy: inada.naoki, levkivskyi
priority: normal
severity: normal
status: open
title: Speed up hasattr(o, name) and getattr(o, name, default)

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue32544>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to