From 3fe7655378d610a103c471f014b61d7a57dfa355 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Fri, 20 May 2022 08:50:24 -0500 Subject: [PATCH] use iterator instead of vec to iterate accounts (#25370) * user iterator instead of vec to iterate accounts * rename --- runtime/src/accounts_db.rs | 7 ++++--- runtime/src/append_vec.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index fe88fd2e8f..161b512033 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -38,7 +38,9 @@ use { active_stats::{ActiveStatItem, ActiveStats}, ancestors::Ancestors, ancient_append_vecs::is_ancient, - append_vec::{AppendVec, StoredAccountMeta, StoredMeta, StoredMetaWriteVersion}, + append_vec::{ + AppendVec, AppendVecAccountsIter, StoredAccountMeta, StoredMeta, StoredMetaWriteVersion, + }, bank::Rewrites, cache_hash_data::CacheHashData, contains::Contains, @@ -5413,8 +5415,7 @@ impl AccountsDb { let mut progress = Vec::with_capacity(len); let mut current = Vec::with_capacity(len); for storage in storages { - let accounts = storage.accounts.accounts(0); - let mut iterator: std::vec::IntoIter> = accounts.into_iter(); + let mut iterator = AppendVecAccountsIter::new(&storage.accounts); if let Some(item) = iterator .next() .map(|stored_account| (stored_account.meta.write_version, Some(stored_account))) diff --git a/runtime/src/append_vec.rs b/runtime/src/append_vec.rs index e86b4084d8..630e040e6e 100644 --- a/runtime/src/append_vec.rs +++ b/runtime/src/append_vec.rs @@ -136,6 +136,33 @@ impl<'a> StoredAccountMeta<'a> { } } +pub struct AppendVecAccountsIter<'a> { + append_vec: &'a AppendVec, + offset: usize, +} + +impl<'a> AppendVecAccountsIter<'a> { + pub fn new(append_vec: &'a AppendVec) -> Self { + Self { + append_vec, + offset: 0, + } + } +} + +impl<'a> Iterator for AppendVecAccountsIter<'a> { + type Item = StoredAccountMeta<'a>; + + fn next(&mut self) -> Option { + if let Some((account, next_offset)) = self.append_vec.get_account(self.offset) { + self.offset = next_offset; + Some(account) + } else { + None + } + } +} + /// A thread-safe, file-backed block of memory used to store `Account` instances. Append operations /// are serialized such that only one thread updates the internal `append_lock` at a time. No /// restrictions are placed on reading. That is, one may read items from one thread while another