Skip to content

Commit

Permalink
More efficient padding (#11656) (#11823)
Browse files Browse the repository at this point in the history
(cherry picked from commit f1ba238)

Co-authored-by: Jack May <[email protected]>
  • Loading branch information
mergify[bot] and jackcmay authored Aug 25, 2020
1 parent f162c6d commit 08bece7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
9 changes: 5 additions & 4 deletions programs/bpf_loader/src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ pub fn serialize_parameters_aligned(
v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64)
.unwrap();
v.write_all(&keyed_account.try_account_ref()?.data).unwrap();
for _ in 0..16 - (v.len() % 16) {
v.write_u8(0).unwrap(); // 128 bit aligned again
}
v.resize(
v.len() + (v.len() as *const u8).align_offset(align_of::<u128>()),
0,
);
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
.unwrap();
}
Expand Down Expand Up @@ -188,7 +189,7 @@ pub fn deserialize_parameters_aligned(
.data
.clone_from_slice(&buffer[start..end]);
start += keyed_account.data_len()?; // data
start += 16 - (start % 16); // padding
start += (start as *const u8).align_offset(align_of::<u128>());
start += mem::size_of::<u64>(); // rent_epoch
} else {
start += 7; // padding
Expand Down
Binary file modified programs/bpf_loader/test_elfs/noop_aligned.so
Binary file not shown.
5 changes: 2 additions & 3 deletions sdk/bpf/c/inc/solana_sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ static bool sol_deserialize(
uint64_t data_len = *(uint64_t *) input;
input += sizeof(uint64_t);
input += data_len;
input += 16 - (data_len % 16); // padding
input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
input += sizeof(uint64_t);
}
continue;
Expand Down Expand Up @@ -334,8 +334,7 @@ static bool sol_deserialize(
input += sizeof(uint64_t);
params->ka[i].data = (uint8_t *) input;
input += params->ka[i].data_len;

input += 16 - (params->ka[i].data_len % 16); // padding
input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding

// rent epoch
params->ka[i].rent_epoch = *(uint64_t *) input;
Expand Down
7 changes: 3 additions & 4 deletions sdk/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubk
use alloc::vec::Vec;
use std::{
cell::RefCell,
mem::size_of,
mem::{align_of, size_of},
rc::Rc,
// Hide Result from bindgen gets confused about generics in non-generic type declarations
result::Result as ResultGeneric,
Expand Down Expand Up @@ -80,7 +80,7 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
let executable = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>();

offset += 4; // padding
offset += size_of::<u32>(); // padding to u64

let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<Pubkey>();
Expand All @@ -100,8 +100,7 @@ pub unsafe fn deserialize<'a>(input: *mut u8) -> (&'a Pubkey, Vec<AccountInfo<'a
from_raw_parts_mut(input.add(offset), data_len)
}));
offset += data_len;

offset += 16 - (offset % 16); // padding
offset += (offset as *const u8).align_offset(align_of::<u128>()); // padding

#[allow(clippy::cast_ptr_alignment)]
let rent_epoch = *(input.add(offset) as *const u64);
Expand Down

0 comments on commit 08bece7

Please sign in to comment.