Skip to content

Commit

Permalink
refactor(source): Reuse the package hash map
Browse files Browse the repository at this point in the history
Primary benefit: looking to track more state in `RecursivePathSource`
and this is a stepping stone.

Minor benefits:
- Cleaner
- Avoid re-allocating
- Faster lookup for `download`
  • Loading branch information
epage committed Jul 11, 2024
1 parent 5261386 commit 848dd7f
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub struct RecursivePathSource<'gctx> {
/// Whether this source has loaded all package information it may contain.
loaded: bool,
/// Packages that this sources has discovered.
packages: Vec<Package>,
packages: HashMap<PackageId, Package>,
gctx: &'gctx GlobalContext,
}

Expand All @@ -244,7 +244,7 @@ impl<'gctx> RecursivePathSource<'gctx> {
source_id,
path: root.to_path_buf(),
loaded: false,
packages: Vec::new(),
packages: Default::default(),
gctx,
}
}
Expand All @@ -253,7 +253,7 @@ impl<'gctx> RecursivePathSource<'gctx> {
/// filesystem if package information haven't yet loaded.
pub fn read_packages(&mut self) -> CargoResult<Vec<Package>> {
self.load()?;
Ok(self.packages.clone())
Ok(self.packages.iter().map(|(_, v)| v.clone()).collect())
}

/// List all files relevant to building this package inside this source.
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<'gctx> Source for RecursivePathSource<'gctx> {
f: &mut dyn FnMut(IndexSummary),
) -> Poll<CargoResult<()>> {
self.load()?;
for s in self.packages.iter().map(|p| p.summary()) {
for s in self.packages.values().map(|p| p.summary()) {
let matched = match kind {
QueryKind::Exact => dep.matches(s),
QueryKind::Alternatives => true,
Expand Down Expand Up @@ -339,7 +339,7 @@ impl<'gctx> Source for RecursivePathSource<'gctx> {
fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
trace!("getting packages; id={}", id);
self.load()?;
let pkg = self.packages.iter().find(|pkg| pkg.package_id() == id);
let pkg = self.packages.get(&id);
pkg.cloned()
.map(MaybePackage::Ready)
.ok_or_else(|| internal(format!("failed to find {} in path source", id)))
Expand Down Expand Up @@ -758,7 +758,7 @@ fn read_packages(
path: &Path,
source_id: SourceId,
gctx: &GlobalContext,
) -> CargoResult<Vec<Package>> {
) -> CargoResult<HashMap<PackageId, Package>> {
let mut all_packages = HashMap::new();
let mut visited = HashSet::<PathBuf>::new();
let mut errors = Vec::<anyhow::Error>::new();
Expand Down Expand Up @@ -823,7 +823,7 @@ fn read_packages(
}
}
} else {
Ok(all_packages.into_iter().map(|(_, v)| v).collect())
Ok(all_packages)
}
}

Expand Down

0 comments on commit 848dd7f

Please sign in to comment.