Skip to content

Commit 7745aa0

Browse files
committed
Refactor some build key logic from SortedBuildIterator
In order to reuse this logic for the resolvo solver, extract it to an external function. It is not practical for the resolvo solver to directly use a SortedBuildIterator. Signed-off-by: J Robert Ray <[email protected]>
1 parent 2256d7e commit 7745aa0

File tree

1 file changed

+77
-53
lines changed

1 file changed

+77
-53
lines changed

crates/spk-solve/crates/package-iterator/src/package_iterator.rs

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -468,64 +468,17 @@ struct ChangeCounter {
468468
pub use_it: bool,
469469
}
470470

471-
impl SortedBuildIterator {
472-
pub async fn new(
473-
_options: OptionMap,
474-
source: Arc<tokio::sync::Mutex<dyn BuildIterator + Send>>,
475-
builds_with_impossible_requests: HashMap<BuildIdent, Compatibility>,
476-
) -> Result<Self> {
477-
// Note: _options is unused in this implementation, it was used
478-
// in the by_distance sorting implementation
479-
let mut builds = VecDeque::<BuildWithRepos>::new();
480-
{
481-
let mut source_lock = source.lock().await;
482-
while let Some(item) = source_lock.next().await? {
483-
builds.push_back(item);
484-
}
485-
}
486-
487-
let mut sbi = SortedBuildIterator { builds };
488-
489-
sbi.sort_by_build_option_values(builds_with_impossible_requests)
490-
.await;
491-
Ok(sbi)
492-
}
493-
494-
/// Helper for making BuildKey structures used in the sorting in
495-
/// sort_by_build_option_values() below
496-
fn make_option_values_build_key(
497-
spec: &Spec,
498-
ordered_names: &Vec<OptNameBuf>,
499-
build_name_values: &HashMap<BuildIdent, OptionMap>,
500-
makes_an_impossible_request: bool,
501-
) -> BuildKey {
502-
let build_id = spec.ident();
503-
let empty = OptionMap::default();
504-
let name_values = match build_name_values.get(build_id) {
505-
Some(nv) => nv,
506-
None => &empty,
507-
};
508-
BuildKey::new(
509-
spec.ident(),
510-
ordered_names,
511-
name_values,
512-
makes_an_impossible_request,
513-
)
514-
}
515-
516-
/// Sorts builds by keys based on ordered build option names and
517-
/// differing values in those options
518-
async fn sort_by_build_option_values(
519-
&mut self,
520-
builds_with_impossible_requests: HashMap<BuildIdent, Compatibility>,
521-
) {
522-
let start = Instant::now();
471+
pub struct BuildToSortedOptName {}
523472

473+
impl BuildToSortedOptName {
474+
pub fn sort_builds<'a>(
475+
builds: impl Iterator<Item = &'a (Arc<Spec>, PackageSource)>,
476+
) -> (Vec<OptNameBuf>, HashMap<BuildIdent, OptionMap>) {
524477
let mut number_non_src_builds: u64 = 0;
525478
let mut build_name_values: HashMap<BuildIdent, OptionMap> = HashMap::default();
526479
let mut changes: HashMap<OptNameBuf, ChangeCounter> = HashMap::new();
527480

528-
for (build, _) in self.builds.iter().flat_map(|hm| hm.values()) {
481+
for (build, _) in builds {
529482
// Skip this if it's a '/src' build because '/src' builds
530483
// won't use the build option values in their key, they
531484
// don't need to be looked at. They have a type of key
@@ -611,6 +564,77 @@ impl SortedBuildIterator {
611564
// position for a site's spk setup.
612565
BUILD_KEY_NAME_ORDER.promote_names(key_entry_names.as_mut_slice(), |n| n);
613566

567+
(key_entry_names, build_name_values)
568+
}
569+
}
570+
571+
impl SortedBuildIterator {
572+
pub async fn new(
573+
_options: OptionMap,
574+
source: Arc<tokio::sync::Mutex<dyn BuildIterator + Send>>,
575+
builds_with_impossible_requests: HashMap<BuildIdent, Compatibility>,
576+
) -> Result<Self> {
577+
// Note: _options is unused in this implementation, it was used
578+
// in the by_distance sorting implementation
579+
let mut builds = VecDeque::<BuildWithRepos>::new();
580+
{
581+
let mut source_lock = source.lock().await;
582+
while let Some(item) = source_lock.next().await? {
583+
builds.push_back(item);
584+
}
585+
}
586+
587+
let mut sbi = SortedBuildIterator { builds };
588+
589+
sbi.sort_by_build_option_values(builds_with_impossible_requests)
590+
.await;
591+
Ok(sbi)
592+
}
593+
594+
pub async fn new_from_builds(
595+
builds: VecDeque<BuildWithRepos>,
596+
builds_with_impossible_requests: HashMap<BuildIdent, Compatibility>,
597+
) -> Result<Self> {
598+
let mut sbi = SortedBuildIterator { builds };
599+
600+
sbi.sort_by_build_option_values(builds_with_impossible_requests)
601+
.await;
602+
Ok(sbi)
603+
}
604+
605+
/// Helper for making BuildKey structures used in the sorting in
606+
/// sort_by_build_option_values() below
607+
fn make_option_values_build_key(
608+
spec: &Spec,
609+
ordered_names: &Vec<OptNameBuf>,
610+
build_name_values: &HashMap<BuildIdent, OptionMap>,
611+
makes_an_impossible_request: bool,
612+
) -> BuildKey {
613+
let build_id = spec.ident();
614+
let empty = OptionMap::default();
615+
let name_values = match build_name_values.get(build_id) {
616+
Some(nv) => nv,
617+
None => &empty,
618+
};
619+
BuildKey::new(
620+
spec.ident(),
621+
ordered_names,
622+
name_values,
623+
makes_an_impossible_request,
624+
)
625+
}
626+
627+
/// Sorts builds by keys based on ordered build option names and
628+
/// differing values in those options
629+
async fn sort_by_build_option_values(
630+
&mut self,
631+
builds_with_impossible_requests: HashMap<BuildIdent, Compatibility>,
632+
) {
633+
let start = Instant::now();
634+
635+
let (key_entry_names, build_name_values) =
636+
BuildToSortedOptName::sort_builds(self.builds.iter().flat_map(|hm| hm.values()));
637+
614638
// Sort the builds by their generated keys generated from the
615639
// ordered names and values worth including.
616640
self.builds.make_contiguous().sort_by_cached_key(|hm| {

0 commit comments

Comments
 (0)