Refactor - `LoadedPrograms::get_entries_sorted_by_tx_usage()` (#32874)

Factor get_entries_sorted_by_tx_usage() out of sort_and_unload().
This commit is contained in:
Alexander Meißner 2023-08-17 19:14:32 +02:00 committed by GitHub
parent c5a251e8c3
commit be6d6fa5ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 13 deletions

View File

@ -673,29 +673,39 @@ impl LoadedPrograms {
}) })
} }
/// Unloads programs which were used infrequently /// Returns the list of loaded programs which are verified and compiled sorted by `tx_usage_counter`.
pub fn sort_and_unload(&mut self, shrink_to: PercentageInteger) { ///
let sorted_candidates: Vec<(Pubkey, Arc<LoadedProgram>)> = self /// Entries from program runtime v1 and v2 can be individually filtered.
.entries pub fn get_entries_sorted_by_tx_usage(
&self,
include_program_runtime_v1: bool,
include_program_runtime_v2: bool,
) -> Vec<(Pubkey, Arc<LoadedProgram>)> {
self.entries
.iter() .iter()
.flat_map(|(id, list)| { .flat_map(|(id, list)| {
list.iter() list.iter()
.filter_map(move |program| match program.program { .filter_map(move |program| match program.program {
LoadedProgramType::LegacyV0(_) LoadedProgramType::LegacyV0(_) | LoadedProgramType::LegacyV1(_)
| LoadedProgramType::LegacyV1(_) if include_program_runtime_v1 =>
| LoadedProgramType::Typed(_) => Some((*id, program.clone())), {
Some((*id, program.clone()))
}
LoadedProgramType::Typed(_) if include_program_runtime_v2 => {
Some((*id, program.clone()))
}
#[cfg(test)] #[cfg(test)]
LoadedProgramType::TestLoaded(_) => Some((*id, program.clone())), LoadedProgramType::TestLoaded(_) => Some((*id, program.clone())),
LoadedProgramType::Unloaded(_) _ => None,
| LoadedProgramType::FailedVerification(_)
| LoadedProgramType::Closed
| LoadedProgramType::DelayVisibility
| LoadedProgramType::Builtin(_) => None,
}) })
}) })
.sorted_by_cached_key(|(_id, program)| program.tx_usage_counter.load(Ordering::Relaxed)) .sorted_by_cached_key(|(_id, program)| program.tx_usage_counter.load(Ordering::Relaxed))
.collect(); .collect()
}
/// Unloads programs which were used infrequently
pub fn sort_and_unload(&mut self, shrink_to: PercentageInteger) {
let sorted_candidates = self.get_entries_sorted_by_tx_usage(true, true);
let num_to_unload = sorted_candidates let num_to_unload = sorted_candidates
.len() .len()
.saturating_sub(shrink_to.apply_to(MAX_LOADED_ENTRY_COUNT)); .saturating_sub(shrink_to.apply_to(MAX_LOADED_ENTRY_COUNT));