From 9935c2b5e7eac7d78f1fa64d3dd4aef466320d06 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang <93241502+yhchiang-sol@users.noreply.github.com> Date: Sat, 3 Feb 2024 23:02:09 -0800 Subject: [PATCH] [AppendVec] Use proper Vec initial size in append_accounts() (#35047) #### Problem append_accounts() only appends (len - skip) accounts. However, AppendVec::append_accounts() reserves `len` instead of `(len - skip)` for its vectors. #### Summary of Changes Use (len - skip) as the initial size of the Vectors. --- accounts-db/src/append_vec.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/accounts-db/src/append_vec.rs b/accounts-db/src/append_vec.rs index 353cb58e60..578371c45a 100644 --- a/accounts-db/src/append_vec.rs +++ b/accounts-db/src/append_vec.rs @@ -586,7 +586,11 @@ impl AppendVec { let mut offset = self.len(); let len = accounts.accounts.len(); - let mut offsets = Vec::with_capacity(len); + // Here we have `len - skip` number of accounts. The +1 extra capacity + // is for storing the aligned offset of the last entry to that is used + // to compute the StoredAccountInfo of the last entry. + let offsets_len = len - skip + 1; + let mut offsets = Vec::with_capacity(offsets_len); for i in skip..len { let (account, pubkey, hash, write_version_obsolete) = accounts.get(i); let account_meta = account @@ -629,10 +633,11 @@ impl AppendVec { if offsets.is_empty() { None } else { + let mut rv = Vec::with_capacity(offsets.len()); + // 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. offsets.push(u64_align!(offset)); - let mut rv = Vec::with_capacity(len); for offsets in offsets.windows(2) { rv.push(StoredAccountInfo { offset: offsets[0],