From 4837dc071fd6afcaded4a75c2cc1ad9aab1d3ec6 Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Wed, 16 Nov 2022 10:59:11 -0800 Subject: [PATCH] cleanup return value from append_accounts (#28836) --- runtime/src/accounts_db.rs | 4 ++-- runtime/src/append_vec.rs | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 60269e625d..13b1770fed 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -6202,10 +6202,9 @@ impl AccountsDb { &accounts_and_meta_to_store[infos.len()..], &hashes[infos.len()..], ); - assert!(!rvs.is_empty()); append_accounts.stop(); total_append_accounts_us += append_accounts.as_us(); - if rvs.len() == 1 { + if rvs.is_none() { storage.set_status(AccountStorageStatus::Full); // See if an account overflows the append vecs in the slot. @@ -6223,6 +6222,7 @@ impl AccountsDb { } for (offsets, (_, account)) in rvs + .unwrap() .windows(2) .zip(&accounts_and_meta_to_store[infos.len()..]) { diff --git a/runtime/src/append_vec.rs b/runtime/src/append_vec.rs index f22f755e1c..425fcbd842 100644 --- a/runtime/src/append_vec.rs +++ b/runtime/src/append_vec.rs @@ -510,14 +510,17 @@ impl AppendVec { } /// Copy each account metadata, account and hash to the internal buffer. - /// Return the starting offset of each account metadata. + /// If there is no room to write the first entry, None is returned. + /// Otherwise, returns the starting offset of each account metadata. + /// Plus, the final return value is the offset where the next entry would be appended. + /// So, return.len() is 1 + (number of accounts written) /// After each account is appended, the internal `current_len` is updated /// and will be available to other threads. pub fn append_accounts( &self, accounts: &[(StoredMeta, Option<&impl ReadableAccount>)], hashes: &[impl Borrow], - ) -> Vec { + ) -> Option> { let _lock = self.append_lock.lock().unwrap(); let mut offset = self.len(); let mut rv = Vec::with_capacity(accounts.len()); @@ -544,11 +547,15 @@ impl AppendVec { } } - // The last entry in this offset needs to be the u64 aligned offset, because that's - // where the *next* entry will begin to be stored. - rv.push(u64_align!(offset)); + if rv.is_empty() { + None + } else { + // The last entry in this offset needs to be the u64 aligned offset, because that's + // where the *next* entry will begin to be stored. + rv.push(u64_align!(offset)); - rv + Some(rv) + } } /// Copy the account metadata, account and hash to the internal buffer. @@ -561,11 +568,7 @@ impl AppendVec { hash: Hash, ) -> Option { let res = self.append_accounts(&[(storage_meta, Some(account))], &[&hash]); - if res.len() == 1 { - None - } else { - res.first().cloned() - } + res.and_then(|res| res.first().cloned()) } }