Skip to content

Commit

Permalink
chore: replace PieceCacheState::free_stored_piece_if with free_unneed…
Browse files Browse the repository at this point in the history
…ed_stored_pieces
  • Loading branch information
tediou5 committed Aug 23, 2024
1 parent 4f744c9 commit de70bc1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/subspace-farmer/src/farmer_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ where
// Filter-out piece indices that are stored, but should not be as well as clean
// `inserted_piece_indices` from already stored piece indices, leaving just those that are
// still missing in cache
caches.free_stored_piece_if(|key, _offset| piece_indices_to_store.remove(key).is_none());
caches.free_unneeded_stored_pieces(&mut piece_indices_to_store);

if let Some(metrics) = &self.metrics {
for offset in caches.stored_pieces_offests() {
Expand Down
15 changes: 9 additions & 6 deletions crates/subspace-farmer/src/farmer_cache/piece_cache_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::hash_map::Values;
use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::hash::Hash;
use subspace_core_primitives::PieceIndex;
use subspace_networking::libp2p::kad::RecordKey;
use tracing::{debug, trace};

Expand Down Expand Up @@ -85,7 +86,9 @@ where
self.stored_pieces.insert(key, cache_offset)
}

pub(super) fn stored_pieces_offests(&self) -> Values<'_, RecordKey, FarmerCacheOffset<CacheIndex>> {
pub(super) fn stored_pieces_offests(
&self,
) -> Values<'_, RecordKey, FarmerCacheOffset<CacheIndex>> {
self.stored_pieces.values()
}

Expand All @@ -96,12 +99,12 @@ where
self.stored_pieces.remove(key)
}

pub(super) fn free_stored_piece_if<F>(&mut self, pred: F)
where
F: FnMut(&RecordKey, &mut FarmerCacheOffset<CacheIndex>) -> bool,
{
pub(super) fn free_unneeded_stored_pieces(
&mut self,
piece_indices_to_store: &mut HashMap<RecordKey, PieceIndex>,
) {
self.stored_pieces
.extract_if(pred)
.extract_if(|key, _offset| piece_indices_to_store.remove(key).is_none())
.for_each(|(_piece_index, offset)| {
// There is no need to adjust the `last_stored_offset` of the `backend` here,
// as the free_offset will be preferentially taken from the dangling free offsets
Expand Down

0 comments on commit de70bc1

Please sign in to comment.