https://github.com/python/cpython/commit/dd3c0fa3fd2795326dae0e0ed63c668f5506cf32
commit: dd3c0fa3fd2795326dae0e0ed63c668f5506cf32
branch: main
author: J. Nick Koston <[email protected]>
committer: hauntsaninja <[email protected]>
date: 2024-10-31T12:05:40-07:00
summary:

gh-126156: Improve performance of creating `Morsel` objects (#126157)

Replaces the manually constructed loop with a call to `dict.update`

files:
A Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst
M Lib/http/cookies.py

diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py
index 6b9ed24ad8ec78..d7e8d08b2d92c1 100644
--- a/Lib/http/cookies.py
+++ b/Lib/http/cookies.py
@@ -266,6 +266,8 @@ class Morsel(dict):
         "samesite" : "SameSite",
     }
 
+    _reserved_defaults = dict.fromkeys(_reserved, "")
+
     _flags = {'secure', 'httponly'}
 
     def __init__(self):
@@ -273,8 +275,7 @@ def __init__(self):
         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
index 00000000000000..4fe18275ab9384
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst
@@ -0,0 +1 @@
+Improved performances of creating :py:class:`~http.cookies.Morsel` objects by 
a factor of 3.8x.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to