From 02be881297690bcb95ea096a6fb4de766c596f41 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 24 Jul 2024 20:28:02 -0500 Subject: [PATCH 1/3] Small speed up to cookiejar Using str.format is ~16% faster than the lambda --- aiohttp/cookiejar.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index da16254310f..50fa3269078 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -36,6 +36,9 @@ CookieItem = Union[str, "Morsel[str]"] +_FORMAT_PATH = "{}/{}".format +_FORMAT_DOMAIN_REVERSED = "{1}.{0}".format + class CookieJar(AbstractCookieJar): """Implements cookie storage adhering to RFC 6265.""" @@ -279,12 +282,11 @@ def filter_cookies(self, request_url: URL = URL()) -> "BaseCookie[str]": else: # Get all the subdomains that might match a cookie (e.g. "foo.bar.com", "bar.com", "com") domains = itertools.accumulate( - reversed(hostname.split(".")), lambda x, y: f"{y}.{x}" + reversed(hostname.split(".")), _FORMAT_DOMAIN_REVERSED ) + # Get all the path prefixes that might match a cookie (e.g. "", "/foo", "/foo/bar") - paths = itertools.accumulate( - request_url.path.split("/"), lambda x, y: f"{x}/{y}" - ) + paths = itertools.accumulate(request_url.path.split("/"), _FORMAT_PATH) # Create every combination of (domain, path) pairs. pairs = itertools.product(domains, paths) From db6a79f4b3ecec35aaa475c266a6310a5f65895b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 24 Jul 2024 20:35:00 -0500 Subject: [PATCH 2/3] change --- CHANGES/8535.misc.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 CHANGES/8535.misc.rst diff --git a/CHANGES/8535.misc.rst b/CHANGES/8535.misc.rst new file mode 100644 index 00000000000..e1acc438695 --- /dev/null +++ b/CHANGES/8535.misc.rst @@ -0,0 +1,3 @@ +Improve performance of filtering cookies -- by :user:`bdraco`. + +This change is a followup to the improvements in :issue:`7583` From 2f191864f76f806e31ecdcc8d53d129207315876 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 25 Jul 2024 12:42:34 -0500 Subject: [PATCH 3/3] Update aiohttp/cookiejar.py Co-authored-by: Sam Bull --- aiohttp/cookiejar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index 50fa3269078..396016a69ff 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -36,6 +36,7 @@ CookieItem = Union[str, "Morsel[str]"] +# We cache these string methods here as their use is in performance critical code. _FORMAT_PATH = "{}/{}".format _FORMAT_DOMAIN_REVERSED = "{1}.{0}".format