Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ console = { version = "0.16.0", default-features = false, features = ["std"] }
csv = { version = "1.3.0" }
ctrlc = { version = "3.4.5" }
cyclonedx-bom = { version = "0.8.1" }
dashmap = { version = "6.1.0" }
data-encoding = { version = "2.6.0" }
diskus = { version = "0.9.0", default-features = false }
dotenvy = { version = "0.15.7" }
Expand Down Expand Up @@ -183,6 +182,7 @@ miette = { version = "7.2.0", features = ["fancy-no-backtrace"] }
nix = { version = "0.31.2", features = ["resource", "signal"] }
open = { version = "5.3.2" }
owo-colors = { version = "4.1.0" }
papaya = { version = "0.2.4" }
path-slash = { version = "0.2.1" }
pathdiff = { version = "0.2.1" }
percent-encoding = { version = "2.3.1" }
Expand Down
4 changes: 2 additions & 2 deletions crates/uv-distribution/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3025,15 +3025,15 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> {
};

if let Some(builder) = self.build_context.build_arena().remove(&build_key) {
debug!("Creating build environment for: {source}");
debug!("Reusing existing build environment for: {source}");
let wheel = builder.wheel(temp_dir.path()).await.map_err(Error::Build)?;

// Store the build context.
self.build_context.build_arena().insert(build_key, builder);

wheel
} else {
debug!("Reusing existing build environment for: {source}");
debug!("Creating build environment for: {source}");

let builder = self
.build_context
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-git/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uv-warnings = { workspace = true }

anyhow = { workspace = true }
cargo-util = { workspace = true }
dashmap = { workspace = true }
papaya = { workspace = true }
fs-err = { workspace = true, features = ["tokio"] }
owo-colors = { workspace = true }
reqwest = { workspace = true }
Expand Down
31 changes: 19 additions & 12 deletions crates/uv-git/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;

use dashmap::DashMap;
use dashmap::mapref::one::Ref;
use fs_err::tokio as fs;
use papaya::{HashMap, ResizeMode};
use reqwest_middleware::ClientWithMiddleware;
use tracing::debug;

Expand Down Expand Up @@ -60,18 +59,26 @@ impl GitHttpSettings {
}

/// A resolver for Git repositories.
#[derive(Default, Clone)]
pub struct GitResolver(Arc<DashMap<RepositoryReference, GitOid>>);
#[derive(Clone)]
pub struct GitResolver(Arc<HashMap<RepositoryReference, GitOid>>);

impl Default for GitResolver {
fn default() -> Self {
Self(Arc::new(
HashMap::builder().resize_mode(ResizeMode::Blocking).build(),
))
}
}

impl GitResolver {
/// Inserts a new [`GitOid`] for the given [`RepositoryReference`].
pub fn insert(&self, reference: RepositoryReference, sha: GitOid) {
self.0.insert(reference, sha);
self.0.pin().insert(reference, sha);
}

/// Returns the [`GitOid`] for the given [`RepositoryReference`], if it exists.
fn get(&self, reference: &RepositoryReference) -> Option<Ref<'_, RepositoryReference, GitOid>> {
self.0.get(reference)
fn get(&self, reference: &RepositoryReference) -> Option<GitOid> {
self.0.pin().get(reference).copied()
}

/// Return the [`GitOid`] for the given [`GitUrl`], if it is already known.
Expand All @@ -84,7 +91,7 @@ impl GitResolver {
// If we know the precise commit already, return it.
let reference = RepositoryReference::from(url);
if let Some(precise) = self.get(&reference) {
return Some(*precise);
return Some(precise);
}

None
Expand Down Expand Up @@ -181,7 +188,7 @@ impl GitResolver {
// single process are consistent.
let url = {
if let Some(precise) = self.get(&reference) {
Cow::Owned(url.clone().with_precise(*precise))
Cow::Owned(url.clone().with_precise(precise))
} else {
Cow::Borrowed(url)
}
Expand Down Expand Up @@ -241,7 +248,7 @@ impl GitResolver {
pub fn precise(&self, url: GitUrl) -> Option<GitUrl> {
let reference = RepositoryReference::from(&url);
let precise = self.get(&reference)?;
Some(url.with_precise(*precise))
Some(url.with_precise(precise))
}

/// Returns `true` if the two Git URLs refer to the same precise commit.
Expand All @@ -263,11 +270,11 @@ impl GitResolver {
}

// Otherwise, the URLs must resolve to the same precise commit.
let Some(a_precise) = a.precise().or_else(|| self.get(&a_ref).map(|sha| *sha)) else {
let Some(a_precise) = a.precise().or_else(|| self.get(&a_ref)) else {
return false;
};

let Some(b_precise) = b.precise().or_else(|| self.get(&b_ref).map(|sha| *sha)) else {
let Some(b_precise) = b.precise().or_else(|| self.get(&b_ref)) else {
return false;
};

Expand Down
2 changes: 1 addition & 1 deletion crates/uv-once-map/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ test = false
workspace = true

[dependencies]
dashmap = { workspace = true }
futures = { workspace = true }
papaya = { workspace = true }
tokio = { workspace = true }
55 changes: 24 additions & 31 deletions crates/uv-once-map/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::{Debug, Display, Formatter};
use std::hash::{BuildHasher, Hash, RandomState};
use std::sync::Arc;

use dashmap::{DashMap, Entry};
use papaya::{HashMap, ResizeMode};
use tokio::sync::Notify;

/// The caller tried to wait for a task that was never registered.
Expand All @@ -28,7 +28,7 @@ impl<K: Debug + Display> std::error::Error for UnregisteredTask<K> {}
/// Note that this always clones the value out of the underlying map. Because
/// of this, it's common to wrap the `V` in an `Arc<V>` to make cloning cheap.
pub struct OnceMap<K, V, S = RandomState> {
items: DashMap<K, Value<V>, S>,
items: HashMap<K, Value<V>, S>,
}

impl<K: Eq + Hash + Debug, V: Debug, S: BuildHasher + Clone> Debug for OnceMap<K, V, S> {
Expand All @@ -44,14 +44,10 @@ impl<K: Eq + Hash + Clone, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
/// or other tasks will hang. If it returns `false`, this job is already in progress and you
/// can [`OnceMap::wait`] for the result.
pub fn register(&self, key: K) -> bool {
let entry = self.items.entry(key);
match entry {
Entry::Occupied(_) => false,
Entry::Vacant(entry) => {
entry.insert(Value::Waiting(Arc::new(Notify::new())));
true
}
}
self.items
.pin()
.try_insert(key, Value::Waiting(Arc::new(Notify::new())))
.is_ok()
}

/// Register that you want to start a job, unless it was already started, then wait for its
Expand All @@ -74,43 +70,37 @@ impl<K: Eq + Hash + Clone, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
/// ```
pub async fn register_or_wait(&self, key: &K) -> Option<V> {
let notify = {
let entry = self.items.entry(key.clone());
match entry {
Entry::Occupied(value) => match value.get() {
let items = self.items.pin();
match items.try_insert(key.clone(), Value::Waiting(Arc::new(Notify::new()))) {
Comment thread
konstin marked this conversation as resolved.
Outdated
Ok(_) => return None,
Err(value) => match value.current {
Value::Filled(value) => return Some(value.clone()),
Value::Waiting(notify) => notify.clone(),
},
Entry::Vacant(entry) => {
// We insert the notify even if the caller is `wait`. Calling `wait` without
// a previous `register` is a fatal error, so the state of the map doesn't
// matter.
entry.insert(Value::Waiting(Arc::new(Notify::new())));
return None;
}
}
};

// Register the waiter for calls to `notify_waiters`.
let notification = notify.notified();

// Make sure the value wasn't inserted in-between us checking the map and registering the waiter.
if let Value::Filled(value) = self.items.get(key).expect("map is append-only").value() {
if let Value::Filled(value) = self.items.pin().get(key).expect("map is append-only") {
return Some(value.clone());
}

// Wait until the value is inserted.
notification.await;

let entry = self.items.get(key).expect("map is append-only");
match entry.value() {
let items = self.items.pin();
match items.get(key).expect("map is append-only") {
Value::Filled(value) => Some(value.clone()),
Value::Waiting(_) => unreachable!("notify was called"),
}
}

/// Submit the result of a job you registered.
pub fn done(&self, key: K, value: V) {
if let Some(Value::Waiting(notify)) = self.items.insert(key, Value::Filled(value)) {
if let Some(Value::Waiting(notify)) = self.items.pin().insert(key, Value::Filled(value)) {
notify.notify_waiters();
}
}
Expand Down Expand Up @@ -139,8 +129,8 @@ impl<K: Eq + Hash + Clone, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
where
K: Borrow<Q>,
{
let entry = self.items.get(key)?;
match entry.value() {
let items = self.items.pin();
match items.get(key)? {
Value::Filled(value) => Some(value.clone()),
Value::Waiting(_) => None,
}
Expand All @@ -151,18 +141,21 @@ impl<K: Eq + Hash + Clone, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
where
K: Borrow<Q>,
{
let entry = self.items.remove(key)?;
match entry {
(_, Value::Filled(value)) => Some(value),
(_, Value::Waiting(_)) => None,
let items = self.items.pin();
match items.remove(key)? {
Value::Filled(value) => Some(value.clone()),
Value::Waiting(_) => None,
}
}
}

impl<K: Eq + Hash + Clone, V, H: Default + BuildHasher + Clone> Default for OnceMap<K, V, H> {
fn default() -> Self {
Self {
items: DashMap::with_hasher(H::default()),
items: HashMap::builder()
.hasher(H::default())
.resize_mode(ResizeMode::Blocking)
.build(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ uv-workspace = { workspace = true }
arcstr = { workspace = true }
clap = { workspace = true, features = ["derive"], optional = true }
cyclonedx-bom = { workspace = true }
dashmap = { workspace = true }
papaya = { workspace = true }
either = { workspace = true }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
Expand Down
Loading
Loading