Skip to content

Commit

Permalink
Add a cache to parsing the host when encoding hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco committed Oct 11, 2024
1 parent 092513a commit b37f3f2
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,8 @@ def _parse_host(
lower_host = host.lower()
is_ascii = host.isascii()

# If the host ends with a digit or contains a colon, its likely
# an IP address.
if host and (host[-1].isdigit() or ":" in host):
if "%" in host:
return True, lower_host, is_ascii, *host.partition("%")
Expand All @@ -1025,8 +1027,7 @@ def _encode_host(
"""Encode host part of URL."""
looks_like_ip, lower_host, is_ascii, raw_ip, sep, zone = cls._parse_host(host)
if looks_like_ip:
# If the host ends with a digit or contains a colon, its likely
# an IP address. So we check with _ip_compressed_version
# If it looks like an IP, we check with _ip_compressed_version
# and fall-through if its not an IP address. This is a performance
# optimization to avoid parsing IP addresses as much as possible
# because it is orders of magnitude slower than almost any other
Expand Down

0 comments on commit b37f3f2

Please sign in to comment.