]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-126156: Improve performance of creating `Morsel` objects (#126157)
authorJ. Nick Koston <nick@koston.org>
Thu, 31 Oct 2024 19:05:40 +0000 (14:05 -0500)
committerGitHub <noreply@github.com>
Thu, 31 Oct 2024 19:05:40 +0000 (12:05 -0700)
Replaces the manually constructed loop with a call to `dict.update`

Lib/http/cookies.py
Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst [new file with mode: 0644]

index 6b9ed24ad8ec78e3734919aa9ac39027c3f8574f..d7e8d08b2d92c156daeb94a7651ae9713eb55779 100644 (file)
@@ -266,6 +266,8 @@ class Morsel(dict):
         "samesite" : "SameSite",
     }
 
+    _reserved_defaults = dict.fromkeys(_reserved, "")
+
     _flags = {'secure', 'httponly'}
 
     def __init__(self):
@@ -273,8 +275,7 @@ class Morsel(dict):
         self._key = self._value = self._coded_value = None
 
         # Set default attributes
-        for key in self._reserved:
-            dict.__setitem__(self, key, "")
+        dict.update(self, self._reserved_defaults)
 
     @property
     def key(self):
diff --git a/Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst b/Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst
new file mode 100644 (file)
index 0000000..4fe1827
--- /dev/null
@@ -0,0 +1 @@
+Improved performances of creating :py:class:`~http.cookies.Morsel` objects by a factor of 3.8x.