Skip to content

Commit 0c2f7d8

Browse files
committed
flatten artifacts
1 parent 26a2657 commit 0c2f7d8

File tree

2 files changed

+42
-60
lines changed

2 files changed

+42
-60
lines changed

crates/compilers/src/artifact_output/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mod hh;
3232
pub use hh::*;
3333

3434
use crate::{
35-
cache::{CachedArtifacts, CompilerCache},
35+
cache::{CachedArtifact, CompilerCache},
3636
output::{
3737
contracts::VersionedContracts,
3838
sources::{VersionedSourceFile, VersionedSourceFiles},
@@ -860,8 +860,7 @@ pub trait ArtifactOutput {
860860
.existing_artifacts
861861
.values()
862862
.flat_map(|artifacts| artifacts.values())
863-
.flat_map(|artifacts| artifacts.values())
864-
.flat_map(|artifacts| artifacts.values())
863+
.flatten()
865864
.map(|a| a.path.to_slash_lossy().to_lowercase())
866865
.collect::<HashSet<_>>();
867866

@@ -1029,7 +1028,7 @@ pub struct OutputContext<'a> {
10291028
/// └── inner
10301029
/// └── a.sol
10311030
/// ```
1032-
pub existing_artifacts: BTreeMap<&'a Path, &'a CachedArtifacts>,
1031+
pub existing_artifacts: BTreeMap<&'a Path, &'a BTreeMap<String, Vec<CachedArtifact>>>,
10331032
}
10341033

10351034
// === impl OutputContext
@@ -1058,10 +1057,10 @@ impl<'a> OutputContext<'a> {
10581057
profile: &str,
10591058
) -> Option<&Path> {
10601059
self.existing_artifacts
1061-
.get(file)
1062-
.and_then(|contracts| contracts.get(contract))
1063-
.and_then(|versions| versions.get(version))
1064-
.and_then(|profiles| profiles.get(profile))
1060+
.get(file)?
1061+
.get(contract)?
1062+
.iter()
1063+
.find(|a| a.version == *version && a.profile == *profile)
10651064
.map(|a| a.path.as_path())
10661065
}
10671066
}

crates/compilers/src/cache.rs

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ impl<S: CompilerSettings> CompilerCache<S> {
157157
if !self
158158
.entries()
159159
.flat_map(|e| e.artifacts.values())
160-
.flat_map(|a| a.values())
161-
.flat_map(|a| a.values())
160+
.flatten()
162161
.any(|a| a.build_id == *build_id)
163162
{
164163
outdated.push(build_id.to_owned());
@@ -395,10 +394,12 @@ pub struct CachedArtifact {
395394
pub path: PathBuf,
396395
/// Build id which produced the given artifact.
397396
pub build_id: String,
397+
/// Compiler version used to produce the artifact.
398+
pub version: Version,
399+
/// Profile used to produce the artifact.
400+
pub profile: String,
398401
}
399402

400-
pub type CachedArtifacts = BTreeMap<String, BTreeMap<Version, BTreeMap<String, CachedArtifact>>>;
401-
402403
/// A `CacheEntry` in the cache file represents a solidity file
403404
///
404405
/// A solidity file can contain several contracts, for every contract a separate `Artifact` is
@@ -426,9 +427,9 @@ pub struct CacheEntry {
426427
/// file `C` would be compiled twice, with `0.8.10` and `0.8.11`, producing two different
427428
/// artifacts.
428429
///
429-
/// This map tracks the artifacts by `name -> (Version -> profile -> PathBuf)`.
430+
/// This map tracks the artifacts by `name -> vec of artifacts`
430431
/// This mimics the default artifacts directory structure
431-
pub artifacts: CachedArtifacts,
432+
pub artifacts: BTreeMap<String, Vec<CachedArtifact>>,
432433
/// Whether this file was compiled at least once.
433434
///
434435
/// If this is true and `artifacts` are empty, it means that given version of the file does
@@ -460,12 +461,7 @@ impl CacheEntry {
460461
/// # }
461462
/// ```
462463
pub fn find_artifact_path(&self, contract_name: &str) -> Option<&Path> {
463-
self.artifacts
464-
.get(contract_name)?
465-
.iter()
466-
.next()
467-
.and_then(|(_, a)| a.iter().next())
468-
.map(|(_, p)| p.path.as_path())
464+
self.artifacts.get(contract_name)?.iter().next().map(|a| a.path.as_path())
469465
}
470466

471467
/// Reads the last modification date from the file's metadata
@@ -489,17 +485,15 @@ impl CacheEntry {
489485
let mut artifacts = BTreeMap::new();
490486
for (artifact_name, versioned_files) in self.artifacts.iter() {
491487
let mut files = Vec::with_capacity(versioned_files.len());
492-
for (version, cached_artifact) in versioned_files {
493-
for (profile, cached_artifact) in cached_artifact {
494-
let artifact: Artifact = utils::read_json_file(&cached_artifact.path)?;
495-
files.push(ArtifactFile {
496-
artifact,
497-
file: cached_artifact.path.clone(),
498-
version: version.clone(),
499-
build_id: cached_artifact.build_id.clone(),
500-
profile: profile.clone(),
501-
});
502-
}
488+
for cached_artifact in versioned_files {
489+
let artifact: Artifact = utils::read_json_file(&cached_artifact.path)?;
490+
files.push(ArtifactFile {
491+
artifact,
492+
file: cached_artifact.path.clone(),
493+
version: cached_artifact.version.clone(),
494+
build_id: cached_artifact.build_id.clone(),
495+
profile: cached_artifact.profile.clone(),
496+
});
503497
}
504498
artifacts.insert(artifact_name.clone(), files);
505499
}
@@ -513,35 +507,27 @@ impl CacheEntry {
513507
{
514508
for (name, artifacts) in artifacts.into_iter() {
515509
for artifact in artifacts {
516-
self.artifacts
517-
.entry(name.clone())
518-
.or_default()
519-
.entry(artifact.version.clone())
520-
.or_default()
521-
.insert(
522-
artifact.profile.clone(),
523-
CachedArtifact {
524-
build_id: artifact.build_id.clone(),
525-
path: artifact.file.clone(),
526-
},
527-
);
510+
self.artifacts.entry(name.clone()).or_default().push(CachedArtifact {
511+
build_id: artifact.build_id.clone(),
512+
path: artifact.file.clone(),
513+
version: artifact.version.clone(),
514+
profile: artifact.profile.clone(),
515+
});
528516
}
529517
}
530518
}
531519

532520
/// Returns `true` if the artifacts set contains the given version
533521
pub fn contains(&self, version: &Version, profile: &str) -> bool {
534-
self.artifacts.values().any(|artifacts| {
535-
artifacts.get(version).and_then(|artifacts| artifacts.get(profile)).is_some()
536-
})
537-
}
538-
539-
/// Iterator that yields all artifact files and their version
540-
pub fn artifacts_versions(&self) -> impl Iterator<Item = (&Version, &str, &CachedArtifact)> {
541522
self.artifacts
542523
.values()
543524
.flatten()
544-
.flat_map(|(v, a)| a.iter().map(move |(p, a)| (v, p.as_str(), a)))
525+
.any(|artifact| artifact.version == *version && artifact.profile == *profile)
526+
}
527+
528+
/// Iterator that yields all artifact files
529+
pub fn artifacts_versions(&self) -> impl Iterator<Item = (&Version, &str, &CachedArtifact)> {
530+
self.artifacts.values().flatten().map(|a| (&a.version, a.profile.as_str(), a))
545531
}
546532

547533
/// Returns the artifact file for the contract and version pair
@@ -552,9 +538,9 @@ impl CacheEntry {
552538
profile: &str,
553539
) -> Option<&CachedArtifact> {
554540
self.artifacts
555-
.get(contract)
556-
.and_then(|files| files.get(version))
557-
.and_then(|files| files.get(profile))
541+
.get(contract)?
542+
.iter()
543+
.find(|a| a.version == *version && a.profile == *profile)
558544
}
559545

560546
/// Iterator that yields all artifact files and their version
@@ -567,12 +553,12 @@ impl CacheEntry {
567553

568554
/// Iterator that yields all artifact files
569555
pub fn artifacts(&self) -> impl Iterator<Item = &CachedArtifact> {
570-
self.artifacts.values().flat_map(BTreeMap::values).flat_map(BTreeMap::values)
556+
self.artifacts.values().flatten()
571557
}
572558

573559
/// Mutable iterator over all artifact files
574560
pub fn artifacts_mut(&mut self) -> impl Iterator<Item = &mut CachedArtifact> {
575-
self.artifacts.values_mut().flat_map(BTreeMap::values_mut).flat_map(BTreeMap::values_mut)
561+
self.artifacts.values_mut().flatten()
576562
}
577563

578564
/// Checks if all artifact files exist
@@ -802,10 +788,7 @@ impl<T: ArtifactOutput, C: Compiler> ArtifactsCacheInner<'_, T, C> {
802788
return true;
803789
}
804790
entry.artifacts.retain(|_, artifacts| {
805-
artifacts.retain(|_, artifacts| {
806-
artifacts.retain(|profile, _| !dirty_profiles.contains(profile));
807-
!artifacts.is_empty()
808-
});
791+
artifacts.retain(|artifact| !dirty_profiles.contains(&artifact.profile));
809792
!artifacts.is_empty()
810793
});
811794
!entry.artifacts.is_empty()

0 commit comments

Comments
 (0)