Skip to content

Commit

Permalink
Prefer optimized builds for freethreaded Python downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
zanieb committed Oct 15, 2024
1 parent c683191 commit 5b78c86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
12 changes: 6 additions & 6 deletions crates/uv-python/download-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@
"minor": 13,
"patch": 0,
"prerelease": "",
"url": "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-aarch64-apple-darwin-freethreaded%2Bdebug-full.tar.zst",
"sha256": "4ba7f477c56af4f057ca40535aa6907662b7206e3b8b39971d0a507b1c955e44",
"url": "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-aarch64-apple-darwin-freethreaded%2Bpgo%2Blto-full.tar.zst",
"sha256": "8cc1586c4ee730bb33b7e6d39f1b6388f895075fadb1771e3c27b0561abb9242",
"variant": "freethreaded"
},
"cpython-3.13.0+freethreaded-darwin-x86_64-none": {
Expand All @@ -164,8 +164,8 @@
"minor": 13,
"patch": 0,
"prerelease": "",
"url": "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-apple-darwin-freethreaded%2Bdebug-full.tar.zst",
"sha256": "b72b7df7cd22a4e7462dbe95633c5ca61caab41237d68e0ff89e9774cd4985e7",
"url": "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-apple-darwin-freethreaded%2Bpgo%2Blto-full.tar.zst",
"sha256": "117528b68096379b1303faee1f4f9e32ef3d255207ec92fb063f1bd0b942549d",
"variant": "freethreaded"
},
"cpython-3.13.0+freethreaded-linux-aarch64-gnu": {
Expand Down Expand Up @@ -242,8 +242,8 @@
"minor": 13,
"patch": 0,
"prerelease": "",
"url": "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-unknown-linux-gnu-freethreaded%2Bdebug-full.tar.zst",
"sha256": "2b46a0f38711cfcdffab90f69ea1372288bd6dcb0f9676b765babb491d42ce06",
"url": "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-unknown-linux-gnu-freethreaded%2Bpgo%2Blto-full.tar.zst",
"sha256": "00a159a64640ce614bdac064b270a9854d86d40d1d8387a822daf1fe0881e64b",
"variant": "freethreaded"
},
"cpython-3.13.0+freethreaded-windows-i686-none": {
Expand Down
41 changes: 30 additions & 11 deletions crates/uv-python/fetch-download-metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -211,14 +212,15 @@ async def _fetch_downloads(self, pages: int = 100) -> list[PythonDownload]:
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
Expand Down Expand Up @@ -301,10 +303,10 @@ def _parse_download_url(self, url: str) -> PythonDownload | None:

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
Expand All @@ -322,6 +324,7 @@ def _parse_download_url(self, url: str) -> PythonDownload | None:
implementation=self.implementation,
filename=filename,
url=url,
build_options=build_options,
variant=variant,
)

Expand Down Expand Up @@ -355,13 +358,29 @@ def _normalize_arch(self, arch: str) -> str:
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):
Expand Down
12 changes: 6 additions & 6 deletions crates/uv-python/src/downloads.inc
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ pub(crate) const PYTHON_DOWNLOADS: &[ManagedPythonDownload] = &[
variant: PythonVariant::Freethreaded

},
url: "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-aarch64-apple-darwin-freethreaded%2Bdebug-full.tar.zst",
sha256: Some("4ba7f477c56af4f057ca40535aa6907662b7206e3b8b39971d0a507b1c955e44")
url: "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-aarch64-apple-darwin-freethreaded%2Bpgo%2Blto-full.tar.zst",
sha256: Some("8cc1586c4ee730bb33b7e6d39f1b6388f895075fadb1771e3c27b0561abb9242")
},
ManagedPythonDownload {
key: PythonInstallationKey {
Expand All @@ -212,8 +212,8 @@ pub(crate) const PYTHON_DOWNLOADS: &[ManagedPythonDownload] = &[
variant: PythonVariant::Freethreaded

},
url: "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-apple-darwin-freethreaded%2Bdebug-full.tar.zst",
sha256: Some("b72b7df7cd22a4e7462dbe95633c5ca61caab41237d68e0ff89e9774cd4985e7")
url: "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-apple-darwin-freethreaded%2Bpgo%2Blto-full.tar.zst",
sha256: Some("117528b68096379b1303faee1f4f9e32ef3d255207ec92fb063f1bd0b942549d")
},
ManagedPythonDownload {
key: PythonInstallationKey {
Expand Down Expand Up @@ -308,8 +308,8 @@ pub(crate) const PYTHON_DOWNLOADS: &[ManagedPythonDownload] = &[
variant: PythonVariant::Freethreaded

},
url: "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-unknown-linux-gnu-freethreaded%2Bdebug-full.tar.zst",
sha256: Some("2b46a0f38711cfcdffab90f69ea1372288bd6dcb0f9676b765babb491d42ce06")
url: "https://github.com/indygreg/python-build-standalone/releases/download/20241008/cpython-3.13.0%2B20241008-x86_64-unknown-linux-gnu-freethreaded%2Bpgo%2Blto-full.tar.zst",
sha256: Some("00a159a64640ce614bdac064b270a9854d86d40d1d8387a822daf1fe0881e64b")
},
ManagedPythonDownload {
key: PythonInstallationKey {
Expand Down

0 comments on commit 5b78c86

Please sign in to comment.