Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690

Closed
orenbenkiki mannequin opened this issue May 29, 2017 · 21 comments · Fixed by #103348
Closed

Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance #74690

orenbenkiki mannequin opened this issue May 29, 2017 · 21 comments · Fixed by #103348
Assignees
Labels
3.12 new features, bugs and security fixes expert-typing performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@orenbenkiki
Copy link
Mannequin

orenbenkiki mannequin commented May 29, 2017

BPO 30505
Nosy @ilevkivskyi, @orenbenkiki

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 = None
closed_at = None
created_at = <Date 2017-05-29.09:16:49.884>
labels = ['library', 'performance']
title = 'Performance of typing._ProtocolMeta._get_protocol_attrs and isinstance'
updated_at = <Date 2017-06-02.17:44:59.311>
user = 'https://github.com/orenbenkiki'

bugs.python.org fields:

activity = <Date 2017-06-02.17:44:59.311>
actor = 'levkivskyi'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2017-05-29.09:16:49.884>
creator = 'orenbenkiki'
dependencies = []
files = []
hgrepos = []
issue_num = 30505
keywords = []
message_count = 2.0
messages = ['294686', '295044']
nosy_count = 2.0
nosy_names = ['levkivskyi', 'orenbenkiki']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'performance'
url = 'https://bugs.python.org/issue30505'
versions = ['Python 3.6']

Linked PRs

@orenbenkiki
Copy link
Mannequin Author

orenbenkiki mannequin commented May 29, 2017

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.

@orenbenkiki orenbenkiki mannequin added stdlib Python modules in the Lib dir performance Performance or resource usage labels May 29, 2017
@ilevkivskyi
Copy link
Member

Thanks for reporting!

The runtime implementation of protocol classes will be thoroughly reworked as a part of PEP-544, see also python/typing#417 for a proof of concept runtime implementation.

Also, there is another ongoing discussion python/typing#432 about a global refactoring of typing module that will significantly improve performance.

Therefore, I would wait with any large PRs until these two stories are settled. If you still want to propose a small PR, you can do this at the upstream typing repo https://github.com/python/typing

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel
Copy link
Member

@orenbenkiki @ilevkivskyi What is the status of this issue, five years on?

@ilevkivskyi
Copy link
Member

TBH I am not sure this issue is still relevant (at least I didn't hear any recent complaints about Protocol performance). @orenbenkiki is performance still bad for your application?

@AlexWaygood
Copy link
Member

I've seen recent complaints about the performance of Protocol from users of the beartype third-party library, and by @posita here. (I'm not sure I've ever seen a proper write-up of the performance problems in a CPython issue, however.)

@ilevkivskyi
Copy link
Member

Hm, actually the short-circuiting proposed in that comment may be a good idea (unless I forgot something) for a simple perf optimization (btw FWIW this is what mypy does internally, it always checks nominal subtypig first).

@AlexWaygood
Copy link
Member

@posita appears to have implemented optimised versions of ProtocolMeta here and here.

(I haven't studied the code for either link, but may be worth looking at!)

@iritkatriel iritkatriel added the 3.12 new features, bugs and security fixes label Sep 7, 2022
@posita
Copy link
Contributor

posita commented Sep 7, 2022

@posita appears to have implemented optimised versions of ProtocolMeta here and here.

(I haven't studied the code for either link, but may be worth looking at!)

For color, the current implementation lives in Beartype and is divided among two classes:

The meat is in _CachingProtocolMeta whereas the Protocol is basically a hack to work around some standard library implementation details and could likely be eliminated. At a high level, _CachingProtocolMeta overrides __instancecheck__ to perform an examination of whether an object "satisfies" a Protocol definition similar to typing._ProtocolMeta.__instancecheck__ and then caches the result by that object's type. Note the asymmetry: the examination is performed on the object, but it is assumed that all objects of the first-examined object's type are consistent. This makes the implementation ill-suited for runtime-composed "types" or monkey-patched objects.

Happy to discuss further, if desired.

@ilevkivskyi
Copy link
Member

Caching may need a more careful consideration (especially w.r.t. monkey-patching classes). I was specifically referring to this idea

class _ProtocolMeta(ABCMeta):
    # …
    def __instancecheck__(cls, instance):
        if super().__instancecheck__(instance):
            # Short circuit for direct inheritors
            return True
        else:
            # … existing implementation that checks method names, etc. …
            return False

@posita Do you have any good code base where you can benchmark this w.r.t. current implementation?

@posita
Copy link
Contributor

posita commented Oct 9, 2022

I can probably do something crude with numerary's and dyce's respective batteries of unit tests. Those are what motivated beartype.typing._typingpep544._CachingProtocolMeta in the first place. Give me a few days and I can probably throw something together.

@posita
Copy link
Contributor

posita commented Oct 11, 2022

Okay, this was a bit more involved than I thought it would be, but I have some rough numbers. My approach was to create a metaclass deriving from typing._ProtocolMeta but implementing its own __instancecheck__ method:

from typing import _ProtocolMeta as ProtocolMeta

class ProtocolMetaShortCircuit(ProtocolMeta):
    def __instancecheck__(cls, instance):
        return super(ProtocolMeta, cls).__instancecheck__(instance) or super(
            ProtocolMetaShortCircuit, cls
        ).__instancecheck__(instance)

This is used to create a special version of the big, scary (non-caching) SupportsLotsOfNumberStuff protocol borrowed from the numerary docs.

@runtime_checkable
class SupportsLotsOfNumberStuffShortCircuit(
    SupportsLotsOfNumberStuff,
    Protocol,
    metaclass=ProtocolMetaShortCircuit,
): pass

# ABC registration
assert isinstance(Fraction(), SupportsLotsOfNumberStuffShortCircuit)
SupportsLotsOfNumberStuffShortCircuit.register(Fraction)

# Direct derivation
class FractionShortCircuit(Fraction, SupportsLotsOfNumberStuffShortCircuit): pass

Initial results:

====
%timeit isinstance(1, SupportsLotsOfNumberStuff)
50 µs ± 616 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(1, SupportsLotsOfNumberStuffShortCircuit)
54.4 µs ± 785 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(2.0, SupportsLotsOfNumberStuff)
48.6 µs ± 464 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(2.0, SupportsLotsOfNumberStuffShortCircuit)
51.3 µs ± 508 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(Decimal('3'), SupportsLotsOfNumberStuff)
48.3 µs ± 288 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(Decimal('3'), SupportsLotsOfNumberStuffShortCircuit)
52.1 µs ± 247 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(Fraction(4, 1), SupportsLotsOfNumberStuff)
52 µs ± 318 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(Fraction(4, 1), SupportsLotsOfNumberStuffShortCircuit)
26.1 µs ± 271 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
====
%timeit isinstance(FractionShortCircuit(5, 1), SupportsLotsOfNumberStuff)
52.7 µs ± 346 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
%timeit isinstance(FractionShortCircuit(5, 1), SupportsLotsOfNumberStuffShortCircuit)
262 ns ± 3.02 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

For built-in types, the short-circuit implementation imposes a small overhead of ~300 ns on my machine (which could likely be reduced), and is about twice as fast¹ for ABCMeta.registered types, but (as anticipated) is way faster for types that explicitly derive from the short-circuiting protocol.

You can play around with the notebook in Binder: Binder 👈 [might take awhile to load]

I tinkered with using this metaclass in my battery of unit tests for numerary and dyce. Anecdotally, I didn't notice much of a difference. I think this is largely because those tests rarely hit cases where this approach is beneficial (even fewer than I had anticipated).


¹ The registry check is likely O(1) due to caching (if I'm reading that implementation right).

AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 30, 2023
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 30, 2023
…` twice in `_ProtocolMeta.__instancecheck__`
AlexWaygood added a commit that referenced this issue Mar 31, 2023
…e in `_ProtocolMeta.__instancecheck__` (#103141)

Speed up `isinstance()` calls against runtime-checkable protocols
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 31, 2023
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 31, 2023
AlexWaygood added a commit that referenced this issue Mar 31, 2023
Improve performance of `isinstance()` checks against runtime-checkable protocols
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Mar 31, 2023
AlexWaygood added a commit that referenced this issue Apr 5, 2023
…nly` at protocol class creation time, not during `isinstance()` checks (#103160)
@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 5, 2023

According to my benchmarks (PGO-optimised, non-debug build on Windows), isinstance() checks against simple runtime-checkable protocols using the main branch are now between 2.5x and 22x as fast as they were on 3.11 (depending on exactly what object you're comparing using isinstance()). However, they're still around 17x slower than isinstance() checks against "normal" classes. (Performance will also be much worse for protocols with lots of members than it is for simple protocols with few members.)

The question is whether isinstance() checks against runtime-checkable protocols are now "fast enough", or whether we want to consider adding a cache to the whole of _ProtocolMeta.__instancecheck__, similar to how beartype does it (and similar to the way ABCMeta.__subclasscheck__ in the stdlib also works). For clarity, caching the whole call would mean that we could end up with this behaviour:

>>> from typing import *
>>> @runtime_checkable
... class HasX(Protocol):
...     x: int
...
>>> class Foo: ...
>>> f = Foo()
>>> isinstance(f, HasX)
False
>>> f.x = 42
>>> isinstance(f, HasX)  # evaluates to `True` on `main`, but would be `False` if we cached the whole call to _ProtocolMeta.__instancecheck__`

The resultant behaviour if we cache the whole call can be pretty surprising, in my opinion -- and it would be a big behaviour change, given that protocols have been in the stdlib for some time now. But, in some ways, the new behaviour would be consistent with the behaviour we already get from ABCMeta.__subclasscheck__, which already does cache the whole call:

>>> from collections.abc import Iterable
>>> class Foo:
...     def __iter__(self):
...         yield from [1, 2, 3]
...
>>> isinstance(Foo(), Iterable)
True
>>> del Foo.__iter__
>>> isinstance(Foo(), Iterable)
True
>>> iter(Foo())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Foo' object is not iterable

I'm curious if @carljm or @hauntsaninja have any thoughts here.

@carljm
Copy link
Contributor

carljm commented Apr 5, 2023

I don't like the cached semantics; they seem surprising to me too. So given that we are already making this a lot faster than it previously was, I'd be inclined to call it fast enough for now. Except: the fact that ABCMeta already does the caching does give me pause. That's clearly the closest analogue to Protocol in the stdlib, and it makes sense to match its behavior. So... I don't know :) I think if I had to make the call, I would leave it as is. But I wouldn't object to either choice.

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 5, 2023

If I were designing this API from scratch, I would probably cache calls to __instancecheck__, since there's a strong precedent with ABCMeta. But _ProtocolMeta has had its current behaviour for 5 years now, and I don't want to be the one to change it to a less intuitive behaviour after 5 years of stable behaviour. So: let's just say that 3.12 is now "fast enough", and leave it at that :)

I'll add NEWS/Whatsnew/docs, and then close this out.

@AlexWaygood
Copy link
Member

For other readers of this issue: note that it is also now clearly documented that runtime-checkable protocols probably shouldn't be used for performance-critical code:

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 6, 2023

I wanted to have accurate numbers to put in Whatsnew, so I did some more benchmarking. Unfortunately, while this isinstance() call is much faster on main than it was in 3.11...

from typing import *
from dataclasses import dataclass

@runtime_checkable
class Foo(Protocol):
    a: int

@dataclass
class Bar:
    a: int

isinstance(Bar(42), Foo)

...This isinstance() call is only a little bit faster...

from typing import *
from dataclasses import dataclass

@runtime_checkable
class Foo(Protocol):
    a: int
    b: int

@dataclass
class Bar:
    a: int
    b: int

isinstance(Bar(42, 42), Foo)

...This one is a little slower...

from typing import *
from dataclasses import dataclass

@runtime_checkable
class Foo(Protocol):
    a: int
    b: int
    c: int

@dataclass
class Bar:
    a: int
    b: int
    c: int

isinstance(Bar(42, 42, 42), Foo)

...And this one is much slower:

from typing import *
from dataclasses import dataclass

@runtime_checkable
class Foo(Protocol):
    a: int
    b: int
    c: int
    d: int

@dataclass
class Bar:
    a: int
    b: int
    c: int
    d: int

isinstance(Bar(42, 42, 42, 42), Foo)

This all makes sense, because the more members the protocol has, the more getattr_static calls we have to do in this loop here:

cpython/Lib/typing.py

Lines 2037 to 2045 in 23cf1e2

for attr in cls.__protocol_attrs__:
try:
val = getattr_static(instance, attr)
except AttributeError:
break
if val is None and callable(getattr(cls, attr, None)):
break
else:
return True

As we know, getattr_static is much slower than getattr, and in 3.11 we just used simple hasattr/getattr calls in this loop.

The good news is that I think most protocols in the wild are generally pretty simple, and don't have very many members. However, this still isn't great.

Ideally, we'd make getattr_static faster (#103193). Unfortunately, I'm out of ideas on that front.

@AlexWaygood
Copy link
Member

Here is a short profiling script with some expensive isinstance() calls:

from typing import *
import cProfile

@runtime_checkable
class Foo(Protocol):
    a: int
    b: int
    c: int
    d: int
    e: int
    f: int
    g: int
    h: int
    i: int
    j: int

class Bar:
    def __init__(self):
        for attrname in 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j':
            setattr(self, attrname, 42)

bars = [Bar() for _ in range(100_000)]

cProfile.run("""\
for bar in bars:
    isinstance(bar, Foo)
"""
)

And here are the profiling results:

         8607409 function calls (8606970 primitive calls) in 6.675 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100000    0.027    0.000    0.050    0.000 <frozen abc>:117(__instancecheck__)
        1    0.000    0.000    0.000    0.000 <frozen abc>:121(__subclasscheck__)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1051(find_spec)
       23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1157(__enter__)
       23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1161(__exit__)
        9    0.000    0.000    0.003    0.000 <frozen importlib._bootstrap>:1185(_find_spec)
      9/1    0.000    0.000    0.017    0.017 <frozen importlib._bootstrap>:1251(_find_and_load_unlocked)
      9/1    0.000    0.000    0.017    0.017 <frozen importlib._bootstrap>:1296(_find_and_load)
        1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1337(_handle_fromlist)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:144(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:216(acquire)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:284(release)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:324(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:328(__enter__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:332(__exit__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:338(_get_module_lock)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:357(cb)
     18/2    0.000    0.000    0.015    0.007 <frozen importlib._bootstrap>:392(_call_with_frames_removed)
      114    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:403(_verbose_message)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:413(_requires_builtin_wrapper)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:48(_new_module)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:511(__init__)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:544(cached)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:557(parent)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:565(has_location)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:574(spec_from_loader)
        9    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap>:645(_init_module_attrs)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:70(__init__)
        9    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap>:718(module_from_spec)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:74(__enter__)
      9/1    0.000    0.000    0.016    0.016 <frozen importlib._bootstrap>:817(_load_unlocked)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:85(__exit__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:888(find_spec)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:912(create_module)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:920(exec_module)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:937(is_package)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1014(create_module)
      7/1    0.000    0.000    0.016    0.016 <frozen importlib._bootstrap_external>:1017(exec_module)
        7    0.000    0.000    0.006    0.001 <frozen importlib._bootstrap_external>:1090(get_code)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1181(__init__)
      105    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:119(<listcomp>)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1206(get_filename)
        7    0.000    0.000    0.003    0.000 <frozen importlib._bootstrap_external>:1211(get_data)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1230(path_stats)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:132(_path_split)
       42    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:134(<genexpr>)
       35    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:140(_path_stat)
       28    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1494(_path_importer_cache)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:150(_path_is_mode_type)
        7    0.000    0.000    0.003    0.000 <frozen importlib._bootstrap_external>:1537(_get_spec)
        7    0.000    0.000    0.003    0.000 <frozen importlib._bootstrap_external>:1569(find_spec)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:159(_path_isfile)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1670(_get_spec)
       21    0.000    0.000    0.003    0.000 <frozen importlib._bootstrap_external>:1675(find_spec)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:172(_path_isabs)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:185(_path_abspath)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:474(cache_from_source)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:603(_get_cached)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:635(_check_name_wrapper)
       21    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:67(_relax_case)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:678(_classify_pyc)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:711(_validate_timestamp_pyc)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:763(_compile_bytecode)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:814(spec_from_file_location)
       21    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:84(_unpack_uint32)
      105    0.001    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:96(_path_join)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        2    0.000    0.000    0.001    0.001 __init__.py:226(compile)
       47    0.000    0.000    0.000    0.000 __init__.py:255(escape)
        2    0.000    0.000    0.001    0.001 __init__.py:280(_compile)
       11    0.001    0.000    0.001    0.000 __init__.py:348(namedtuple)
       67    0.000    0.000    0.000    0.000 __init__.py:422(<genexpr>)
        6    0.000    0.000    0.000    0.000 _compiler.py:214(_compile_charset)
        6    0.000    0.000    0.000    0.000 _compiler.py:241(_optimize_charset)
        1    0.000    0.000    0.000    0.000 _compiler.py:31(_combine_flags)
     10/2    0.000    0.000    0.000    0.000 _compiler.py:37(_compile)
        3    0.000    0.000    0.000    0.000 _compiler.py:384(_mk_bitmap)
        3    0.000    0.000    0.000    0.000 _compiler.py:386(<listcomp>)
        5    0.000    0.000    0.000    0.000 _compiler.py:396(_simple)
        2    0.000    0.000    0.000    0.000 _compiler.py:426(_get_iscased)
        1    0.000    0.000    0.000    0.000 _compiler.py:434(_get_literal_prefix)
        1    0.000    0.000    0.000    0.000 _compiler.py:465(_get_charset_prefix)
        2    0.000    0.000    0.000    0.000 _compiler.py:509(_compile_info)
        4    0.000    0.000    0.000    0.000 _compiler.py:568(isstring)
        2    0.000    0.000    0.000    0.000 _compiler.py:571(_code)
        2    0.000    0.000    0.001    0.001 _compiler.py:738(compile)
       11    0.000    0.000    0.000    0.000 _parser.py:109(__init__)
       25    0.000    0.000    0.000    0.000 _parser.py:158(__len__)
       66    0.000    0.000    0.000    0.000 _parser.py:162(__getitem__)
        6    0.000    0.000    0.000    0.000 _parser.py:166(__setitem__)
       20    0.000    0.000    0.000    0.000 _parser.py:170(append)
     11/3    0.000    0.000    0.000    0.000 _parser.py:172(getwidth)
        2    0.000    0.000    0.000    0.000 _parser.py:222(__init__)
       54    0.000    0.000    0.000    0.000 _parser.py:231(__next)
       40    0.000    0.000    0.000    0.000 _parser.py:247(match)
       47    0.000    0.000    0.000    0.000 _parser.py:252(get)
       17    0.000    0.000    0.000    0.000 _parser.py:284(tell)
        8    0.000    0.000    0.000    0.000 _parser.py:303(_class_escape)
        6    0.000    0.000    0.000    0.000 _parser.py:440(_uniq)
      4/2    0.000    0.000    0.000    0.000 _parser.py:443(_parse_sub)
      5/2    0.000    0.000    0.000    0.000 _parser.py:503(_parse)
        2    0.000    0.000    0.000    0.000 _parser.py:73(__init__)
        6    0.000    0.000    0.000    0.000 _parser.py:79(groups)
        1    0.000    0.000    0.000    0.000 _parser.py:82(opengroup)
        1    0.000    0.000    0.000    0.000 _parser.py:94(closegroup)
        2    0.000    0.000    0.000    0.000 _parser.py:944(fix_flags)
        2    0.000    0.000    0.001    0.000 _parser.py:960(parse)
        1    0.000    0.000    0.002    0.002 ast.py:1(<module>)
        1    0.000    0.000    0.000    0.000 ast.py:395(NodeVisitor)
        1    0.000    0.000    0.000    0.000 ast.py:453(NodeTransformer)
        1    0.000    0.000    0.000    0.000 ast.py:527(_ABC)
        5    0.000    0.000    0.000    0.000 ast.py:529(__init__)
        1    0.000    0.000    0.000    0.000 ast.py:559(Num)
        1    0.000    0.000    0.000    0.000 ast.py:563(Str)
        1    0.000    0.000    0.000    0.000 ast.py:567(Bytes)
        1    0.000    0.000    0.000    0.000 ast.py:571(NameConstant)
        1    0.000    0.000    0.000    0.000 ast.py:574(Ellipsis)
        1    0.000    0.000    0.000    0.000 ast.py:604(slice)
        1    0.000    0.000    0.000    0.000 ast.py:607(Index)
        1    0.000    0.000    0.000    0.000 ast.py:612(ExtSlice)
        1    0.000    0.000    0.000    0.000 ast.py:631(Suite)
        1    0.000    0.000    0.000    0.000 ast.py:634(AugLoad)
        1    0.000    0.000    0.000    0.000 ast.py:637(AugStore)
        1    0.000    0.000    0.000    0.000 ast.py:640(Param)
        1    0.000    0.000    0.000    0.000 ast.py:648(_Precedence)
        1    0.000    0.000    0.001    0.001 ast.py:684(_Unparser)
        3    0.000    0.000    0.001    0.000 contextlib.py:260(contextmanager)
        1    0.000    0.000    0.003    0.003 dis.py:1(<module>)
        1    0.000    0.000    0.000    0.000 dis.py:160(_Unknown)
        1    0.000    0.000    0.000    0.000 dis.py:283(Instruction)
        1    0.000    0.000    0.000    0.000 dis.py:49(<listcomp>)
        1    0.000    0.000    0.000    0.000 dis.py:55(<dictcomp>)
        1    0.000    0.000    0.000    0.000 dis.py:709(Bytecode)
        1    0.000    0.000    0.000    0.000 enum.py:1001(_find_new_)
        4    0.000    0.000    0.000    0.000 enum.py:1095(__new__)
       23    0.000    0.000    0.000    0.000 enum.py:1146(__init__)
       18    0.000    0.000    0.000    0.000 enum.py:1149(_generate_next_value_)
        2    0.000    0.000    0.000    0.000 enum.py:1248(value)
        4    0.000    0.000    0.000    0.000 enum.py:1506(__and__)
        1    0.000    0.000    0.000    0.000 enum.py:1635(_simple_enum)
        1    0.000    0.000    0.000    0.000 enum.py:1651(convert_class)
       18    0.000    0.000    0.000    0.000 enum.py:177(__init__)
       21    0.000    0.000    0.000    0.000 enum.py:194(__get__)
       19    0.000    0.000    0.000    0.000 enum.py:230(__set_name__)
        5    0.000    0.000    0.000    0.000 enum.py:240(__init__)
        5    0.000    0.000    0.000    0.000 enum.py:243(__set_name__)
        1    0.000    0.000    0.000    0.000 enum.py:352(__init__)
       10    0.000    0.000    0.000    0.000 enum.py:359(__setitem__)
       25    0.000    0.000    0.000    0.000 enum.py:37(_is_descriptor)
       31    0.000    0.000    0.000    0.000 enum.py:47(_is_dunder)
        1    0.000    0.000    0.000    0.000 enum.py:476(__prepare__)
        2    0.000    0.000    0.000    0.000 enum.py:491(__new__)
       30    0.000    0.000    0.000    0.000 enum.py:58(_is_sunder)
       10    0.000    0.000    0.000    0.000 enum.py:69(_is_internal_class)
        4    0.000    0.000    0.000    0.000 enum.py:699(__call__)
        9    0.000    0.000    0.000    0.000 enum.py:753(__delattr__)
       30    0.000    0.000    0.000    0.000 enum.py:78(_is_private)
        5    0.000    0.000    0.000    0.000 enum.py:796(__members__)
       29    0.000    0.000    0.000    0.000 enum.py:818(__setattr__)
        1    0.000    0.000    0.000    0.000 enum.py:924(_check_for_existing_members_)
        2    0.000    0.000    0.000    0.000 enum.py:934(_get_mixins_)
        1    0.000    0.000    0.000    0.000 enum.py:953(_find_data_repr_)
        2    0.000    0.000    0.000    0.000 enum.py:975(_find_data_type_)
        4    0.001    0.000    0.001    0.000 functools.py:35(update_wrapper)
        1    0.000    0.000    0.000    0.000 functools.py:479(lru_cache)
        3    0.000    0.000    0.000    0.000 functools.py:65(wraps)
        1    0.000    0.000    0.015    0.015 inspect.py:1(<module>)
        1    0.000    0.000    0.000    0.000 inspect.py:1031(ClassFoundException)
        1    0.000    0.000    0.000    0.000 inspect.py:1035(_ClassFinder)
        1    0.000    0.000    0.000    0.000 inspect.py:1179(EndOfBlock)
        1    0.000    0.000    0.000    0.000 inspect.py:1181(BlockFinder)
        1    0.000    0.000    0.000    0.000 inspect.py:1649(Traceback)
        1    0.000    0.000    0.000    0.000 inspect.py:1721(FrameInfo)
  1000000    0.477    0.000    0.569    0.000 inspect.py:1779(_check_instance)
  1000000    1.237    0.000    2.885    0.000 inspect.py:1788(_check_class)
  1000000    0.651    0.000    0.651    0.000 inspect.py:1797(_is_type)
  3000000    2.513    0.000    2.513    0.000 inspect.py:1804(_shadowed_dict)
  1000000    1.184    0.000    6.154    0.000 inspect.py:1817(getattr_static)
        1    0.000    0.000    0.000    0.000 inspect.py:2657(_void)
        1    0.000    0.000    0.000    0.000 inspect.py:2661(_empty)
        1    0.000    0.000    0.000    0.000 inspect.py:2665(_ParameterKind)
        5    0.000    0.000    0.000    0.000 inspect.py:2672(__new__)
        1    0.000    0.000    0.000    0.000 inspect.py:2689(Parameter)
        1    0.000    0.000    0.000    0.000 inspect.py:2847(BoundArguments)
        1    0.000    0.000    0.000    0.000 inspect.py:2977(Signature)
        1    0.000    0.000    0.004    0.004 linecache.py:1(<module>)
        1    0.000    0.000    0.001    0.001 opcode.py:1(<module>)
        1    0.000    0.000    0.000    0.000 opcode.py:224(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:245(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:373(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:422(<listcomp>)
      112    0.000    0.000    0.000    0.000 opcode.py:51(def_op)
       11    0.000    0.000    0.000    0.000 opcode.py:54(name_op)
        9    0.000    0.000    0.000    0.000 opcode.py:58(jrel_op)
        7    0.000    0.000    0.000    0.000 opcode.py:66(pseudo_op)
       63    0.000    0.000    0.000    0.000 opcode.py:71(<listcomp>)
        1    0.000    0.000    0.000    0.000 token.py:1(<module>)
        1    0.000    0.000    0.000    0.000 token.py:75(<dictcomp>)
        1    0.000    0.000    0.003    0.003 tokenize.py:1(<module>)
        1    0.000    0.000    0.000    0.000 tokenize.py:161(TokenError)
        1    0.000    0.000    0.000    0.000 tokenize.py:163(StopTokenizing)
        1    0.000    0.000    0.000    0.000 tokenize.py:166(Untokenizer)
        1    0.000    0.000    0.000    0.000 tokenize.py:46(TokenInfo)
       19    0.000    0.000    0.000    0.000 tokenize.py:59(group)
        1    0.000    0.000    0.000    0.000 tokenize.py:60(any)
        2    0.000    0.000    0.000    0.000 tokenize.py:61(maybe)
        3    0.000    0.000    0.000    0.000 tokenize.py:84(_all_string_prefixes)
       24    0.000    0.000    0.000    0.000 tokenize.py:95(<listcomp>)
       19    0.000    0.000    0.000    0.000 types.py:163(__init__)
        1    0.000    0.000    0.000    0.000 typing.py:1968(_caller)
        1    0.000    0.000    0.000    0.000 typing.py:1979(_allow_reckless_class_checks)
        1    0.000    0.000    0.017    0.017 typing.py:1997(_lazy_load_getattr_static)
   100000    0.385    0.000    6.625    0.000 typing.py:2020(__instancecheck__)
        1    0.000    0.000    0.000    0.000 typing.py:2092(_proto_hook)
    25/20    0.000    0.000    0.000    0.000 {built-in method __new__ of type object at 0x00007FFF949AC400}
   100000    0.022    0.000    0.022    0.000 {built-in method _abc._abc_instancecheck}
        1    0.000    0.000    0.000    0.000 {built-in method _abc._abc_subclasscheck}
        7    0.000    0.000    0.000    0.000 {built-in method _imp._fix_co_filename}
       41    0.000    0.000    0.000    0.000 {built-in method _imp.acquire_lock}
        2    0.000    0.000    0.000    0.000 {built-in method _imp.create_builtin}
        2    0.000    0.000    0.000    0.000 {built-in method _imp.exec_builtin}
        7    0.000    0.000    0.000    0.000 {built-in method _imp.find_frozen}
        9    0.000    0.000    0.000    0.000 {built-in method _imp.is_builtin}
       41    0.000    0.000    0.000    0.000 {built-in method _imp.release_lock}
        2    0.000    0.000    0.000    0.000 {built-in method _sre.compile}
        9    0.000    0.000    0.000    0.000 {built-in method _thread.allocate_lock}
       18    0.000    0.000    0.000    0.000 {built-in method _thread.get_ident}
       36    0.001    0.000    0.001    0.000 {built-in method builtins.__build_class__}
        6    0.000    0.000    0.000    0.000 {built-in method builtins.all}
       63    0.000    0.000    0.000    0.000 {built-in method builtins.any}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.callable}
        9    0.000    0.000    0.000    0.000 {built-in method builtins.delattr}
       11    0.001    0.000    0.001    0.000 {built-in method builtins.eval}
      8/1    0.000    0.000    6.705    6.705 {built-in method builtins.exec}
   200128    0.020    0.000    0.020    0.000 {built-in method builtins.getattr}
        2    0.000    0.000    0.000    0.000 {built-in method builtins.globals}
      125    0.000    0.000    0.000    0.000 {built-in method builtins.hasattr}
100348/100000    0.050    0.000    6.675    0.000 {built-in method builtins.isinstance}
        9    0.000    0.000    0.000    0.000 {built-in method builtins.issubclass}
  589/577    0.000    0.000    0.000    0.000 {built-in method builtins.len}
       16    0.000    0.000    0.000    0.000 {built-in method builtins.max}
       23    0.000    0.000    0.000    0.000 {built-in method builtins.min}
       15    0.000    0.000    0.000    0.000 {built-in method builtins.ord}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.repr}
       47    0.000    0.000    0.000    0.000 {built-in method builtins.setattr}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.sorted}
      256    0.000    0.000    0.000    0.000 {built-in method builtins.sum}
       21    0.000    0.000    0.000    0.000 {built-in method from_bytes}
        6    0.000    0.000    0.000    0.000 {built-in method fromkeys}
        7    0.003    0.000    0.003    0.000 {built-in method io.open_code}
        7    0.002    0.000    0.002    0.000 {built-in method marshal.loads}
        7    0.000    0.000    0.000    0.000 {built-in method nt._path_splitroot}
       21    0.000    0.000    0.000    0.000 {built-in method nt.fspath}
       35    0.001    0.000    0.001    0.000 {built-in method nt.stat}
       12    0.000    0.000    0.000    0.000 {built-in method sys._getframemodulename}
       67    0.000    0.000    0.000    0.000 {built-in method sys.intern}
       67    0.000    0.000    0.000    0.000 {method '__contains__' of 'frozenset' objects}
        7    0.000    0.000    0.000    0.000 {method '__exit__' of '_io._IOBase' objects}
       18    0.000    0.000    0.000    0.000 {method '__exit__' of '_thread.RLock' objects}
      232    0.000    0.000    0.000    0.000 {method 'add' of 'set' objects}
      391    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'bit_length' of 'int' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
      357    0.000    0.000    0.000    0.000 {method 'endswith' of 'str' objects}
        7    0.000    0.000    0.000    0.000 {method 'extend' of 'list' objects}
       28    0.000    0.000    0.000    0.000 {method 'find' of 'bytearray' objects}
  1000294    0.092    0.000    0.092    0.000 {method 'get' of 'dict' objects}
       57    0.000    0.000    0.000    0.000 {method 'get' of 'mappingproxy' objects}
       67    0.000    0.000    0.000    0.000 {method 'isidentifier' of 'str' objects}
        8    0.000    0.000    0.000    0.000 {method 'items' of 'dict' objects}
        1    0.000    0.000    0.000    0.000 {method 'items' of 'mappingproxy' objects}
      232    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
       13    0.000    0.000    0.000    0.000 {method 'pop' of 'dict' objects}
        9    0.000    0.000    0.000    0.000 {method 'pop' of 'list' objects}
        2    0.000    0.000    0.000    0.000 {method 'pop' of 'set' objects}
        7    0.000    0.000    0.000    0.000 {method 'read' of '_io.BufferedReader' objects}
        9    0.000    0.000    0.000    0.000 {method 'remove' of 'list' objects}
       15    0.000    0.000    0.000    0.000 {method 'replace' of 'str' objects}
       28    0.000    0.000    0.000    0.000 {method 'rfind' of 'str' objects}
       53    0.000    0.000    0.000    0.000 {method 'rpartition' of 'str' objects}
      329    0.000    0.000    0.000    0.000 {method 'rstrip' of 'str' objects}
       15    0.000    0.000    0.000    0.000 {method 'setdefault' of 'dict' objects}
       17    0.000    0.000    0.000    0.000 {method 'sort' of 'list' objects}
        8    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
      618    0.000    0.000    0.000    0.000 {method 'startswith' of 'str' objects}
        3    0.000    0.000    0.000    0.000 {method 'translate' of 'bytearray' objects}
       47    0.000    0.000    0.000    0.000 {method 'translate' of 'str' objects}
        5    0.000    0.000    0.000    0.000 {method 'update' of 'dict' objects}
       36    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}
      259    0.000    0.000    0.000    0.000 {method 'values' of 'dict' objects}

We're spending a lot of time in inspect._shadowed_dict().

@AlexWaygood
Copy link
Member

Following #103318, we're now faster than 3.11 for protocols with 4 members or fewer (but slower than 3.11 for protocols with 5+ members).

New profiling results:
         8607409 function calls (8606970 primitive calls) in 5.621 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100000    0.026    0.000    0.045    0.000 <frozen abc>:117(__instancecheck__)
        1    0.000    0.000    0.000    0.000 <frozen abc>:121(__subclasscheck__)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1051(find_spec)
       23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1157(__enter__)
       23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1161(__exit__)
        9    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap>:1185(_find_spec)
      9/1    0.000    0.000    0.013    0.013 <frozen importlib._bootstrap>:1251(_find_and_load_unlocked)
      9/1    0.000    0.000    0.013    0.013 <frozen importlib._bootstrap>:1296(_find_and_load)
        1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1337(_handle_fromlist)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:144(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:216(acquire)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:284(release)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:324(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:328(__enter__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:332(__exit__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:338(_get_module_lock)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:357(cb)
     18/2    0.000    0.000    0.011    0.006 <frozen importlib._bootstrap>:392(_call_with_frames_removed)
      114    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:403(_verbose_message)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:413(_requires_builtin_wrapper)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:48(_new_module)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:511(__init__)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:544(cached)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:557(parent)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:565(has_location)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:574(spec_from_loader)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:645(_init_module_attrs)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:70(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:718(module_from_spec)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:74(__enter__)
      9/1    0.000    0.000    0.013    0.013 <frozen importlib._bootstrap>:817(_load_unlocked)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:85(__exit__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:888(find_spec)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:912(create_module)
        2    0.000    0.000    0.001    0.001 <frozen importlib._bootstrap>:920(exec_module)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:937(is_package)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1014(create_module)
      7/1    0.000    0.000    0.012    0.012 <frozen importlib._bootstrap_external>:1017(exec_module)
        7    0.000    0.000    0.004    0.001 <frozen importlib._bootstrap_external>:1090(get_code)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1181(__init__)
      105    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:119(<listcomp>)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1206(get_filename)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1211(get_data)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1230(path_stats)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:132(_path_split)
       42    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:134(<genexpr>)
       35    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:140(_path_stat)
       28    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1494(_path_importer_cache)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:150(_path_is_mode_type)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1537(_get_spec)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1569(find_spec)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:159(_path_isfile)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1670(_get_spec)
       21    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1675(find_spec)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:172(_path_isabs)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:185(_path_abspath)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:474(cache_from_source)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:603(_get_cached)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:635(_check_name_wrapper)
       21    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:67(_relax_case)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:678(_classify_pyc)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:711(_validate_timestamp_pyc)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:763(_compile_bytecode)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:814(spec_from_file_location)
       21    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:84(_unpack_uint32)
      105    0.001    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:96(_path_join)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        2    0.000    0.000    0.001    0.000 __init__.py:226(compile)
       47    0.000    0.000    0.000    0.000 __init__.py:255(escape)
        2    0.000    0.000    0.001    0.000 __init__.py:280(_compile)
       11    0.000    0.000    0.002    0.000 __init__.py:348(namedtuple)
       67    0.000    0.000    0.000    0.000 __init__.py:422(<genexpr>)
        6    0.000    0.000    0.000    0.000 _compiler.py:214(_compile_charset)
        6    0.000    0.000    0.000    0.000 _compiler.py:241(_optimize_charset)
        1    0.000    0.000    0.000    0.000 _compiler.py:31(_combine_flags)
     10/2    0.000    0.000    0.000    0.000 _compiler.py:37(_compile)
        3    0.000    0.000    0.000    0.000 _compiler.py:384(_mk_bitmap)
        3    0.000    0.000    0.000    0.000 _compiler.py:386(<listcomp>)
        5    0.000    0.000    0.000    0.000 _compiler.py:396(_simple)
        2    0.000    0.000    0.000    0.000 _compiler.py:426(_get_iscased)
        1    0.000    0.000    0.000    0.000 _compiler.py:434(_get_literal_prefix)
        1    0.000    0.000    0.000    0.000 _compiler.py:465(_get_charset_prefix)
        2    0.000    0.000    0.000    0.000 _compiler.py:509(_compile_info)
        4    0.000    0.000    0.000    0.000 _compiler.py:568(isstring)
        2    0.000    0.000    0.000    0.000 _compiler.py:571(_code)
        2    0.000    0.000    0.001    0.000 _compiler.py:738(compile)
       11    0.000    0.000    0.000    0.000 _parser.py:109(__init__)
       25    0.000    0.000    0.000    0.000 _parser.py:158(__len__)
       66    0.000    0.000    0.000    0.000 _parser.py:162(__getitem__)
        6    0.000    0.000    0.000    0.000 _parser.py:166(__setitem__)
       20    0.000    0.000    0.000    0.000 _parser.py:170(append)
     11/3    0.000    0.000    0.000    0.000 _parser.py:172(getwidth)
        2    0.000    0.000    0.000    0.000 _parser.py:222(__init__)
       54    0.000    0.000    0.000    0.000 _parser.py:231(__next)
       40    0.000    0.000    0.000    0.000 _parser.py:247(match)
       47    0.000    0.000    0.000    0.000 _parser.py:252(get)
       17    0.000    0.000    0.000    0.000 _parser.py:284(tell)
        8    0.000    0.000    0.000    0.000 _parser.py:303(_class_escape)
        6    0.000    0.000    0.000    0.000 _parser.py:440(_uniq)
      4/2    0.000    0.000    0.000    0.000 _parser.py:443(_parse_sub)
      5/2    0.000    0.000    0.000    0.000 _parser.py:503(_parse)
        2    0.000    0.000    0.000    0.000 _parser.py:73(__init__)
        6    0.000    0.000    0.000    0.000 _parser.py:79(groups)
        1    0.000    0.000    0.000    0.000 _parser.py:82(opengroup)
        1    0.000    0.000    0.000    0.000 _parser.py:94(closegroup)
        2    0.000    0.000    0.000    0.000 _parser.py:944(fix_flags)
        2    0.000    0.000    0.000    0.000 _parser.py:960(parse)
        1    0.000    0.000    0.002    0.002 ast.py:1(<module>)
        1    0.000    0.000    0.000    0.000 ast.py:395(NodeVisitor)
        1    0.000    0.000    0.000    0.000 ast.py:453(NodeTransformer)
        1    0.000    0.000    0.000    0.000 ast.py:527(_ABC)
        5    0.000    0.000    0.000    0.000 ast.py:529(__init__)
        1    0.000    0.000    0.000    0.000 ast.py:559(Num)
        1    0.000    0.000    0.000    0.000 ast.py:563(Str)
        1    0.000    0.000    0.000    0.000 ast.py:567(Bytes)
        1    0.000    0.000    0.000    0.000 ast.py:571(NameConstant)
        1    0.000    0.000    0.000    0.000 ast.py:574(Ellipsis)
        1    0.000    0.000    0.000    0.000 ast.py:604(slice)
        1    0.000    0.000    0.000    0.000 ast.py:607(Index)
        1    0.000    0.000    0.000    0.000 ast.py:612(ExtSlice)
        1    0.000    0.000    0.000    0.000 ast.py:631(Suite)
        1    0.000    0.000    0.000    0.000 ast.py:634(AugLoad)
        1    0.000    0.000    0.000    0.000 ast.py:637(AugStore)
        1    0.000    0.000    0.000    0.000 ast.py:640(Param)
        1    0.000    0.000    0.000    0.000 ast.py:648(_Precedence)
        1    0.000    0.000    0.000    0.000 ast.py:684(_Unparser)
        3    0.000    0.000    0.000    0.000 contextlib.py:260(contextmanager)
        1    0.000    0.000    0.002    0.002 dis.py:1(<module>)
        1    0.000    0.000    0.000    0.000 dis.py:160(_Unknown)
        1    0.000    0.000    0.000    0.000 dis.py:283(Instruction)
        1    0.000    0.000    0.000    0.000 dis.py:49(<listcomp>)
        1    0.000    0.000    0.000    0.000 dis.py:55(<dictcomp>)
        1    0.000    0.000    0.000    0.000 dis.py:709(Bytecode)
        1    0.000    0.000    0.000    0.000 enum.py:1001(_find_new_)
        4    0.000    0.000    0.000    0.000 enum.py:1095(__new__)
       23    0.000    0.000    0.000    0.000 enum.py:1146(__init__)
       18    0.000    0.000    0.000    0.000 enum.py:1149(_generate_next_value_)
        2    0.000    0.000    0.000    0.000 enum.py:1248(value)
        4    0.000    0.000    0.000    0.000 enum.py:1506(__and__)
        1    0.000    0.000    0.000    0.000 enum.py:1635(_simple_enum)
        1    0.000    0.000    0.000    0.000 enum.py:1651(convert_class)
       18    0.000    0.000    0.000    0.000 enum.py:177(__init__)
       21    0.000    0.000    0.000    0.000 enum.py:194(__get__)
       19    0.000    0.000    0.000    0.000 enum.py:230(__set_name__)
        5    0.000    0.000    0.000    0.000 enum.py:240(__init__)
        5    0.000    0.000    0.000    0.000 enum.py:243(__set_name__)
        1    0.000    0.000    0.000    0.000 enum.py:352(__init__)
       10    0.000    0.000    0.000    0.000 enum.py:359(__setitem__)
       25    0.000    0.000    0.000    0.000 enum.py:37(_is_descriptor)
       31    0.000    0.000    0.000    0.000 enum.py:47(_is_dunder)
        1    0.000    0.000    0.000    0.000 enum.py:476(__prepare__)
        2    0.000    0.000    0.000    0.000 enum.py:491(__new__)
       30    0.000    0.000    0.000    0.000 enum.py:58(_is_sunder)
       10    0.000    0.000    0.000    0.000 enum.py:69(_is_internal_class)
        4    0.000    0.000    0.000    0.000 enum.py:699(__call__)
        9    0.000    0.000    0.000    0.000 enum.py:753(__delattr__)
       30    0.000    0.000    0.000    0.000 enum.py:78(_is_private)
        5    0.000    0.000    0.000    0.000 enum.py:796(__members__)
       29    0.000    0.000    0.000    0.000 enum.py:818(__setattr__)
        1    0.000    0.000    0.000    0.000 enum.py:924(_check_for_existing_members_)
        2    0.000    0.000    0.000    0.000 enum.py:934(_get_mixins_)
        1    0.000    0.000    0.000    0.000 enum.py:953(_find_data_repr_)
        2    0.000    0.000    0.000    0.000 enum.py:975(_find_data_type_)
        4    0.000    0.000    0.000    0.000 functools.py:35(update_wrapper)
        1    0.000    0.000    0.000    0.000 functools.py:479(lru_cache)
        3    0.000    0.000    0.000    0.000 functools.py:65(wraps)
        1    0.000    0.000    0.011    0.011 inspect.py:1(<module>)
        1    0.000    0.000    0.000    0.000 inspect.py:1031(ClassFoundException)
        1    0.000    0.000    0.000    0.000 inspect.py:1035(_ClassFinder)
        1    0.000    0.000    0.000    0.000 inspect.py:1179(EndOfBlock)
        1    0.000    0.000    0.000    0.000 inspect.py:1181(BlockFinder)
        1    0.000    0.000    0.000    0.000 inspect.py:1649(Traceback)
        1    0.000    0.000    0.000    0.000 inspect.py:1721(FrameInfo)
  1000000    0.445    0.000    0.534    0.000 inspect.py:1779(_check_instance)
  1000000    0.883    0.000    2.169    0.000 inspect.py:1788(_check_class)
  1000000    0.629    0.000    0.629    0.000 inspect.py:1794(_is_type)
  3000000    1.986    0.000    1.986    0.000 inspect.py:1801(_shadowed_dict)
  1000000    1.120    0.000    5.152    0.000 inspect.py:1812(getattr_static)
        1    0.000    0.000    0.000    0.000 inspect.py:2652(_void)
        1    0.000    0.000    0.000    0.000 inspect.py:2656(_empty)
        1    0.000    0.000    0.000    0.000 inspect.py:2660(_ParameterKind)
        5    0.000    0.000    0.000    0.000 inspect.py:2667(__new__)
        1    0.000    0.000    0.000    0.000 inspect.py:2684(Parameter)
        1    0.000    0.000    0.000    0.000 inspect.py:2842(BoundArguments)
        1    0.000    0.000    0.000    0.000 inspect.py:2972(Signature)
        1    0.000    0.000    0.003    0.003 linecache.py:1(<module>)
        1    0.000    0.000    0.000    0.000 opcode.py:1(<module>)
        1    0.000    0.000    0.000    0.000 opcode.py:224(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:245(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:373(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:422(<listcomp>)
      112    0.000    0.000    0.000    0.000 opcode.py:51(def_op)
       11    0.000    0.000    0.000    0.000 opcode.py:54(name_op)
        9    0.000    0.000    0.000    0.000 opcode.py:58(jrel_op)
        7    0.000    0.000    0.000    0.000 opcode.py:66(pseudo_op)
       63    0.000    0.000    0.000    0.000 opcode.py:71(<listcomp>)
        1    0.000    0.000    0.000    0.000 token.py:1(<module>)
        1    0.000    0.000    0.000    0.000 token.py:75(<dictcomp>)
        1    0.000    0.000    0.002    0.002 tokenize.py:1(<module>)
        1    0.000    0.000    0.000    0.000 tokenize.py:161(TokenError)
        1    0.000    0.000    0.000    0.000 tokenize.py:163(StopTokenizing)
        1    0.000    0.000    0.000    0.000 tokenize.py:166(Untokenizer)
        1    0.000    0.000    0.000    0.000 tokenize.py:46(TokenInfo)
       19    0.000    0.000    0.000    0.000 tokenize.py:59(group)
        1    0.000    0.000    0.000    0.000 tokenize.py:60(any)
        2    0.000    0.000    0.000    0.000 tokenize.py:61(maybe)
        3    0.000    0.000    0.000    0.000 tokenize.py:84(_all_string_prefixes)
       24    0.000    0.000    0.000    0.000 tokenize.py:95(<listcomp>)
       19    0.000    0.000    0.000    0.000 types.py:163(__init__)
        1    0.000    0.000    0.000    0.000 typing.py:1968(_caller)
        1    0.000    0.000    0.000    0.000 typing.py:1979(_allow_reckless_class_checks)
        1    0.000    0.000    0.013    0.013 typing.py:1997(_lazy_load_getattr_static)
   100000    0.350    0.000    5.578    0.000 typing.py:2020(__instancecheck__)
        1    0.000    0.000    0.000    0.000 typing.py:2092(_proto_hook)
    25/20    0.000    0.000    0.000    0.000 {built-in method __new__ of type object at 0x00007FFFB2E1C400}
   100000    0.019    0.000    0.019    0.000 {built-in method _abc._abc_instancecheck}
        1    0.000    0.000    0.000    0.000 {built-in method _abc._abc_subclasscheck}
        7    0.000    0.000    0.000    0.000 {built-in method _imp._fix_co_filename}
       41    0.000    0.000    0.000    0.000 {built-in method _imp.acquire_lock}
        2    0.000    0.000    0.000    0.000 {built-in method _imp.create_builtin}
        2    0.001    0.000    0.001    0.000 {built-in method _imp.exec_builtin}
        7    0.000    0.000    0.000    0.000 {built-in method _imp.find_frozen}
        9    0.000    0.000    0.000    0.000 {built-in method _imp.is_builtin}
       41    0.000    0.000    0.000    0.000 {built-in method _imp.release_lock}
        2    0.000    0.000    0.000    0.000 {built-in method _sre.compile}
        9    0.000    0.000    0.000    0.000 {built-in method _thread.allocate_lock}
       18    0.000    0.000    0.000    0.000 {built-in method _thread.get_ident}
       36    0.000    0.000    0.001    0.000 {built-in method builtins.__build_class__}
        6    0.000    0.000    0.000    0.000 {built-in method builtins.all}
       63    0.000    0.000    0.000    0.000 {built-in method builtins.any}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.callable}
        9    0.000    0.000    0.000    0.000 {built-in method builtins.delattr}
       11    0.001    0.000    0.001    0.000 {built-in method builtins.eval}
      8/1    0.000    0.000    5.648    5.648 {built-in method builtins.exec}
   200128    0.019    0.000    0.019    0.000 {built-in method builtins.getattr}
        2    0.000    0.000    0.000    0.000 {built-in method builtins.globals}
      125    0.000    0.000    0.000    0.000 {built-in method builtins.hasattr}
100348/100000    0.043    0.000    5.621    0.000 {built-in method builtins.isinstance}
        9    0.000    0.000    0.000    0.000 {built-in method builtins.issubclass}
  589/577    0.000    0.000    0.000    0.000 {built-in method builtins.len}
       16    0.000    0.000    0.000    0.000 {built-in method builtins.max}
       23    0.000    0.000    0.000    0.000 {built-in method builtins.min}
       15    0.000    0.000    0.000    0.000 {built-in method builtins.ord}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.repr}
       47    0.000    0.000    0.000    0.000 {built-in method builtins.setattr}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.sorted}
      256    0.000    0.000    0.000    0.000 {built-in method builtins.sum}
       21    0.000    0.000    0.000    0.000 {built-in method from_bytes}
        6    0.000    0.000    0.000    0.000 {built-in method fromkeys}
        7    0.002    0.000    0.002    0.000 {built-in method io.open_code}
        7    0.002    0.000    0.002    0.000 {built-in method marshal.loads}
        7    0.000    0.000    0.000    0.000 {built-in method nt._path_splitroot}
       21    0.000    0.000    0.000    0.000 {built-in method nt.fspath}
       35    0.001    0.000    0.001    0.000 {built-in method nt.stat}
       12    0.000    0.000    0.000    0.000 {built-in method sys._getframemodulename}
       67    0.000    0.000    0.000    0.000 {built-in method sys.intern}
       67    0.000    0.000    0.000    0.000 {method '__contains__' of 'frozenset' objects}
        7    0.000    0.000    0.000    0.000 {method '__exit__' of '_io._IOBase' objects}
       18    0.000    0.000    0.000    0.000 {method '__exit__' of '_thread.RLock' objects}
      232    0.000    0.000    0.000    0.000 {method 'add' of 'set' objects}
      391    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'bit_length' of 'int' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
      357    0.000    0.000    0.000    0.000 {method 'endswith' of 'str' objects}
        7    0.000    0.000    0.000    0.000 {method 'extend' of 'list' objects}
       28    0.000    0.000    0.000    0.000 {method 'find' of 'bytearray' objects}
  1000294    0.088    0.000    0.088    0.000 {method 'get' of 'dict' objects}
       57    0.000    0.000    0.000    0.000 {method 'get' of 'mappingproxy' objects}
       67    0.000    0.000    0.000    0.000 {method 'isidentifier' of 'str' objects}
        8    0.000    0.000    0.000    0.000 {method 'items' of 'dict' objects}
        1    0.000    0.000    0.000    0.000 {method 'items' of 'mappingproxy' objects}
      232    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
       13    0.000    0.000    0.000    0.000 {method 'pop' of 'dict' objects}
        9    0.000    0.000    0.000    0.000 {method 'pop' of 'list' objects}
        2    0.000    0.000    0.000    0.000 {method 'pop' of 'set' objects}
        7    0.000    0.000    0.000    0.000 {method 'read' of '_io.BufferedReader' objects}
        9    0.000    0.000    0.000    0.000 {method 'remove' of 'list' objects}
       15    0.000    0.000    0.000    0.000 {method 'replace' of 'str' objects}
       28    0.000    0.000    0.000    0.000 {method 'rfind' of 'str' objects}
       53    0.000    0.000    0.000    0.000 {method 'rpartition' of 'str' objects}
      329    0.000    0.000    0.000    0.000 {method 'rstrip' of 'str' objects}
       15    0.000    0.000    0.000    0.000 {method 'setdefault' of 'dict' objects}
       17    0.000    0.000    0.000    0.000 {method 'sort' of 'list' objects}
        8    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
      618    0.000    0.000    0.000    0.000 {method 'startswith' of 'str' objects}
        3    0.000    0.000    0.000    0.000 {method 'translate' of 'bytearray' objects}
       47    0.000    0.000    0.000    0.000 {method 'translate' of 'str' objects}
        5    0.000    0.000    0.000    0.000 {method 'update' of 'dict' objects}
       36    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}
      259    0.000    0.000    0.000    0.000 {method 'values' of 'dict' objects}

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 6, 2023

Following #103321, we're now faster than 3.11 for protocols with 6 members or fewer (but slower than 3.11 for protocols with 7+ members).

New profiling results:
         7607409 function calls (7606970 primitive calls) in 4.843 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   100000    0.024    0.000    0.042    0.000 <frozen abc>:117(__instancecheck__)
        1    0.000    0.000    0.000    0.000 <frozen abc>:121(__subclasscheck__)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1051(find_spec)
       23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1157(__enter__)
       23    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1161(__exit__)
        9    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap>:1185(_find_spec)
      9/1    0.000    0.000    0.014    0.014 <frozen importlib._bootstrap>:1251(_find_and_load_unlocked)
      9/1    0.000    0.000    0.014    0.014 <frozen importlib._bootstrap>:1296(_find_and_load)
        1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:1337(_handle_fromlist)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:144(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:216(acquire)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:284(release)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:324(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:328(__enter__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:332(__exit__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:338(_get_module_lock)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:357(cb)
     18/2    0.000    0.000    0.012    0.006 <frozen importlib._bootstrap>:392(_call_with_frames_removed)
      114    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:403(_verbose_message)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:413(_requires_builtin_wrapper)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:48(_new_module)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:511(__init__)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:544(cached)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:557(parent)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:565(has_location)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:574(spec_from_loader)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:645(_init_module_attrs)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:70(__init__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:718(module_from_spec)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:74(__enter__)
      9/1    0.000    0.000    0.014    0.014 <frozen importlib._bootstrap>:817(_load_unlocked)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:85(__exit__)
        9    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:888(find_spec)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:912(create_module)
        2    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap>:920(exec_module)
        2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:937(is_package)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1014(create_module)
      7/1    0.000    0.000    0.014    0.014 <frozen importlib._bootstrap_external>:1017(exec_module)
        7    0.000    0.000    0.006    0.001 <frozen importlib._bootstrap_external>:1090(get_code)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1181(__init__)
      105    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:119(<listcomp>)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1206(get_filename)
        7    0.000    0.000    0.004    0.001 <frozen importlib._bootstrap_external>:1211(get_data)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1230(path_stats)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:132(_path_split)
       42    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:134(<genexpr>)
       35    0.000    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:140(_path_stat)
       28    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1494(_path_importer_cache)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:150(_path_is_mode_type)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1537(_get_spec)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1569(find_spec)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:159(_path_isfile)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1670(_get_spec)
       21    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:1675(find_spec)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:172(_path_isabs)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:185(_path_abspath)
       14    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:474(cache_from_source)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:603(_get_cached)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:635(_check_name_wrapper)
       21    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:67(_relax_case)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:678(_classify_pyc)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:711(_validate_timestamp_pyc)
        7    0.000    0.000    0.002    0.000 <frozen importlib._bootstrap_external>:763(_compile_bytecode)
        7    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:814(spec_from_file_location)
       21    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:84(_unpack_uint32)
      105    0.001    0.000    0.001    0.000 <frozen importlib._bootstrap_external>:96(_path_join)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        2    0.000    0.000    0.001    0.000 __init__.py:226(compile)
       47    0.000    0.000    0.000    0.000 __init__.py:255(escape)
        2    0.000    0.000    0.001    0.000 __init__.py:280(_compile)
       11    0.000    0.000    0.002    0.000 __init__.py:348(namedtuple)
       67    0.000    0.000    0.000    0.000 __init__.py:422(<genexpr>)
        6    0.000    0.000    0.000    0.000 _compiler.py:214(_compile_charset)
        6    0.000    0.000    0.000    0.000 _compiler.py:241(_optimize_charset)
        1    0.000    0.000    0.000    0.000 _compiler.py:31(_combine_flags)
     10/2    0.000    0.000    0.000    0.000 _compiler.py:37(_compile)
        3    0.000    0.000    0.000    0.000 _compiler.py:384(_mk_bitmap)
        3    0.000    0.000    0.000    0.000 _compiler.py:386(<listcomp>)
        5    0.000    0.000    0.000    0.000 _compiler.py:396(_simple)
        2    0.000    0.000    0.000    0.000 _compiler.py:426(_get_iscased)
        1    0.000    0.000    0.000    0.000 _compiler.py:434(_get_literal_prefix)
        1    0.000    0.000    0.000    0.000 _compiler.py:465(_get_charset_prefix)
        2    0.000    0.000    0.000    0.000 _compiler.py:509(_compile_info)
        4    0.000    0.000    0.000    0.000 _compiler.py:568(isstring)
        2    0.000    0.000    0.000    0.000 _compiler.py:571(_code)
        2    0.000    0.000    0.001    0.000 _compiler.py:738(compile)
       11    0.000    0.000    0.000    0.000 _parser.py:109(__init__)
       25    0.000    0.000    0.000    0.000 _parser.py:158(__len__)
       66    0.000    0.000    0.000    0.000 _parser.py:162(__getitem__)
        6    0.000    0.000    0.000    0.000 _parser.py:166(__setitem__)
       20    0.000    0.000    0.000    0.000 _parser.py:170(append)
     11/3    0.000    0.000    0.000    0.000 _parser.py:172(getwidth)
        2    0.000    0.000    0.000    0.000 _parser.py:222(__init__)
       54    0.000    0.000    0.000    0.000 _parser.py:231(__next)
       40    0.000    0.000    0.000    0.000 _parser.py:247(match)
       47    0.000    0.000    0.000    0.000 _parser.py:252(get)
       17    0.000    0.000    0.000    0.000 _parser.py:284(tell)
        8    0.000    0.000    0.000    0.000 _parser.py:303(_class_escape)
        6    0.000    0.000    0.000    0.000 _parser.py:440(_uniq)
      4/2    0.000    0.000    0.000    0.000 _parser.py:443(_parse_sub)
      5/2    0.000    0.000    0.000    0.000 _parser.py:503(_parse)
        2    0.000    0.000    0.000    0.000 _parser.py:73(__init__)
        6    0.000    0.000    0.000    0.000 _parser.py:79(groups)
        1    0.000    0.000    0.000    0.000 _parser.py:82(opengroup)
        1    0.000    0.000    0.000    0.000 _parser.py:94(closegroup)
        2    0.000    0.000    0.000    0.000 _parser.py:944(fix_flags)
        2    0.000    0.000    0.000    0.000 _parser.py:960(parse)
        1    0.000    0.000    0.001    0.001 ast.py:1(<module>)
        1    0.000    0.000    0.000    0.000 ast.py:395(NodeVisitor)
        1    0.000    0.000    0.000    0.000 ast.py:453(NodeTransformer)
        1    0.000    0.000    0.000    0.000 ast.py:527(_ABC)
        5    0.000    0.000    0.000    0.000 ast.py:529(__init__)
        1    0.000    0.000    0.000    0.000 ast.py:559(Num)
        1    0.000    0.000    0.000    0.000 ast.py:563(Str)
        1    0.000    0.000    0.000    0.000 ast.py:567(Bytes)
        1    0.000    0.000    0.000    0.000 ast.py:571(NameConstant)
        1    0.000    0.000    0.000    0.000 ast.py:574(Ellipsis)
        1    0.000    0.000    0.000    0.000 ast.py:604(slice)
        1    0.000    0.000    0.000    0.000 ast.py:607(Index)
        1    0.000    0.000    0.000    0.000 ast.py:612(ExtSlice)
        1    0.000    0.000    0.000    0.000 ast.py:631(Suite)
        1    0.000    0.000    0.000    0.000 ast.py:634(AugLoad)
        1    0.000    0.000    0.000    0.000 ast.py:637(AugStore)
        1    0.000    0.000    0.000    0.000 ast.py:640(Param)
        1    0.000    0.000    0.000    0.000 ast.py:648(_Precedence)
        1    0.000    0.000    0.000    0.000 ast.py:684(_Unparser)
        3    0.000    0.000    0.000    0.000 contextlib.py:260(contextmanager)
        1    0.000    0.000    0.003    0.003 dis.py:1(<module>)
        1    0.000    0.000    0.000    0.000 dis.py:160(_Unknown)
        1    0.000    0.000    0.000    0.000 dis.py:283(Instruction)
        1    0.000    0.000    0.000    0.000 dis.py:49(<listcomp>)
        1    0.000    0.000    0.000    0.000 dis.py:55(<dictcomp>)
        1    0.000    0.000    0.000    0.000 dis.py:709(Bytecode)
        1    0.000    0.000    0.000    0.000 enum.py:1001(_find_new_)
        4    0.000    0.000    0.000    0.000 enum.py:1095(__new__)
       23    0.000    0.000    0.000    0.000 enum.py:1146(__init__)
       18    0.000    0.000    0.000    0.000 enum.py:1149(_generate_next_value_)
        2    0.000    0.000    0.000    0.000 enum.py:1248(value)
        4    0.000    0.000    0.000    0.000 enum.py:1506(__and__)
        1    0.000    0.000    0.000    0.000 enum.py:1635(_simple_enum)
        1    0.000    0.000    0.000    0.000 enum.py:1651(convert_class)
       18    0.000    0.000    0.000    0.000 enum.py:177(__init__)
       21    0.000    0.000    0.000    0.000 enum.py:194(__get__)
       19    0.000    0.000    0.000    0.000 enum.py:230(__set_name__)
        5    0.000    0.000    0.000    0.000 enum.py:240(__init__)
        5    0.000    0.000    0.000    0.000 enum.py:243(__set_name__)
        1    0.000    0.000    0.000    0.000 enum.py:352(__init__)
       10    0.000    0.000    0.000    0.000 enum.py:359(__setitem__)
       25    0.000    0.000    0.000    0.000 enum.py:37(_is_descriptor)
       31    0.000    0.000    0.000    0.000 enum.py:47(_is_dunder)
        1    0.000    0.000    0.000    0.000 enum.py:476(__prepare__)
        2    0.000    0.000    0.000    0.000 enum.py:491(__new__)
       30    0.000    0.000    0.000    0.000 enum.py:58(_is_sunder)
       10    0.000    0.000    0.000    0.000 enum.py:69(_is_internal_class)
        4    0.000    0.000    0.000    0.000 enum.py:699(__call__)
        9    0.000    0.000    0.000    0.000 enum.py:753(__delattr__)
       30    0.000    0.000    0.000    0.000 enum.py:78(_is_private)
        5    0.000    0.000    0.000    0.000 enum.py:796(__members__)
       29    0.000    0.000    0.000    0.000 enum.py:818(__setattr__)
        1    0.000    0.000    0.000    0.000 enum.py:924(_check_for_existing_members_)
        2    0.000    0.000    0.000    0.000 enum.py:934(_get_mixins_)
        1    0.000    0.000    0.000    0.000 enum.py:953(_find_data_repr_)
        2    0.000    0.000    0.000    0.000 enum.py:975(_find_data_type_)
        4    0.000    0.000    0.000    0.000 functools.py:35(update_wrapper)
        1    0.000    0.000    0.000    0.000 functools.py:479(lru_cache)
        3    0.000    0.000    0.000    0.000 functools.py:65(wraps)
        1    0.000    0.000    0.012    0.012 inspect.py:1(<module>)
        1    0.000    0.000    0.000    0.000 inspect.py:1031(ClassFoundException)
        1    0.000    0.000    0.000    0.000 inspect.py:1035(_ClassFinder)
        1    0.000    0.000    0.000    0.000 inspect.py:1179(EndOfBlock)
        1    0.000    0.000    0.000    0.000 inspect.py:1181(BlockFinder)
        1    0.000    0.000    0.000    0.000 inspect.py:1649(Traceback)
        1    0.000    0.000    0.000    0.000 inspect.py:1721(FrameInfo)
  1000000    0.427    0.000    0.507    0.000 inspect.py:1779(_check_instance)
  1000000    0.866    0.000    2.179    0.000 inspect.py:1788(_check_class)
  3000000    1.991    0.000    1.991    0.000 inspect.py:1794(_shadowed_dict)
  1000000    1.017    0.000    4.381    0.000 inspect.py:1805(getattr_static)
        1    0.000    0.000    0.000    0.000 inspect.py:2647(_void)
        1    0.000    0.000    0.000    0.000 inspect.py:2651(_empty)
        1    0.000    0.000    0.000    0.000 inspect.py:2655(_ParameterKind)
        5    0.000    0.000    0.000    0.000 inspect.py:2662(__new__)
        1    0.000    0.000    0.000    0.000 inspect.py:2679(Parameter)
        1    0.000    0.000    0.000    0.000 inspect.py:2837(BoundArguments)
        1    0.000    0.000    0.000    0.000 inspect.py:2967(Signature)
        1    0.000    0.000    0.004    0.004 linecache.py:1(<module>)
        1    0.000    0.000    0.001    0.001 opcode.py:1(<module>)
        1    0.000    0.000    0.000    0.000 opcode.py:224(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:245(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:373(<listcomp>)
        1    0.000    0.000    0.000    0.000 opcode.py:422(<listcomp>)
      112    0.000    0.000    0.000    0.000 opcode.py:51(def_op)
       11    0.000    0.000    0.000    0.000 opcode.py:54(name_op)
        9    0.000    0.000    0.000    0.000 opcode.py:58(jrel_op)
        7    0.000    0.000    0.000    0.000 opcode.py:66(pseudo_op)
       63    0.000    0.000    0.000    0.000 opcode.py:71(<listcomp>)
        1    0.000    0.000    0.000    0.000 token.py:1(<module>)
        1    0.000    0.000    0.000    0.000 token.py:75(<dictcomp>)
        1    0.000    0.000    0.002    0.002 tokenize.py:1(<module>)
        1    0.000    0.000    0.000    0.000 tokenize.py:161(TokenError)
        1    0.000    0.000    0.000    0.000 tokenize.py:163(StopTokenizing)
        1    0.000    0.000    0.000    0.000 tokenize.py:166(Untokenizer)
        1    0.000    0.000    0.000    0.000 tokenize.py:46(TokenInfo)
       19    0.000    0.000    0.000    0.000 tokenize.py:59(group)
        1    0.000    0.000    0.000    0.000 tokenize.py:60(any)
        2    0.000    0.000    0.000    0.000 tokenize.py:61(maybe)
        3    0.000    0.000    0.000    0.000 tokenize.py:84(_all_string_prefixes)
       24    0.000    0.000    0.000    0.000 tokenize.py:95(<listcomp>)
       19    0.000    0.000    0.000    0.000 types.py:163(__init__)
        1    0.000    0.000    0.000    0.000 typing.py:1968(_caller)
        1    0.000    0.000    0.000    0.000 typing.py:1979(_allow_reckless_class_checks)
        1    0.000    0.000    0.014    0.014 typing.py:1997(_lazy_load_getattr_static)
   100000    0.348    0.000    4.803    0.000 typing.py:2020(__instancecheck__)
        1    0.000    0.000    0.000    0.000 typing.py:2092(_proto_hook)
    25/20    0.000    0.000    0.000    0.000 {built-in method __new__ of type object at 0x00007FFF6D6EC400}
   100000    0.018    0.000    0.018    0.000 {built-in method _abc._abc_instancecheck}
        1    0.000    0.000    0.000    0.000 {built-in method _abc._abc_subclasscheck}
        7    0.000    0.000    0.000    0.000 {built-in method _imp._fix_co_filename}
       41    0.000    0.000    0.000    0.000 {built-in method _imp.acquire_lock}
        2    0.000    0.000    0.000    0.000 {built-in method _imp.create_builtin}
        2    0.001    0.000    0.001    0.000 {built-in method _imp.exec_builtin}
        7    0.000    0.000    0.000    0.000 {built-in method _imp.find_frozen}
        9    0.000    0.000    0.000    0.000 {built-in method _imp.is_builtin}
       41    0.000    0.000    0.000    0.000 {built-in method _imp.release_lock}
        2    0.000    0.000    0.000    0.000 {built-in method _sre.compile}
        9    0.000    0.000    0.000    0.000 {built-in method _thread.allocate_lock}
       18    0.000    0.000    0.000    0.000 {built-in method _thread.get_ident}
       36    0.000    0.000    0.001    0.000 {built-in method builtins.__build_class__}
        6    0.000    0.000    0.000    0.000 {built-in method builtins.all}
       63    0.000    0.000    0.000    0.000 {built-in method builtins.any}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.callable}
        9    0.000    0.000    0.000    0.000 {built-in method builtins.delattr}
       11    0.001    0.000    0.001    0.000 {built-in method builtins.eval}
      8/1    0.000    0.000    4.868    4.868 {built-in method builtins.exec}
   200128    0.018    0.000    0.018    0.000 {built-in method builtins.getattr}
        2    0.000    0.000    0.000    0.000 {built-in method builtins.globals}
      125    0.000    0.000    0.000    0.000 {built-in method builtins.hasattr}
100348/100000    0.040    0.000    4.843    0.000 {built-in method builtins.isinstance}
        9    0.000    0.000    0.000    0.000 {built-in method builtins.issubclass}
  589/577    0.000    0.000    0.000    0.000 {built-in method builtins.len}
       16    0.000    0.000    0.000    0.000 {built-in method builtins.max}
       23    0.000    0.000    0.000    0.000 {built-in method builtins.min}
       15    0.000    0.000    0.000    0.000 {built-in method builtins.ord}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.repr}
       47    0.000    0.000    0.000    0.000 {built-in method builtins.setattr}
        1    0.000    0.000    0.000    0.000 {built-in method builtins.sorted}
      256    0.000    0.000    0.000    0.000 {built-in method builtins.sum}
       21    0.000    0.000    0.000    0.000 {built-in method from_bytes}
        6    0.000    0.000    0.000    0.000 {built-in method fromkeys}
        7    0.003    0.000    0.003    0.000 {built-in method io.open_code}
        7    0.001    0.000    0.001    0.000 {built-in method marshal.loads}
        7    0.000    0.000    0.000    0.000 {built-in method nt._path_splitroot}
       21    0.000    0.000    0.000    0.000 {built-in method nt.fspath}
       35    0.001    0.000    0.001    0.000 {built-in method nt.stat}
       12    0.000    0.000    0.000    0.000 {built-in method sys._getframemodulename}
       67    0.000    0.000    0.000    0.000 {built-in method sys.intern}
       67    0.000    0.000    0.000    0.000 {method '__contains__' of 'frozenset' objects}
        7    0.000    0.000    0.000    0.000 {method '__exit__' of '_io._IOBase' objects}
       18    0.000    0.000    0.000    0.000 {method '__exit__' of '_thread.RLock' objects}
      232    0.000    0.000    0.000    0.000 {method 'add' of 'set' objects}
      391    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        1    0.000    0.000    0.000    0.000 {method 'bit_length' of 'int' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
      357    0.000    0.000    0.000    0.000 {method 'endswith' of 'str' objects}
        7    0.000    0.000    0.000    0.000 {method 'extend' of 'list' objects}
       28    0.000    0.000    0.000    0.000 {method 'find' of 'bytearray' objects}
  1000294    0.080    0.000    0.080    0.000 {method 'get' of 'dict' objects}
       57    0.000    0.000    0.000    0.000 {method 'get' of 'mappingproxy' objects}
       67    0.000    0.000    0.000    0.000 {method 'isidentifier' of 'str' objects}
        8    0.000    0.000    0.000    0.000 {method 'items' of 'dict' objects}
        1    0.000    0.000    0.000    0.000 {method 'items' of 'mappingproxy' objects}
      232    0.000    0.000    0.000    0.000 {method 'join' of 'str' objects}
       13    0.000    0.000    0.000    0.000 {method 'pop' of 'dict' objects}
        9    0.000    0.000    0.000    0.000 {method 'pop' of 'list' objects}
        2    0.000    0.000    0.000    0.000 {method 'pop' of 'set' objects}
        7    0.001    0.000    0.001    0.000 {method 'read' of '_io.BufferedReader' objects}
        9    0.000    0.000    0.000    0.000 {method 'remove' of 'list' objects}
       15    0.000    0.000    0.000    0.000 {method 'replace' of 'str' objects}
       28    0.000    0.000    0.000    0.000 {method 'rfind' of 'str' objects}
       53    0.000    0.000    0.000    0.000 {method 'rpartition' of 'str' objects}
      329    0.000    0.000    0.000    0.000 {method 'rstrip' of 'str' objects}
       15    0.000    0.000    0.000    0.000 {method 'setdefault' of 'dict' objects}
       17    0.000    0.000    0.000    0.000 {method 'sort' of 'list' objects}
        8    0.000    0.000    0.000    0.000 {method 'split' of 'str' objects}
      618    0.000    0.000    0.000    0.000 {method 'startswith' of 'str' objects}
        3    0.000    0.000    0.000    0.000 {method 'translate' of 'bytearray' objects}
       47    0.000    0.000    0.000    0.000 {method 'translate' of 'str' objects}
        5    0.000    0.000    0.000    0.000 {method 'update' of 'dict' objects}
       36    0.000    0.000    0.000    0.000 {method 'upper' of 'str' objects}
      259    0.000    0.000    0.000    0.000 {method 'values' of 'dict' objects}

I think that might be just about acceptable now. We're still spending a lot of time in inspect._shadowed_dict(), but I can't see any easy way to optimise that function any further (or to reduce the number of calls to it).

AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Apr 7, 2023
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Apr 7, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Apr 7, 2023
…nGH-103347)

(cherry picked from commit 800382a)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington added a commit that referenced this issue Apr 7, 2023
(cherry picked from commit 800382a)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Apr 7, 2023
#103348)

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@AlexWaygood
Copy link
Member

Many thanks to everybody (and especially @carljm!) for the time spent reviewing these PRs!

@carljm
Copy link
Contributor

carljm commented Apr 7, 2023

Thanks for all the investigation and experimentation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 new features, bugs and security fixes expert-typing performance Performance or resource usage stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants