Skip to content

Commit

Permalink
Improve performance of splitting the netloc on cache miss
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 19, 2024
1 parent 7cdbf04 commit f379998
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,15 +549,18 @@ def __setstate__(self, state):
self._val, *unused = state
self._cache = {}

def _cache_netloc(self) -> None:
"""Cache the netloc parts of the URL."""
cache = self._cache
(
cache["raw_user"],
cache["raw_password"],
cache["raw_host"],
cache["explicit_port"],
) = self._split_netloc(self._val.netloc)
def _cached_split_netloc(
self,
) -> tuple[Union[str, None], Union[str, None], Union[str, None], Union[int, None]]:
"""Cached split of the netloc parts of the URL.
Returns a tuple of the cached parts:
(raw_user, raw_password, raw_host, explicit_port)
"""
c = self._cache
split_loc = self._split_netloc(self._val.netloc)
c["raw_user"], c["raw_password"], c["raw_host"], c["explicit_port"] = split_loc
return split_loc

def is_absolute(self) -> bool:
"""A check for absolute URLs.
Expand Down Expand Up @@ -679,8 +682,7 @@ def raw_user(self) -> Union[str, None]:
"""
# not .username
self._cache_netloc()
return self._cache["raw_user"]
return self._cached_split_netloc()[0]

@cached_property
def user(self) -> Union[str, None]:
Expand All @@ -700,8 +702,7 @@ def raw_password(self) -> Union[str, None]:
None if password is missing.
"""
self._cache_netloc()
return self._cache["raw_password"]
return self._cached_split_netloc()[1]

@cached_property
def password(self) -> Union[str, None]:
Expand All @@ -725,8 +726,7 @@ def raw_host(self) -> Union[str, None]:
"""
# Use host instead of hostname for sake of shortness
# May add .hostname prop later
self._cache_netloc()
return self._cache["raw_host"]
return self._cached_split_netloc()[2]

@cached_property
def host(self) -> Union[str, None]:
Expand Down Expand Up @@ -781,8 +781,7 @@ def explicit_port(self) -> Union[int, None]:
None for relative URLs or URLs without explicit port.
"""
self._cache_netloc()
return self._cache["explicit_port"]
return self._cached_split_netloc()[3]

@cached_property
def raw_path(self) -> str:
Expand Down

0 comments on commit f379998

Please sign in to comment.