Skip to content

Commit d2892da

Browse files
authored
Improve performance of _encode_host (#1176)
1 parent cf08033 commit d2892da

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

CHANGES/1176.misc.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved performance of encoding hosts -- by :user:`bdraco`.

yarl/_url.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -997,13 +997,18 @@ def _normalize_path(cls, path: str) -> str:
997997
def _encode_host(
998998
cls, host: str, human: bool = False, validate_host: bool = True
999999
) -> str:
1000-
if "%" in host:
1001-
raw_ip, sep, zone = host.partition("%")
1002-
else:
1003-
raw_ip = host
1004-
sep = zone = ""
1005-
1006-
if raw_ip and raw_ip[-1].isdigit() or ":" in raw_ip:
1000+
if host and host[-1].isdigit() or ":" in host:
1001+
# If the host ends with a digit or contains a colon, its likely
1002+
# an IP address. So we check with _ip_compressed_version
1003+
# and fall-through if its not an IP address. This is a performance
1004+
# optimization to avoid parsing IP addresses as much as possible
1005+
# because it is orders of magnitude slower than almost any other
1006+
# operation this library does.
1007+
if "%" in host:
1008+
raw_ip, sep, zone = host.partition("%")
1009+
else:
1010+
raw_ip = host
1011+
sep = zone = ""
10071012
# Might be an IP address, check it
10081013
#
10091014
# IP Addresses can look like:
@@ -1016,10 +1021,6 @@ def _encode_host(
10161021
# Rare IP Address formats are not supported per:
10171022
# https://datatracker.ietf.org/doc/html/rfc3986#section-7.4
10181023
#
1019-
# We try to avoid parsing IP addresses as much as possible
1020-
# since its orders of magnitude slower than almost any other operation
1021-
# this library does.
1022-
#
10231024
# IP parsing is slow, so its wrapped in an LRU
10241025
try:
10251026
ip_compressed_version = _ip_compressed_version(raw_ip)
@@ -1029,11 +1030,9 @@ def _encode_host(
10291030
# These checks should not happen in the
10301031
# LRU to keep the cache size small
10311032
host, version = ip_compressed_version
1032-
if sep:
1033-
host += "%" + zone
10341033
if version == 6:
1035-
return f"[{host}]"
1036-
return host
1034+
return f"[{host}%{zone}]" if sep else f"[{host}]"
1035+
return f"{host}%{zone}" if sep else host
10371036

10381037
host = host.lower()
10391038
if human:

0 commit comments

Comments
 (0)