Skip to content

Commit e89f283

Browse files
authored
Refactors updating accounts read cache's data size stat (#10786)
1 parent 1c64a4b commit e89f283

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

accounts-db/src/read_only_accounts_cache.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,23 @@ impl ReadOnlyAccountsCache {
191191
) {
192192
let measure_store = Measure::start("");
193193
self.highest_slot_stored.fetch_max(slot, Ordering::Release);
194-
let account_size = Self::account_size(&account);
195-
self.data_size.fetch_add(account_size, Ordering::Relaxed);
194+
let new_account_size = Self::account_size(&account);
195+
let old_account_size;
196196
match self.cache.entry(pubkey) {
197197
Entry::Vacant(entry) => {
198+
old_account_size = 0;
198199
entry.insert(ReadOnlyAccountCacheEntry::new(account, slot, timestamp));
199200
self.cache_len.fetch_add(1, Ordering::Relaxed);
200201
}
201202
Entry::Occupied(mut entry) => {
202203
let entry = entry.get_mut();
203-
let account_size = Self::account_size(&entry.account);
204-
self.data_size.fetch_sub(account_size, Ordering::Relaxed);
204+
old_account_size = Self::account_size(&entry.account);
205205
entry.account = account;
206206
entry.slot = slot;
207207
entry.last_update_time.store(timestamp, Ordering::Relaxed);
208208
}
209209
};
210+
update_stat(&self.data_size, old_account_size, new_account_size);
210211
let store_us = measure_store.end_as_us();
211212
self.stats.store_us.fetch_add(store_us, Ordering::Relaxed);
212213
}
@@ -462,6 +463,14 @@ impl ReadOnlyAccountCacheEntry {
462463
}
463464
}
464465

466+
/// Updates atomic `stat` with the delta of `old` and `new`
467+
#[inline]
468+
fn update_stat(stat: &AtomicUsize, old: usize, new: usize) {
469+
if new != old {
470+
stat.fetch_add(new.wrapping_sub(old), Ordering::Relaxed);
471+
}
472+
}
473+
465474
#[cfg(test)]
466475
mod tests {
467476
use {
@@ -475,7 +484,7 @@ mod tests {
475484
sync::Arc,
476485
time::{Duration, Instant},
477486
},
478-
test_case::test_matrix,
487+
test_case::{test_case, test_matrix},
479488
};
480489

481490
impl ReadOnlyAccountsCache {
@@ -642,4 +651,14 @@ mod tests {
642651
assert_eq!(cache.cache_len(), 0);
643652
assert!(cache.remove(&Pubkey::new_unique()).is_none());
644653
}
654+
655+
#[test_case(11, 11; "equal")]
656+
#[test_case(22, 27; "greater")]
657+
#[test_case(33, 30; "less")]
658+
fn test_update_stat(old: usize, new: usize) {
659+
let val = old + new;
660+
let stat = val.into();
661+
update_stat(&stat, old, new);
662+
assert_eq!(stat.into_inner(), val - old + new);
663+
}
645664
}

0 commit comments

Comments
 (0)