Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #8535/7108d646 backport][3.10] Small speed up to cookiejar filter_cookies #8537

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES/8535.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve performance of filtering cookies -- by :user:`bdraco`.

This change is a followup to the improvements in :issue:`7583`
11 changes: 7 additions & 4 deletions aiohttp/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

Expand Down
Loading