Skip to content

Commit

Permalink
remaining_bytes aligns len since all writes will align first (#34171)
Browse files Browse the repository at this point in the history
* remaining_bytes aligns len since all writes will align first

* use remaining_bytes() to check for whether the new account can fit into the av storage

* Add test coverage for av remaining bytes alignment

* use great equal to check space available

---------

Co-authored-by: HaoranYi <[email protected]>
  • Loading branch information
jeffwashington and HaoranYi committed Nov 21, 2023
1 parent 90b11a6 commit 481c357
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5677,7 +5677,7 @@ impl AccountsDb {
fn has_space_available(&self, slot: Slot, size: u64) -> bool {
let store = self.storage.get_slot_storage_entry(slot).unwrap();
if store.status() == AccountStorageStatus::Available
&& (store.accounts.capacity() - store.accounts.len() as u64) > size
&& store.accounts.remaining_bytes() >= size
{
return true;
}
Expand Down
29 changes: 28 additions & 1 deletion accounts-db/src/append_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ impl AppendVec {

/// how many more bytes can be stored in this append vec
pub fn remaining_bytes(&self) -> u64 {
(self.capacity()).saturating_sub(self.len() as u64)
self.capacity()
.saturating_sub(u64_align!(self.len()) as u64)
}

pub fn len(&self) -> usize {
Expand Down Expand Up @@ -1002,10 +1003,36 @@ pub mod tests {
let av = AppendVec::new(&path.path, true, sz);
assert_eq!(av.capacity(), sz64);
assert_eq!(av.remaining_bytes(), sz64);

// append first account, an u64 aligned account (136 bytes)
let mut av_len = 0;
let account = create_test_account(0);
av.append_account_test(&account).unwrap();
av_len += STORE_META_OVERHEAD;
assert_eq!(av.capacity(), sz64);
assert_eq!(av.remaining_bytes(), sz64 - (STORE_META_OVERHEAD as u64));
assert_eq!(av.len(), av_len);

// append second account, a *not* u64 aligned account (137 bytes)
let account = create_test_account(1);
let account_storage_len = STORE_META_OVERHEAD + 1;
av_len += account_storage_len;
av.append_account_test(&account).unwrap();
assert_eq!(av.capacity(), sz64);
assert_eq!(av.len(), av_len);
let alignment_bytes = u64_align!(av_len) - av_len; // bytes used for alignment (7 bytes)
assert_eq!(alignment_bytes, 7);
assert_eq!(av.remaining_bytes(), sz64 - u64_align!(av_len) as u64);

// append third account, a *not* u64 aligned account (137 bytes)
let account = create_test_account(1);
av.append_account_test(&account).unwrap();
let account_storage_len = STORE_META_OVERHEAD + 1;
av_len += alignment_bytes; // bytes used for alignment at the end of previous account
av_len += account_storage_len;
assert_eq!(av.capacity(), sz64);
assert_eq!(av.len(), av_len);
assert_eq!(av.remaining_bytes(), sz64 - u64_align!(av_len) as u64);
}

#[test]
Expand Down

0 comments on commit 481c357

Please sign in to comment.