classification
Title: isinstance is 4x as slow as in 2.5 because the common case raises
Type: performance Stage:
Components: Interpreter Core Versions: Python 2.6
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: gregory.p.smith, gvanrossum, jyasskin, rhettinger, theller
Priority: critical Keywords: patch

Created on 2008-03-16 15:45 by jyasskin, last changed 2008-07-13 18:38 by gregory.p.smith. This issue is now closed.

Files
File name Uploaded Description Edit
isinst.diff rhettinger, 2008-03-18 22:25 Patch to short circuit an exact match
Messages (11)
msg63580 - (view) Author: Jeffrey Yasskin (jyasskin) * (Python committer) Date: 2008-03-16 15:45
r58099 added an exception to the common case of PyObject_IsInstance(),
when the class has no __instancecheck__ attribute. This makes
isinstance(3, int) take 4x as long as in python 2.5.
msg63878 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-03-18 04:02
I'll set this to critical to ensure that I look at it at least once
before we release.  I'm not sure however that we can do much about it --
nor that it matters much in practice.

Perhaps we could speed up certain common isinstance() calls by skipping
the lookup for non-heap types; I believe those never override
__instancheck__.
msg63935 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-03-18 16:21
Would it help to implement a default __instancecheck__ and
__subclasscheck__ for object (or for type), that subclasses can override?
msg63952 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-03-18 17:49
Perhaps, though I'm not sure if that doesn't slow things down further
due to the complicated protocol for calling it.  Also, there's a
recursion check in the built-in implementation.
msg63958 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-03-18 18:06
The attribute lookup cost can mostly be eliminated if __instancecheck__
were given a tp slot.
msg63986 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-03-18 20:59
Yeah, but tp_ slots are expensive themselves (mostly in the amount of
code that needs to be changed -- see typeobject.c).
msg63991 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-03-18 21:20
No doubt it would take some work.  IMO, code for a slot is worth it;
otherwise, many apps will have to pay the price for ABCs even if they
don't use the feature.  For my company, that would deter an upgrade to 2.6.
msg64002 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-03-18 22:25
Attaching a small patch to speed-up one easy case.
msg64844 - (view) Author: Thomas Heller (theller) * (Python committer) Date: 2008-04-02 09:48
Issue #2534 has a patch which speeds up isinstance and issubclass by
implementing type.__instancecheck__ and type.__subclasscheck__.
msg69487 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-07-09 21:11
Does anyone care about this still?

I added some comments on
http://codereview.appspot.com/504
msg69616 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2008-07-13 18:38
% ./python.exe -mtimeit 'isinstance(3, int)'
1000000 loops, best of 3: 0.269 usec per loop
% ../release25-maint/python.exe -mtimeit 'isinstance(3, int)'1000000
loops, best of 3: 0.335 usec per loop

So I'd say its no longer 4x slower these days.

Looking at the Object/abstract.c it appears the fix to this has already
been checked in.  PyObject_IsInstance already has this in it:

        /* Quick test for an exact match */
        if (Py_TYPE(inst) == (PyTypeObject *)cls)
                return 1;

closing.
History
Date User Action Args
2008-07-13 18:38:06gregory.p.smithsetstatus: open -> closed
nosy: + gregory.p.smith
resolution: accepted
messages: + msg69616
2008-07-09 21:11:24gvanrossumsetmessages: + msg69487
2008-04-02 09:48:55thellersetmessages: + msg64844
2008-03-18 22:25:15rhettingersetfiles: + isinst.diff
keywords: + patch
messages: + msg64002
2008-03-18 21:20:26rhettingersetmessages: + msg63991
2008-03-18 20:59:32gvanrossumsetmessages: + msg63986
2008-03-18 18:06:36rhettingersetnosy: + rhettinger
messages: + msg63958
2008-03-18 17:49:40gvanrossumsetmessages: + msg63952
2008-03-18 16:21:46thellersetnosy: + theller
messages: + msg63935
2008-03-18 04:02:18gvanrossumsetpriority: critical
messages: + msg63878
2008-03-16 21:05:53georg.brandlsettype: behavior -> performance
2008-03-16 15:45:41jyasskincreate