8000
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000 Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions crates/uv-client/src/registry_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ pub struct RegistryClient {
connectivity: Connectivity,
/// Client HTTP read timeout.
read_timeout: Duration,
/// The flat index entries for each `--find-links`-style index URL.
/// The flat index entries for each `--find-links`-style index URL, with one slot per index.
flat_indexes: Arc<Mutex<FlatIndexCache>>,
/// The pyx token store to use for persistent credentials.
// TODO(charlie): The token store is only needed for `is_known_url`; can we avoid storing it here?
Expand Down Expand Up @@ -459,11 +459,15 @@ impl RegistryClient {
package_name: &PackageName,
index: &IndexUrl,
) -> Result<Vec<FlatIndexEntry>, Error> {
// Store the flat index entries in a cache, to avoid redundant fetches. A flat index will
// typically contain entries for multiple packages; as such, it's more efficient to cache
// the entire index rather than re-fetching it for each package.
let mut cache = self.flat_indexes.lock().await;
if let Some(entries) = cache.get(index) {
// Each flat index gets its own slot, so lookups for the same index share a fetch while
// unrelated indexes can proceed concurrently.
let flat_index_slot = {
let mut cache = self.flat_indexes.lock().await;
cache.get_or_insert(index)
};
let mut flat_index = flat_index_slot.lock().await;

if let Some(entries) = flat_index.as_ref() {
return Ok(entries.get(package_name).cloned().unwrap_or_default());
}

Expand All @@ -488,7 +492,7 @@ impl RegistryClient {
.unwrap_or_default();

// Write to the cache.
cache.insert(index.clone(), entries_by_package);
*flat_index = Some(entries_by_package);

Ok(package_entries)
}
Expand Down Expand Up @@ -1274,25 +1278,22 @@ impl From<IndexStatusCodeDecision> for SimpleMetadataSearchOutcome {

/// A map from [`IndexUrl`] to [`FlatIndexEntry`] entries found at the given URL, indexed by
/// [`PackageName`].
#[derive(Default, Debug, Clone)]
struct FlatIndexCache(FxHashMap<IndexUrl, FxHashMap<PackageName, Vec<FlatIndexEntry>>>);
#[derive(Default, Debug)]
struct FlatIndexCache(FxHashMap<IndexUrl, FlatIndexSlot>);

impl FlatIndexCache {
/// Get the entries for a given index URL.
fn get(&self, index: &IndexUrl) -> Option<&FxHashMap<PackageName, Vec<FlatIndexEntry>>> {
self.0.get(index)
}

/// Insert the entries for a given index URL.
fn insert(
&mut self,
index: IndexUrl,
entries: FxHashMap<PackageName, Vec<FlatIndexEntry>>,
) -> Option<FxHashMap<PackageName, Vec<FlatIndexEntry>>> {
self.0.insert(index, entries)
/// Return the per-index slot for this flat index, creating it on first access.
fn get_or_insert(&mut self, index: &IndexUrl) -> FlatIndexSlot {
self.0
.entry(index.clone())
.or_insert_with(|| Arc::new(Mutex::new(None)))
.clone()
}
}

type FlatIndexEntriesByPackage = FxHashMap<PackageName, Vec<FlatIndexEntry>>;
type FlatIndexSlot = Arc<Mutex<Option<FlatIndexEntriesByPackage>>>;

#[derive(Default, Debug, rkyv::Archive, rkyv::Deserialize, rkyv::Serialize)]
#[rkyv(derive(Debug))]
pub struct VersionFiles {
Expand Down
Loading
0