From 849478fa9164d23db8fbb1824be5fb15c953c8b7 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 24 Jun 2024 23:18:16 +0300 Subject: [PATCH] Add unowned settings variants (#4490) ## Summary This PR adds unowned settings variants so that we can convert from `ResolverInstallerSettings` to `ResolverSettings` without allocating. --- crates/uv/src/commands/project/add.rs | 4 +- crates/uv/src/commands/project/lock.rs | 20 ++--- crates/uv/src/commands/project/remove.rs | 4 +- crates/uv/src/commands/project/run.rs | 4 +- crates/uv/src/commands/project/sync.rs | 22 +++--- crates/uv/src/settings.rs | 95 +++++++++++++++++++++++- 6 files changed, 118 insertions(+), 31 deletions(-) diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index f1e4bfb57..c82721878 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -186,7 +186,7 @@ pub(crate) async fn add( let lock = project::lock::do_lock( project.workspace(), venv.interpreter(), - settings.clone().into(), + settings.as_ref().into(), preview, connectivity, concurrency, @@ -209,7 +209,7 @@ pub(crate) async fn add( extras, dev, Modifications::Sufficient, - settings.into(), + settings.as_ref().into(), preview, connectivity, concurrency, diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index 20ec9e730..17a8545d0 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -19,7 +19,7 @@ use uv_warnings::warn_user; use crate::commands::project::{find_requires_python, ProjectError}; use crate::commands::{pip, project, ExitStatus}; use crate::printer::Printer; -use crate::settings::ResolverSettings; +use crate::settings::{ResolverSettings, ResolverSettingsRef}; /// Resolve the project requirements into a lockfile. #[allow(clippy::too_many_arguments)] @@ -57,7 +57,7 @@ pub(crate) async fn lock( match do_lock( &workspace, &interpreter, - settings, + settings.as_ref(), preview, connectivity, concurrency, @@ -85,7 +85,7 @@ pub(crate) async fn lock( pub(super) async fn do_lock( workspace: &Workspace, interpreter: &Interpreter, - settings: ResolverSettings, + settings: ResolverSettingsRef<'_>, preview: PreviewMode, connectivity: Connectivity, concurrency: Concurrency, @@ -94,7 +94,7 @@ pub(super) async fn do_lock( printer: Printer, ) -> Result { // Extract the project settings. - let ResolverSettings { + let ResolverSettingsRef { index_locations, index_strategy, keyring_provider, @@ -193,11 +193,11 @@ pub(super) async fn do_lock( let flat_index = { let client = FlatIndexClient::new(&client, cache); let entries = client.fetch(index_locations.flat_index()).await?; - FlatIndex::from_entries(entries, None, &hasher, &build_options) + FlatIndex::from_entries(entries, None, &hasher, build_options) }; // If an existing lockfile exists, build up a set of preferences. - let LockedRequirements { preferences, git } = read_lockfile(workspace, &upgrade).await?; + let LockedRequirements { preferences, git } = read_lockfile(workspace, upgrade).await?; // Create the Git resolver. let git = GitResolver::from_refs(git); @@ -207,17 +207,17 @@ pub(super) async fn do_lock( &client, cache, interpreter, - &index_locations, + index_locations, &flat_index, &index, &git, &in_flight, index_strategy, setup_py, - &config_setting, + config_setting, build_isolation, link_mode, - &build_options, + build_options, exclude_newer, concurrency, preview, @@ -236,7 +236,7 @@ pub(super) async fn do_lock( EmptyInstalledPackages, &hasher, &Reinstall::default(), - &upgrade, + upgrade, interpreter, None, None, diff --git a/crates/uv/src/commands/project/remove.rs b/crates/uv/src/commands/project/remove.rs index ef230a4cb..da95a1498 100644 --- a/crates/uv/src/commands/project/remove.rs +++ b/crates/uv/src/commands/project/remove.rs @@ -101,7 +101,7 @@ pub(crate) async fn remove( let lock = project::lock::do_lock( project.workspace(), venv.interpreter(), - settings, + settings.as_ref(), preview, connectivity, concurrency, @@ -125,7 +125,7 @@ pub(crate) async fn remove( extras, dev, Modifications::Exact, - settings, + settings.as_ref(), preview, connectivity, concurrency, diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index 7a3b4ec33..42581d582 100644 --- a/crates/uv/src/commands/project/run.rs +++ b/crates/uv/src/commands/project/run.rs @@ -78,7 +78,7 @@ pub(crate) async fn run( let lock = project::lock::do_lock( project.workspace(), venv.interpreter(), - settings.clone().into(), + settings.as_ref().into(), preview, connectivity, concurrency, @@ -95,7 +95,7 @@ pub(crate) async fn run( extras, dev, Modifications::Sufficient, - settings.clone().into(), + settings.as_ref().into(), preview, connectivity, concurrency, diff --git a/crates/uv/src/commands/project/sync.rs b/crates/uv/src/commands/project/sync.rs index b45a682b9..4c7ed23cb 100644 --- a/crates/uv/src/commands/project/sync.rs +++ b/crates/uv/src/commands/project/sync.rs @@ -19,7 +19,7 @@ use crate::commands::pip::operations::Modifications; use crate::commands::project::ProjectError; use crate::commands::{pip, project, ExitStatus}; use crate::printer::Printer; -use crate::settings::InstallerSettings; +use crate::settings::{InstallerSettings, InstallerSettingsRef}; /// Sync the project environment. #[allow(clippy::too_many_arguments)] @@ -72,7 +72,7 @@ pub(crate) async fn sync( extras, dev, modifications, - settings, + settings.as_ref(), preview, connectivity, concurrency, @@ -95,7 +95,7 @@ pub(super) async fn do_sync( extras: ExtrasSpecification, dev: bool, modifications: Modifications, - settings: InstallerSettings, + settings: InstallerSettingsRef<'_>, preview: PreviewMode, connectivity: Connectivity, concurrency: Concurrency, @@ -104,7 +104,7 @@ pub(super) async fn do_sync( printer: Printer, ) -> Result<(), ProjectError> { // Extract the project settings. - let InstallerSettings { + let InstallerSettingsRef { index_locations, index_strategy, keyring_provider, @@ -167,7 +167,7 @@ pub(super) async fn do_sync( let flat_index = { let client = FlatIndexClient::new(&client, cache); let entries = client.fetch(index_locations.flat_index()).await?; - FlatIndex::from_entries(entries, Some(tags), &hasher, &build_options) + FlatIndex::from_entries(entries, Some(tags), &hasher, build_options) }; // Create a build dispatch. @@ -175,17 +175,17 @@ pub(super) async fn do_sync( &client, cache, venv.interpreter(), - &index_locations, + index_locations, &flat_index, &index, &git, &in_flight, index_strategy, setup_py, - &config_setting, + config_setting, build_isolation, link_mode, - &build_options, + build_options, exclude_newer, concurrency, preview, @@ -198,11 +198,11 @@ pub(super) async fn do_sync( &resolution, site_packages, modifications, - &reinstall, - &build_options, + reinstall, + build_options, link_mode, compile_bytecode, - &index_locations, + index_locations, &hasher, tags, &client, diff --git a/crates/uv/src/settings.rs b/crates/uv/src/settings.rs index 96296ea7d..fe99759cd 100644 --- a/crates/uv/src/settings.rs +++ b/crates/uv/src/settings.rs @@ -1087,6 +1087,18 @@ pub(crate) struct InstallerSettings { pub(crate) build_options: BuildOptions, } +#[derive(Debug, Clone)] +pub(crate) struct InstallerSettingsRef<'a> { + pub(crate) index_locations: &'a IndexLocations, + pub(crate) index_strategy: IndexStrategy, + pub(crate) keyring_provider: KeyringProviderType, + pub(crate) config_setting: &'a ConfigSettings, + pub(crate) link_mode: LinkMode, + pub(crate) compile_bytecode: bool, + pub(crate) reinstall: &'a Reinstall, + pub(crate) build_options: &'a BuildOptions, +} + impl InstallerSettings { /// Resolve the [`InstallerSettings`] from the CLI and filesystem configuration. pub(crate) fn combine(args: InstallerOptions, filesystem: Option) -> Self { @@ -1164,6 +1176,19 @@ impl InstallerSettings { ), } } + + pub(crate) fn as_ref(&self) -> InstallerSettingsRef { + InstallerSettingsRef { + index_locations: &self.index_locations, + index_strategy: self.index_strategy, + keyring_provider: self.keyring_provider, + config_setting: &self.config_setting, + link_mode: self.link_mode, + compile_bytecode: self.compile_bytecode, + reinstall: &self.reinstall, + build_options: &self.build_options, + } + } } /// The resolved settings to use for an invocation of the `uv` CLI when resolving dependencies. @@ -1185,6 +1210,20 @@ pub(crate) struct ResolverSettings { pub(crate) build_options: BuildOptions, } +#[derive(Debug, Clone)] +pub(crate) struct ResolverSettingsRef<'a> { + pub(crate) index_locations: &'a IndexLocations, + pub(crate) index_strategy: IndexStrategy, + pub(crate) keyring_provider: KeyringProviderType, + pub(crate) resolution: ResolutionMode, + pub(crate) prerelease: PreReleaseMode, + pub(crate) config_setting: &'a ConfigSettings, + pub(crate) exclude_newer: Option, + pub(crate) link_mode: LinkMode, + pub(crate) upgrade: &'a Upgrade, + pub(crate) build_options: &'a BuildOptions, +} + impl ResolverSettings { /// Resolve the [`ResolverSettings`] from the CLI and filesystem configuration. pub(crate) fn combine(args: ResolverOptions, filesystem: Option) -> Self { @@ -1261,6 +1300,21 @@ impl ResolverSettings { ), } } + + pub(crate) fn as_ref(&self) -> ResolverSettingsRef { + ResolverSettingsRef { + index_locations: &self.index_locations, + index_strategy: self.index_strategy, + keyring_provider: self.keyring_provider, + resolution: self.resolution, + prerelease: self.prerelease, + config_setting: &self.config_setting, + exclude_newer: self.exclude_newer, + link_mode: self.link_mode, + upgrade: &self.upgrade, + build_options: &self.build_options, + } + } } /// The resolved settings to use for an invocation of the `uv` CLI with both resolver and installer @@ -1285,6 +1339,22 @@ pub(crate) struct ResolverInstallerSettings { pub(crate) build_options: BuildOptions, } +#[derive(Debug, Clone)] +pub(crate) struct ResolverInstallerSettingsRef<'a> { + pub(crate) index_locations: &'a IndexLocations, + pub(crate) index_strategy: IndexStrategy, + pub(crate) keyring_provider: KeyringProviderType, + pub(crate) resolution: ResolutionMode, + pub(crate) prerelease: PreReleaseMode, + pub(crate) config_setting: &'a ConfigSettings, + pub(crate) exclude_newer: Option, + pub(crate) link_mode: LinkMode, + pub(crate) compile_bytecode: bool, + pub(crate) upgrade: &'a Upgrade, + pub(crate) reinstall: &'a Reinstall, + pub(crate) build_options: &'a BuildOptions, +} + impl ResolverInstallerSettings { /// Resolve the [`ResolverInstallerSettings`] from the CLI and filesystem configuration. pub(crate) fn combine( @@ -1374,6 +1444,23 @@ impl ResolverInstallerSettings { ), } } + + pub(crate) fn as_ref(&self) -> ResolverInstallerSettingsRef { + ResolverInstallerSettingsRef { + index_locations: &self.index_locations, + index_strategy: self.index_strategy, + keyring_provider: self.keyring_provider, + resolution: self.resolution, + prerelease: self.prerelease, + config_setting: &self.config_setting, + exclude_newer: self.exclude_newer, + link_mode: self.link_mode, + compile_bytecode: self.compile_bytecode, + upgrade: &self.upgrade, + reinstall: &self.reinstall, + build_options: &self.build_options, + } + } } /// The resolved settings to use for an invocation of the `pip` CLI. @@ -1681,8 +1768,8 @@ impl PipSettings { } } -impl From for ResolverSettings { - fn from(settings: ResolverInstallerSettings) -> Self { +impl<'a> From> for ResolverSettingsRef<'a> { + fn from(settings: ResolverInstallerSettingsRef<'a>) -> Self { Self { index_locations: settings.index_locations, index_strategy: settings.index_strategy, @@ -1698,8 +1785,8 @@ impl From for ResolverSettings { } } -impl From for InstallerSettings { - fn from(settings: ResolverInstallerSettings) -> Self { +impl<'a> From> for InstallerSettingsRef<'a> { + fn from(settings: ResolverInstallerSettingsRef<'a>) -> Self { Self { index_locations: settings.index_locations, index_strategy: settings.index_strategy,