From 2d307cbdc288d52b34615f76cf40944558f4e503 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 29 Oct 2024 14:10:53 -1000 Subject: [PATCH 1/9] gh-126156: Improve performance of creating `Morsel` objects Replaces the manually constructed loop with a call to `dict.update` --- Lib/http/cookies.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 6b9ed24ad8ec78..6c9806c738e320 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -266,6 +266,8 @@ class Morsel(dict): "samesite" : "SameSite", } + _reserved_defaults = {key: "" for key in _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): From 76704009cabee98f78111e57f7316777bd52d21a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:12:26 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst 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..95ced4e87578c8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst @@ -0,0 +1 @@ +Improved performance of creating :py:class:`~http.cookies.Morsel` objects From 9d7b9d95fb2ea309ae0f51a61e319bf46ca8a4c2 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 29 Oct 2024 14:12:50 -1000 Subject: [PATCH 3/9] Update Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst --- .../next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 95ced4e87578c8..34519b1c103a77 100644 --- 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 @@ -1 +1 @@ -Improved performance of creating :py:class:`~http.cookies.Morsel` objects +Improved performance of creating :py:class:`~http.cookies.Morsel` objects. From 8998fae79367162dd6a47e6c3e35def636ae0dfc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 30 Oct 2024 19:45:17 -0500 Subject: [PATCH 4/9] Switch to `dict.fromkeys` --- Lib/http/cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index 6c9806c738e320..d7e8d08b2d92c1 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -266,7 +266,7 @@ class Morsel(dict): "samesite" : "SameSite", } - _reserved_defaults = {key: "" for key in _reserved} + _reserved_defaults = dict.fromkeys(_reserved, "") _flags = {'secure', 'httponly'} From c8e3f36d4f467a510db6ec8af1d0cf00e07343d4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 31 Oct 2024 10:42:51 -0500 Subject: [PATCH 5/9] Update Lib/http/cookies.py --- Lib/http/cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index d7e8d08b2d92c1..a420dc2d851412 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -275,7 +275,7 @@ def __init__(self): self._key = self._value = self._coded_value = None # Set default attributes - dict.update(self, self._reserved_defaults) + self |= self._reserved_defaults @property def key(self): From 975238a3689ca7307dc9b7ee88a00020234d28c9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 31 Oct 2024 10:43:22 -0500 Subject: [PATCH 6/9] Update Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst --- .../next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 34519b1c103a77..891e1490e88038 100644 --- 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 @@ -1 +1 @@ -Improved performance of creating :py:class:`~http.cookies.Morsel` objects. +Improved performance of creating :py:class:`~http.cookies.Morsel` objects by a factor of 4.5x. From 174f8e330643621844162de0af0db3a0175ea237 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 31 Oct 2024 11:04:47 -0500 Subject: [PATCH 7/9] Revert "Update Lib/http/cookies.py" This reverts commit c8e3f36d4f467a510db6ec8af1d0cf00e07343d4. --- Lib/http/cookies.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookies.py b/Lib/http/cookies.py index a420dc2d851412..d7e8d08b2d92c1 100644 --- a/Lib/http/cookies.py +++ b/Lib/http/cookies.py @@ -275,7 +275,7 @@ def __init__(self): self._key = self._value = self._coded_value = None # Set default attributes - self |= self._reserved_defaults + dict.update(self, self._reserved_defaults) @property def key(self): From 46da2ccf5bf738f9ac93a8d367dd2189181aed9e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 31 Oct 2024 11:05:36 -0500 Subject: [PATCH 8/9] Update Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst --- .../next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 891e1490e88038..056241c39d4a57 100644 --- 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 @@ -1 +1 @@ -Improved performance of creating :py:class:`~http.cookies.Morsel` objects by a factor of 4.5x. +Improved performance of creating :py:class:`~http.cookies.Morsel` objects by a factor of 3.8x. From c1e29ac5db014931d03c79945753f83f4e01112e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 31 Oct 2024 11:07:39 -0500 Subject: [PATCH 9/9] Update Misc/NEWS.d/next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2024-10-30-00-12-22.gh-issue-126156.BOSqv0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 056241c39d4a57..4fe18275ab9384 100644 --- 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 @@ -1 +1 @@ -Improved performance of creating :py:class:`~http.cookies.Morsel` objects by a factor of 3.8x. +Improved performances of creating :py:class:`~http.cookies.Morsel` objects by a factor of 3.8x.