From f20e1622875275bb8c74410dff5dad6edc81d054 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 20 Nov 2024 08:58:12 -0600 Subject: [PATCH] DNM: attempt to speed up caching the netloc --- yarl/_url.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/yarl/_url.py b/yarl/_url.py index d20a6106..8eee061d 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -527,9 +527,7 @@ def __setstate__(self, state): def _cache_netloc(self) -> None: """Cache the netloc parts of the URL.""" - c = self._cache - split_loc = split_netloc(self._netloc) - c["raw_user"], c["raw_password"], c["raw_host"], c["explicit_port"] = split_loc + self._cache.update(_split_netloc_to_cache(self._netloc)) def is_absolute(self) -> bool: """A check for absolute URLs. @@ -1403,6 +1401,18 @@ def human_repr(self) -> str: _DEFAULT_ENCODE_SIZE = 512 +@lru_cache +def _split_netloc_to_cache(netloc: str) -> dict[str, Any]: + """Split netloc into cache.""" + user, password, host, port = split_netloc(netloc) + return { + "raw_user": user, + "raw_password": password, + "raw_host": host, + "explicit_port": port, + } + + @lru_cache(_DEFAULT_IDNA_SIZE) def _idna_decode(raw: str) -> str: try: