From 0b6a172a8217e1e630fdabf4c7fb7dd997d4ea3f Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 12 Dec 2023 22:42:33 +0100 Subject: [PATCH] v1.17: Fix - Uses `fetch_add()` to accumulate usage counters in `LoadedPrograms` (backport of #34319) (#34348) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix - Uses `fetch_add()` to accumulate usage counters in `LoadedPrograms` (#34319) Uses fetch_add() to accumulate usage counters. (cherry picked from commit f70d5481bc616c3d747a28b055f61a3035a137da) Co-authored-by: Alexander Meißner --- program-runtime/src/loaded_programs.rs | 40 ++++++++++++-------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/program-runtime/src/loaded_programs.rs b/program-runtime/src/loaded_programs.rs index 77e4c2b228923b..b47729935075ac 100644 --- a/program-runtime/src/loaded_programs.rs +++ b/program-runtime/src/loaded_programs.rs @@ -600,13 +600,11 @@ impl LoadedPrograms { if matches!(existing.program, LoadedProgramType::Unloaded(_)) { // The unloaded program is getting reloaded // Copy over the usage counter to the new entry - let mut usage_count = existing.tx_usage_counter.load(Ordering::Relaxed); - saturating_add_assign!( - usage_count, - entry.tx_usage_counter.load(Ordering::Relaxed) + entry.tx_usage_counter.fetch_add( + existing.tx_usage_counter.load(Ordering::Relaxed), + Ordering::Relaxed, ); - entry.tx_usage_counter.store(usage_count, Ordering::Relaxed); - entry.ix_usage_counter.store( + entry.ix_usage_counter.fetch_add( existing.ix_usage_counter.load(Ordering::Relaxed), Ordering::Relaxed, ); @@ -786,7 +784,7 @@ impl LoadedPrograms { let mut unloaded = Vec::new(); let current_slot = working_slot.current_slot(); let found = keys - .filter_map(|(key, (match_criteria, count))| { + .filter_map(|(key, (match_criteria, usage_count))| { if let Some(second_level) = self.entries.get(&key) { for entry in second_level.iter().rev() { let is_ancestor = if let Some(fork_graph) = &self.fork_graph { @@ -810,41 +808,41 @@ impl LoadedPrograms { { if current_slot >= entry.effective_slot { if !Self::is_entry_usable(entry, current_slot, &match_criteria) { - missing.push((key, count)); + missing.push((key, usage_count)); return None; } if !Self::matches_environment(entry, environments) { - missing.push((key, count)); + missing.push((key, usage_count)); return None; } if let LoadedProgramType::Unloaded(_environment) = &entry.program { - unloaded.push((key, count)); + unloaded.push((key, usage_count)); return None; } - let mut usage_count = - entry.tx_usage_counter.load(Ordering::Relaxed); - saturating_add_assign!(usage_count, count); - entry.tx_usage_counter.store(usage_count, Ordering::Relaxed); + entry + .tx_usage_counter + .fetch_add(usage_count, Ordering::Relaxed); return Some((key, entry.clone())); } else if entry.is_implicit_delay_visibility_tombstone(current_slot) { // Found a program entry on the current fork, but it's not effective // yet. It indicates that the program has delayed visibility. Return // the tombstone to reflect that. - return Some(( - key, - Arc::new(LoadedProgram::new_tombstone( - entry.deployment_slot, - LoadedProgramType::DelayVisibility, - )), + let entry_to_return = Arc::new(LoadedProgram::new_tombstone( + entry.deployment_slot, + LoadedProgramType::DelayVisibility, )); + entry_to_return + .tx_usage_counter + .fetch_add(usage_count, Ordering::Relaxed); + return Some((key, entry_to_return)); } } } } - missing.push((key, count)); + missing.push((key, usage_count)); None }) .collect::>>();