mirror of
https://github.com/astral-sh/uv.git
synced 2025-10-14 20:39:37 +00:00
Prefer optimized builds for freethreaded Python downloads (#8196)
Addresses report in https://github.com/astral-sh/uv/issues/8194
This commit is contained in:
parent
689611417b
commit
b697cee3e1
3 changed files with 42 additions and 23 deletions
|
@ -50,7 +50,7 @@ import json
|
|||
import logging
|
||||
import os
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
from enum import StrEnum
|
||||
from pathlib import Path
|
||||
from typing import Generator, Iterable, NamedTuple, Self
|
||||
|
@ -120,6 +120,7 @@ class PythonDownload:
|
|||
filename: str
|
||||
url: str
|
||||
sha256: str | None = None
|
||||
build_options: list[str] = field(default_factory=list)
|
||||
variant: Variant | None = None
|
||||
|
||||
def key(self) -> str:
|
||||
|
@ -211,14 +212,15 @@ class CPythonFinder(Finder):
|
|||
download
|
||||
)
|
||||
|
||||
# Collapse CPython variants to a single URL flavor per triple and variant
|
||||
# Collapse CPython variants to a single flavor per triple and variant
|
||||
downloads = []
|
||||
for version_downloads in downloads_by_version.values():
|
||||
selected: dict[
|
||||
tuple[PlatformTriple, Variant | None], tuple[PythonDownload, int]
|
||||
tuple[PlatformTriple, Variant | None],
|
||||
tuple[PythonDownload, tuple[int, int]],
|
||||
] = {}
|
||||
for download in version_downloads:
|
||||
priority = self._get_flavor_priority(download.flavor)
|
||||
priority = self._get_priority(download)
|
||||
existing = selected.get((download.triple, download.variant))
|
||||
if existing:
|
||||
existing_download, existing_priority = existing
|
||||
|
@ -301,10 +303,10 @@ class CPythonFinder(Finder):
|
|||
|
||||
version, _date, triple, build_options, flavor = match.groups()
|
||||
|
||||
variants = build_options.split("+") if build_options else []
|
||||
build_options = build_options.split("+") if build_options else []
|
||||
variant: Variant | None
|
||||
for variant in Variant:
|
||||
if variant in variants:
|
||||
if variant in build_options:
|
||||
break
|
||||
else:
|
||||
variant = None
|
||||
|
@ -322,6 +324,7 @@ class CPythonFinder(Finder):
|
|||
implementation=self.implementation,
|
||||
filename=filename,
|
||||
url=url,
|
||||
build_options=build_options,
|
||||
variant=variant,
|
||||
)
|
||||
|
||||
|
@ -355,13 +358,29 @@ class CPythonFinder(Finder):
|
|||
def _normalize_os(self, os: str) -> str:
|
||||
return os
|
||||
|
||||
def _get_flavor_priority(self, flavor: str) -> int:
|
||||
"""Returns the priority of a flavor. Lower is better."""
|
||||
def _get_priority(self, download: PythonDownload) -> tuple[int, int]:
|
||||
"""
|
||||
Returns the priority of a download, a lower score is better.
|
||||
"""
|
||||
flavor_priority = self._flavor_priority(download.flavor)
|
||||
build_option_priority = self._build_option_priority(download.build_options)
|
||||
return (flavor_priority, build_option_priority)
|
||||
|
||||
def _flavor_priority(self, flavor: str) -> int:
|
||||
try:
|
||||
pref = self.FLAVOR_PREFERENCES.index(flavor)
|
||||
priority = self.FLAVOR_PREFERENCES.index(flavor)
|
||||
except ValueError:
|
||||
pref = len(self.FLAVOR_PREFERENCES) + 1
|
||||
return pref
|
||||
priority = len(self.FLAVOR_PREFERENCES) + 1
|
||||
return priority
|
||||
|
||||
def _build_option_priority(self, build_options: list[str]) -> int:
|
||||
# Prefer optimized builds
|
||||
return -1 * sum(
|
||||
(
|
||||
"lgo" in build_options,
|
||||
"pgo" in build_options,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class PyPyFinder(Finder):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue