reduce lock contention on latest_root (#16306)

This commit is contained in:
Jeff Washington (jwash)
2021-04-05 11:35:14 -05:00
committed by GitHub
parent 7a997759fa
commit 95dc7b5449

View File

@ -752,30 +752,35 @@ impl<T: 'static + Clone + IsCached + ZeroLamport> AccountsIndex<T> {
) -> Option<usize> { ) -> Option<usize> {
let mut current_max = 0; let mut current_max = 0;
let mut rv = None; let mut rv = None;
for (i, (slot, _t)) in slice.iter().rev().enumerate() { if let Some(ancestors) = ancestors {
if *slot >= current_max && self.is_ancestor_or_root(*slot, ancestors, max_root) { if !ancestors.is_empty() {
rv = Some((slice.len() - 1) - i); for (i, (slot, _t)) in slice.iter().rev().enumerate() {
current_max = *slot; if *slot >= current_max && ancestors.contains_key(slot) {
rv = Some(i);
current_max = *slot;
}
}
} }
} }
rv let max_root = max_root.unwrap_or(Slot::MAX);
} let mut tracker = None;
// Checks that the given slot is either: for (i, (slot, _t)) in slice.iter().rev().enumerate() {
// 1) in the `ancestors` set if *slot >= current_max && *slot <= max_root {
// 2) or is a root let lock = match tracker {
fn is_ancestor_or_root( Some(inner) => inner,
&self, None => self.roots_tracker.read().unwrap(),
slot: Slot, };
ancestors: Option<&Ancestors>, if lock.roots.contains(&slot) {
max_root: Option<Slot>, rv = Some(i);
) -> bool { current_max = *slot;
ancestors.map_or(false, |ancestors| ancestors.contains_key(&slot)) || }
// If the slot is a root, it must be less than the maximum root specified. This tracker = Some(lock);
// allows scans on non-rooted slots to specify and read data from }
// ancestors > max_root, while not seeing rooted data update during the scan }
(max_root.map_or(true, |max_root| slot <= max_root) && (self.is_root(slot)))
rv.map(|index| slice.len() - 1 - index)
} }
/// Get an account /// Get an account