Skip to content

Commit

Permalink
Avoid cloning script when discovering interpreter (#10172)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Dec 26, 2024
1 parent 79dce73 commit 7411255
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
41 changes: 41 additions & 0 deletions crates/uv-scripts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,47 @@ impl Pep723Item {
}
}

/// A reference to a PEP 723 item.
#[derive(Debug)]
pub enum Pep723ItemRef<'item> {
/// A PEP 723 script read from disk.
Script(&'item Pep723Script),
/// A PEP 723 script provided via `stdin`.
Stdin(&'item Pep723Metadata),
/// A PEP 723 script provided via a remote URL.
Remote(&'item Pep723Metadata),
}

impl Pep723ItemRef<'_> {
/// Return the [`Pep723Metadata`] associated with the item.
pub fn metadata(&self) -> &Pep723Metadata {
match self {
Self::Script(script) => &script.metadata,
Self::Stdin(metadata) => metadata,
Self::Remote(metadata) => metadata,
}
}

/// Return the path of the PEP 723 item, if any.
pub fn path(&self) -> Option<&Path> {
match self {
Self::Script(script) => Some(&script.path),
Self::Stdin(_) => None,
Self::Remote(_) => None,
}
}
}

impl<'item> From<&'item Pep723Item> for Pep723ItemRef<'item> {
fn from(item: &'item Pep723Item) -> Self {
match item {
Pep723Item::Script(script) => Self::Script(script),
Pep723Item::Stdin(metadata) => Self::Stdin(metadata),
Pep723Item::Remote(metadata) => Self::Remote(metadata),
}
}
}

/// A PEP 723 script, including its [`Pep723Metadata`].
#[derive(Debug, Clone)]
pub struct Pep723Script {
Expand Down
4 changes: 2 additions & 2 deletions crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use uv_pypi_types::{redact_credentials, ParsedUrl, RequirementSource, VerbatimPa
use uv_python::{Interpreter, PythonDownloads, PythonEnvironment, PythonPreference, PythonRequest};
use uv_requirements::{NamedRequirementsResolver, RequirementsSource, RequirementsSpecification};
use uv_resolver::FlatIndex;
use uv_scripts::{Pep723Item, Pep723Script};
use uv_scripts::{Pep723ItemRef, Pep723Script};
use uv_settings::PythonInstallMirrors;
use uv_types::{BuildIsolation, HashStrategy};
use uv_warnings::warn_user_once;
Expand Down Expand Up @@ -158,7 +158,7 @@ pub(crate) async fn add(

// Discover the interpreter.
let interpreter = ScriptInterpreter::discover(
&Pep723Item::Script(script.clone()),
Pep723ItemRef::Script(&script),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
Expand Down
6 changes: 3 additions & 3 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use uv_resolver::{
FlatIndex, Lock, OptionsBuilder, PythonRequirement, RequiresPython, ResolverEnvironment,
ResolverOutput,
};
use uv_scripts::Pep723Item;
use uv_scripts::Pep723ItemRef;
use uv_settings::PythonInstallMirrors;
use uv_types::{BuildIsolation, EmptyInstalledPackages, HashStrategy};
use uv_warnings::{warn_user, warn_user_once};
Expand Down Expand Up @@ -444,7 +444,7 @@ pub(crate) struct ScriptInterpreter(Interpreter);
impl ScriptInterpreter {
/// Discover the interpreter to use for the current [`Pep723Item`].
pub(crate) async fn discover(
script: &Pep723Item,
script: Pep723ItemRef<'_>,
python_request: Option<PythonRequest>,
python_preference: PythonPreference,
python_downloads: PythonDownloads,
Expand Down Expand Up @@ -795,7 +795,7 @@ impl ScriptPython {
pub(crate) async fn from_request(
python_request: Option<PythonRequest>,
workspace: Option<&Workspace>,
script: &Pep723Item,
script: Pep723ItemRef<'_>,
no_config: bool,
) -> Result<Self, ProjectError> {
// First, discover a requirement from the workspace
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub(crate) async fn run(

// Discover the interpreter for the script.
let interpreter = ScriptInterpreter::discover(
&script,
(&script).into(),
python.as_deref().map(PythonRequest::parse),
python_preference,
python_downloads,
Expand Down

0 comments on commit 7411255

Please sign in to comment.