Fixed #33532 -- Optimized CaseInsensitiveMapping instantiation for dicts.

Internal usages of this class (e.g. HttpHeaders) provide it with a dict,
so testing for that type first avoids the cost of going through the
potential __instancecheck__ + _abc_instancecheck to establish it's
a Mapping.

Co-authored-by: Nick Pope <nick@nickpope.me.uk>
This commit is contained in:
Keryn Knight 2022-02-23 19:15:22 +00:00 committed by GitHub
parent fe7dbef586
commit 3de787a70b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 1 deletions

View File

@ -327,7 +327,10 @@ class CaseInsensitiveMapping(Mapping):
@staticmethod
def _unpack_items(data):
if isinstance(data, Mapping):
# Explicitly test for dict first as the common case for performance,
# avoiding abc's __instancecheck__ and _abc_instancecheck for the
# general Mapping case.
if isinstance(data, (dict, Mapping)):
yield from data.items()
return
for i, elem in enumerate(data):