[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.
This commit is contained in:
Yueh-Hsuan Chiang 2024-02-03 23:02:09 -08:00 committed by GitHub
parent 919b306733
commit 9935c2b5e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 2 deletions

View File

@ -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],