Skip to content

Commit cb606d6

Browse files
committed
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`
1 parent 1e0d33e commit cb606d6

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/cargo/sources/path.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ pub struct RecursivePathSource<'gctx> {
226226
/// Whether this source has loaded all package information it may contain.
227227
loaded: bool,
228228
/// Packages that this sources has discovered.
229-
packages: Vec<Package>,
229+
packages: HashMap<PackageId, Package>,
230230
gctx: &'gctx GlobalContext,
231231
}
232232

@@ -244,7 +244,7 @@ impl<'gctx> RecursivePathSource<'gctx> {
244244
source_id,
245245
path: root.to_path_buf(),
246246
loaded: false,
247-
packages: Vec::new(),
247+
packages: Default::default(),
248248
gctx,
249249
}
250250
}
@@ -253,7 +253,7 @@ impl<'gctx> RecursivePathSource<'gctx> {
253253
/// filesystem if package information haven't yet loaded.
254254
pub fn read_packages(&mut self) -> CargoResult<Vec<Package>> {
255255
self.load()?;
256-
Ok(self.packages.clone())
256+
Ok(self.packages.iter().map(|(_, v)| v.clone()).collect())
257257
}
258258

259259
/// List all files relevant to building this package inside this source.
@@ -311,7 +311,7 @@ impl<'gctx> Source for RecursivePathSource<'gctx> {
311311
f: &mut dyn FnMut(IndexSummary),
312312
) -> Poll<CargoResult<()>> {
313313
self.load()?;
314-
for s in self.packages.iter().map(|p| p.summary()) {
314+
for s in self.packages.values().map(|p| p.summary()) {
315315
let matched = match kind {
316316
QueryKind::Exact => dep.matches(s),
317317
QueryKind::Alternatives => true,
@@ -339,7 +339,7 @@ impl<'gctx> Source for RecursivePathSource<'gctx> {
339339
fn download(&mut self, id: PackageId) -> CargoResult<MaybePackage> {
340340
trace!("getting packages; id={}", id);
341341
self.load()?;
342-
let pkg = self.packages.iter().find(|pkg| pkg.package_id() == id);
342+
let pkg = self.packages.get(&id);
343343
pkg.cloned()
344344
.map(MaybePackage::Ready)
345345
.ok_or_else(|| internal(format!("failed to find {} in path source", id)))
@@ -758,7 +758,7 @@ fn read_packages(
758758
path: &Path,
759759
source_id: SourceId,
760760
gctx: &GlobalContext,
761-
) -> CargoResult<Vec<Package>> {
761+
) -> CargoResult<HashMap<PackageId, Package>> {
762762
let mut all_packages = HashMap::new();
763763
let mut visited = HashSet::<PathBuf>::new();
764764
let mut errors = Vec::<anyhow::Error>::new();
@@ -823,7 +823,7 @@ fn read_packages(
823823
}
824824
}
825825
} else {
826-
Ok(all_packages.into_iter().map(|(_, v)| v).collect())
826+
Ok(all_packages)
827827
}
828828
}
829829

0 commit comments

Comments
 (0)