New submission from Oren Ben-Kiki:

In 3.6.0, invocation of isinstance calls 
typing._ProtocolMeta._get_protocol_attrs.
This creates a set of all attributes in all base classes, loops on these 
attributes to check they exist, and discards the set. It is very slow.

My program uses isinstance to allow for flexibility in parameter types in 
certain key functions. I realize that using isinstance is frowned upon, but it 
seems to make sense in my case.

As a result, >95% of its run-time is inside 
typing._ProtocolMeta._get_protocol_attrs (!).

I have created a simple wrapper around isinstance which caches its result with 
a Dict[Tuple[type, type], bool]. This solved the performance problem, but 
introduced a different problem - type checking.

I use mypy and type annotations, and my code cleanly type-checks (with the 
occasional # type: ignore). If I switch to using my own isinstance function, 
then mypy's type inference no longer treats it as special, so it starts 
complaining about all uses of values protected by if isinstance(value, 
SomeType): ...

I propose that either the private typing._ProtocolMeta.__subclasscheck__ (which 
invokes _get_protocol_attrs), or the public isinstance, would be modified to 
cache their results.

I can create a PR for either approach, if this is acceptable.

----------
components: Library (Lib)
messages: 294686
nosy: orenbenkiki
priority: normal
severity: normal
status: open
title: Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance
type: performance
versions: Python 3.6

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

Reply via email to