Skip to content

isinstance is 4x as slow as in 2.5 because the common case raises #46556

Closed

Description

@jyasskin
mannequin
BPO 2303
Nosy @gvanrossum, @theller, @rhettinger, @gpshead
Files
  • isinst.diff: Patch to short circuit an exact match
  • 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:

    assignee = 'https://github.com/gvanrossum'
    closed_at = <Date 2008-07-13.18:38:06.819>
    created_at = <Date 2008-03-16.15:45:41.157>
    labels = ['interpreter-core', 'performance']
    title = 'isinstance is 4x as slow as in 2.5 because the common case raises'
    updated_at = <Date 2008-07-13.18:38:06.813>
    user = 'https://bugs.python.org/jyasskin'

    bugs.python.org fields:

    activity = <Date 2008-07-13.18:38:06.813>
    actor = 'gregory.p.smith'
    assignee = 'gvanrossum'
    closed = True
    closed_date = <Date 2008-07-13.18:38:06.819>
    closer = 'gregory.p.smith'
    components = ['Interpreter Core']
    creation = <Date 2008-03-16.15:45:41.157>
    creator = 'jyasskin'
    dependencies = []
    files = ['9742']
    hgrepos = []
    issue_num = 2303
    keywords = ['patch']
    message_count = 11.0
    messages = ['63580', '63878', '63935', '63952', '63958', '63986', '63991', '64002', '64844', '69487', '69616']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'theller', 'rhettinger', 'gregory.p.smith', 'jyasskin']
    pr_nums = []
    priority = 'critical'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue2303'
    versions = ['Python 2.6']

    Activity

    1. jyasskin commented on Mar 16, 2008

      jyasskinmannequin
      MannequinAuthor

      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.

    2. added
      performancePerformance or resource usage
      and removed
      type-bugAn unexpected behavior, bug, or error
      on Mar 16, 2008
    3. gvanrossum commented on Mar 18, 2008

      @gvanrossum
      Member

      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__.

    4. theller commented on Mar 18, 2008

      @theller

      Would it help to implement a default __instancecheck__ and
      __subclasscheck__ for object (or for type), that subclasses can override?

    5. gvanrossum commented on Mar 18, 2008

      @gvanrossum
      Member

      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.

    6. rhettinger commented on Mar 18, 2008

      @rhettinger
      Contributor

      The attribute lookup cost can mostly be eliminated if __instancecheck__
      were given a tp slot.

    7. gvanrossum commented on Mar 18, 2008

      @gvanrossum
      Member

      Yeah, but tp_ slots are expensive themselves (mostly in the amount of
      code that needs to be changed -- see typeobject.c).

    8. rhettinger commented on Mar 18, 2008

      @rhettinger
      Contributor

      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.

    9. rhettinger commented on Mar 18, 2008

      @rhettinger
      Contributor

      Attaching a small patch to speed-up one easy case.

    10. theller commented on Apr 2, 2008

      @theller

      Issue bpo-2534 has a patch which speeds up isinstance and issubclass by
      implementing type.__instancecheck__ and type.__subclasscheck__.

    11. gvanrossum commented on Jul 9, 2008

      @gvanrossum
      Member

      Does anyone care about this still?

      I added some comments on
      http://codereview.appspot.com/504

    12. gpshead commented on Jul 13, 2008

      @gpshead
      Member

      % ./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.

    13. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usage

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions