You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Morsel.__init__ loops reserved values and could be replaced with a simple dict update #126156
Backstory: aio-libs/aiohttp#9594
Processing cookies is a significant part of the aiohttp request time, and some requesters have to create many Morsel objects because they have a lot of cookies on every request.
There are few places that can be optimized in http.cookies
(venv) bdraco cpython % python3 morsel_bench.py
0.33160758309531957 - original
0.28600058297161013 - new
(venv) bdraco cpython % cat morsel_bench.py
fromhttp.cookiesimportMorselimporttimeitclassFastMorsel(dict):
"""A class to hold ONE (key, value) pair. In a cookie, each such pair may have several attributes, so this class is used to keep the attributes associated with the appropriate key,value pair. This class also includes a coded_value attribute, which is used to hold the network representation of the value. """# RFC 2109 lists these attributes as reserved:# path comment domain# max-age secure version## For historical reasons, these attributes are also reserved:# expires## This is an extension from Microsoft:# httponly## This dictionary provides a mapping from the lowercase# variant on the left to the appropriate traditional# formatting on the right._reserved= {
"expires": "expires",
"path": "Path",
"comment": "Comment",
"domain": "Domain",
"max-age": "Max-Age",
"secure": "Secure",
"httponly": "HttpOnly",
"version": "Version",
"samesite": "SameSite",
}
_reserved_defaults= {key: ""forkeyin_reserved}
_flags= {"secure", "httponly"}
def__init__(self):
# Set defaultsself._key=self._value=self._coded_value=None# Set default attributesdict.update(self, self._reserved_defaults)
print(timeit.timeit("Morsel()", globals={"Morsel": Morsel}))
print(timeit.timeit("FastMorsel()", globals={"FastMorsel": FastMorsel}))
Activity
pythongh-126156: Improve performance of creating `Morsel` objects
bdraco commentedon Oct 30, 2024
(venv) bdraco cpython % python3 morsel_bench.py
0.33160758309531957 - original
0.28600058297161013 - new
(venv) bdraco cpython % cat morsel_bench.py
gh-126156: Improve performance of creating `Morsel` objects (#126157)
pythongh-126156: Improve performance of creating `Morsel` objects (py…
pythongh-126156: Improve performance of creating `Morsel` objects (py…