Skip to content

Commit 7a42947

Browse files
authored
Merge pull request #2253 from subspace/improve-replotting-order
Replot sectors in order of expiration
2 parents c730843 + 456dc12 commit 7a42947

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

crates/subspace-farmer/src/single_disk_farm/plotting.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,11 @@ where
598598
}
599599
}
600600

601+
struct SectorToReplot {
602+
sector_index: SectorIndex,
603+
expires_at: SegmentIndex,
604+
}
605+
601606
#[allow(clippy::too_many_arguments)]
602607
async fn send_plotting_notifications<NC>(
603608
public_key_hash: Blake3Hash,
@@ -641,9 +646,10 @@ where
641646
let _ = initial_plotting_finished.send(());
642647
}
643648

644-
let mut sectors_expire_at = HashMap::with_capacity(usize::from(target_sector_count));
649+
let mut sectors_expire_at =
650+
HashMap::<SectorIndex, SegmentIndex>::with_capacity(usize::from(target_sector_count));
645651

646-
let mut sector_indices_to_replot = Vec::new();
652+
let mut sectors_to_replot = Vec::new();
647653
let mut sectors_to_check = Vec::with_capacity(usize::from(target_sector_count));
648654
let mut archived_segment_commitments_cache = LruCache::new(ARCHIVED_SEGMENTS_CACHE_SIZE);
649655

@@ -664,26 +670,27 @@ where
664670
.map(|sector_metadata| (sector_metadata.sector_index, sector_metadata.history_size))
665671
.collect_into(&mut sectors_to_check);
666672
for (sector_index, history_size) in sectors_to_check.drain(..) {
667-
if let Some(sector_expire_at) = sectors_expire_at.get(&sector_index) {
673+
if let Some(expires_at) = sectors_expire_at.get(&sector_index).copied() {
668674
trace!(
669675
%sector_index,
670676
%history_size,
671-
%sector_expire_at,
677+
%expires_at,
672678
"Checking sector for expiration"
673679
);
674680
// +1 means we will start replotting a bit before it actually expires to avoid
675681
// storing expired sectors
676-
if *sector_expire_at
677-
<= (archived_segment_header.segment_index() + SegmentIndex::ONE)
678-
{
682+
if expires_at <= (archived_segment_header.segment_index() + SegmentIndex::ONE) {
679683
debug!(
680684
%sector_index,
681685
%history_size,
682-
%sector_expire_at,
686+
%expires_at,
683687
"Sector expires soon #1, scheduling replotting"
684688
);
685689
// Time to replot
686-
sector_indices_to_replot.push(sector_index);
690+
sectors_to_replot.push(SectorToReplot {
691+
sector_index,
692+
expires_at,
693+
});
687694
}
688695
continue;
689696
}
@@ -746,14 +753,18 @@ where
746753
if expiration_history_size.segment_index()
747754
<= (archived_segment_header.segment_index() + SegmentIndex::ONE)
748755
{
756+
let expires_at = expiration_history_size.segment_index();
749757
debug!(
750758
%sector_index,
751759
%history_size,
752-
sector_expire_at = %expiration_history_size.segment_index(),
760+
%expires_at,
753761
"Sector expires soon #2, scheduling replotting"
754762
);
755763
// Time to replot
756-
sector_indices_to_replot.push(sector_index);
764+
sectors_to_replot.push(SectorToReplot {
765+
sector_index,
766+
expires_at,
767+
});
757768
} else {
758769
trace!(
759770
%sector_index,
@@ -769,10 +780,12 @@ where
769780
}
770781
}
771782

772-
let sectors_queued = sector_indices_to_replot.len();
773-
let mut sector_indices_to_replot =
774-
sector_indices_to_replot.drain(..).enumerate().peekable();
775-
while let Some((index, sector_index)) = sector_indices_to_replot.next() {
783+
let sectors_queued = sectors_to_replot.len();
784+
sectors_to_replot.sort_by_key(|sector_to_replot| sector_to_replot.expires_at);
785+
let mut sector_indices_to_replot = sectors_to_replot.drain(..).enumerate().peekable();
786+
while let Some((index, SectorToReplot { sector_index, .. })) =
787+
sector_indices_to_replot.next()
788+
{
776789
let (acknowledgement_sender, acknowledgement_receiver) = oneshot::channel();
777790
if let Err(error) = sectors_to_plot_sender
778791
.send(SectorToPlot {
@@ -782,7 +795,7 @@ where
782795
acknowledgement_sender,
783796
next_segment_index_hint: sector_indices_to_replot
784797
.peek()
785-
.map(|(_index, sector_index)| *sector_index),
798+
.map(|(_index, SectorToReplot { sector_index, .. })| *sector_index),
786799
})
787800
.await
788801
{

0 commit comments

Comments
 (0)