Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remaining_bytes aligns len since all writes will align first #34171

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion accounts-db/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5752,7 +5752,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