Skip to content

Commit dd3c0fa

Browse files
authored
gh-126156: Improve performance of creating Morsel objects (#126157)
Replaces the manually constructed loop with a call to `dict.update`
1 parent 0e86655 commit dd3c0fa

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

Lib/http/cookies.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,16 @@ class Morsel(dict):
266266
"samesite" : "SameSite",
267267
}
268268

269+
_reserved_defaults = dict.fromkeys(_reserved, "")
270+
269271
_flags = {'secure', 'httponly'}
270272

271273
def __init__(self):
272274
# Set defaults
273275
self._key = self._value = self._coded_value = None
274276

275277
# Set default attributes
276-
for key in self._reserved:
277-
dict.__setitem__(self, key, "")
278+
dict.update(self, self._reserved_defaults)
278279

279280
@property
280281
def key(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved performances of creating :py:class:`~http.cookies.Morsel` objects by a factor of 3.8x.

0 commit comments

Comments
 (0)