Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(source): More RecursivePathSource clean up #14231

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
refactor(source): Reuse the package hash map
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
commit 848dd7f1cd2a503c83b724776e2d50adbd07e09c
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