Speed up _parse_package_output by caching normalized key strings (#3239)

This commit helps avoid ~260,000 chained strip(), lower(), and
replace() sequences.
This commit is contained in:
correctmost 2025-03-10 07:04:41 -04:00 committed by GitHub
parent 624143ed96
commit a5fcf21a12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 1 deletions

View File

@ -139,6 +139,11 @@ def list_available_packages(
return packages
@lru_cache(maxsize=128)
def _normalize_key_name(key: str) -> str:
return key.strip().lower().replace(' ', '_')
def _parse_package_output[PackageType: (AvailablePackage, LocalPackage)](
package_meta: list[str],
cls: type[PackageType]
@ -148,7 +153,7 @@ def _parse_package_output[PackageType: (AvailablePackage, LocalPackage)](
for line in package_meta:
if ':' in line:
key, value = line.split(':', 1)
key = key.strip().lower().replace(' ', '_')
key = _normalize_key_name(key)
package[key] = value.strip()
return cls.model_validate(package)