Skip to content

Commit cb9451c

Browse files
authored
Optimize max_flushed_root update in flushing roots (solana-labs#4320)
* optimize acchounts_cache max_root tracking in flush_roots * better comments * remove max_root check --------- Co-authored-by: HaoranYi <[email protected]>
1 parent 2d40631 commit cb9451c

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

accounts-db/src/accounts_cache.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,7 @@ impl AccountsCache {
234234
}
235235

236236
pub fn add_root(&self, root: Slot) {
237-
let max_flushed_root = self.fetch_max_flush_root();
238-
if root > max_flushed_root || (root == max_flushed_root && root == 0) {
239-
self.maybe_unflushed_roots.write().unwrap().insert(root);
240-
}
237+
self.maybe_unflushed_roots.write().unwrap().insert(root);
241238
}
242239

243240
pub fn clear_roots(&self, max_root: Option<Slot>) -> BTreeSet<Slot> {

accounts-db/src/accounts_db.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6296,30 +6296,33 @@ impl AccountsDb {
62966296
});
62976297

62986298
// Always flush up to `requested_flush_root`, which is necessary for things like snapshotting.
6299-
let cached_roots: BTreeSet<Slot> = self.accounts_cache.clear_roots(requested_flush_root);
6299+
let flushed_roots: BTreeSet<Slot> = self.accounts_cache.clear_roots(requested_flush_root);
63006300

63016301
// Iterate from highest to lowest so that we don't need to flush earlier
63026302
// outdated updates in earlier roots
63036303
let mut num_roots_flushed = 0;
63046304
let mut flush_stats = FlushStats::default();
6305-
for &root in cached_roots.iter().rev() {
6305+
for &root in flushed_roots.iter().rev() {
63066306
if let Some(stats) =
63076307
self.flush_slot_cache_with_clean(root, should_flush_f.as_mut(), max_clean_root)
63086308
{
63096309
num_roots_flushed += 1;
63106310
flush_stats.accumulate(&stats);
63116311
}
6312+
}
63126313

6313-
// Regardless of whether this slot was *just* flushed from the cache by the above
6314-
// `flush_slot_cache()`, we should update the `max_flush_root`.
6315-
// This is because some rooted slots may be flushed to storage *before* they are marked as root.
6316-
// This can occur for instance when
6317-
// the cache is overwhelmed, we flushed some yet to be rooted frozen slots
6318-
// These slots may then *later* be marked as root, so we still need to handle updating the
6319-
// `max_flush_root` in the accounts cache.
6314+
// Note that self.flush_slot_cache_with_clean() can return None if the
6315+
// slot is already been flushed. This can happen if the cache is
6316+
// overwhelmed and we flushed some yet to be rooted frozen slots.
6317+
// However, Independent of whether the last slot was actually flushed
6318+
// from the cache by the above loop, we should always update the
6319+
// `max_flush_root` to the max of the flushed roots, because that's
6320+
// max_flushed_root tracks the logical last root that was flushed to
6321+
// storage by snapshotting.
6322+
if let Some(&root) = flushed_roots.last() {
63206323
self.accounts_cache.set_max_flush_root(root);
63216324
}
6322-
let num_new_roots = cached_roots.len();
6325+
let num_new_roots = flushed_roots.len();
63236326
(num_new_roots, num_roots_flushed, flush_stats)
63246327
}
63256328

0 commit comments

Comments
 (0)