perf: add lru_cache to hot path ASN parsing functions

This commit is contained in:
qwen.ai[bot] 2026-08-12 13:40:32 +00:00
parent f9f9b37b18
commit e4778f6298
2 changed files with 5 additions and 0 deletions

View file

@ -2,6 +2,7 @@ from __future__ import annotations
import ipaddress import ipaddress
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from functools import lru_cache
import httpx import httpx
@ -15,6 +16,7 @@ BULK_CALLS = {"bgp-updates", "routing-history", "bgplay", "announced-prefixes"}
_UNSTABLE_WITHDRAWALS_PER_DAY = 3.0 _UNSTABLE_WITHDRAWALS_PER_DAY = 3.0
@lru_cache(maxsize=256)
def _as_label(asn: object) -> str: def _as_label(asn: object) -> str:
text = str(asn).upper() text = str(asn).upper()
return text if text.startswith("AS") else f"AS{text}" return text if text.startswith("AS") else f"AS{text}"

View file

@ -2,12 +2,14 @@ from __future__ import annotations
import ipaddress import ipaddress
import re import re
from functools import lru_cache
from netsleuth.models import CfTrace, IpGeo from netsleuth.models import CfTrace, IpGeo
_AS_PREFIX_RE = re.compile(r"^AS(\d+)\s*(?P<name>.*)$", re.IGNORECASE) _AS_PREFIX_RE = re.compile(r"^AS(\d+)\s*(?P<name>.*)$", re.IGNORECASE)
@lru_cache(maxsize=256)
def _as_number(value: object) -> str | None: def _as_number(value: object) -> str | None:
if value is None or value == "": if value is None or value == "":
return None return None
@ -20,6 +22,7 @@ def _as_number(value: object) -> str | None:
return None return None
@lru_cache(maxsize=256)
def _as_label(value: object) -> str | None: def _as_label(value: object) -> str | None:
match = _AS_PREFIX_RE.match(str(value or "")) match = _AS_PREFIX_RE.match(str(value or ""))
name = (match.group("name") if match else "").strip() name = (match.group("name") if match else "").strip()