summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeryn Knight <keryn@kerynknight.com>2022-02-23 19:15:22 +0000
committerGitHub <noreply@github.com>2022-02-23 20:15:22 +0100
commit3de787a70b6c140a3b1289a8ce12b17ebcb8a200 (patch)
treea5c26ffb4b335866cba359f4fb5ae18409bf44ed
parentfe7dbef5867c577995f0fc849d8dfdb8f2e6bbfa (diff)
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>
-rw-r--r--django/utils/datastructures.py5
1 files changed, 4 insertions, 1 deletions
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py
index b5858b8076..51a586d152 100644
--- a/django/utils/datastructures.py
+++ b/django/utils/datastructures.py
@@ -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):