From 3baa6de1ec831e1b6d97127adced3f4ff1d5cb05 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Thu, 25 Jul 2024 18:43:03 +0000 Subject: [PATCH] [PR #8535/7108d646 backport][3.10] Small speed up to cookiejar filter_cookies (#8537) **This is a backport of PR #8535 as merged into master (7108d6469d20dd48919d312b2c654aef867ebe51).** ## What do these changes do? Small speed up to cookiejar Using `str.format` is ~16% faster than the lambda followup to https://github.com/aio-libs/aiohttp/pull/7944#discussion_r1430823536. I was hoping to use `join` there but later realized `str.format` will take `*args` ## Are there changes in behavior for the user? no ## Is it a substantial burden for the maintainers to support this? no benchmark ```python import timeit import itertools _FORMAT_PATH = "{0}/{1}".format path = "lolonglonglonglonglonglongng/path/to/a/file" print( timeit.timeit( 'itertools.accumulate(path.split("/"), _FORMAT_PATH)', globals=globals() ) ) print( timeit.timeit( 'itertools.accumulate(path.split("/"), lambda x, y: f"{x}/{y}")', globals=globals(), ) ) ``` Co-authored-by: J. Nick Koston --- CHANGES/8535.misc.rst | 3 +++ aiohttp/cookiejar.py | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) 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` diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index 3c7629c7f33..e9997ce2935 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -36,6 +36,10 @@ 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 + class CookieJar(AbstractCookieJar): """Implements cookie storage adhering to RFC 6265.""" @@ -274,12 +278,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)