More efficient padding (#11656)

This commit is contained in:
Jack May
2020-08-17 10:24:34 -07:00
committed by GitHub
parent 750e5344f1
commit f1ba2387d3
4 changed files with 10 additions and 11 deletions

View File

@ -147,9 +147,10 @@ pub fn serialize_parameters_aligned(
v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64) v.write_u64::<LittleEndian>(keyed_account.data_len()? as u64)
.unwrap(); .unwrap();
v.write_all(&keyed_account.try_account_ref()?.data).unwrap(); v.write_all(&keyed_account.try_account_ref()?.data).unwrap();
for _ in 0..16 - (v.len() % 16) { v.resize(
v.write_u8(0).unwrap(); // 128 bit aligned again v.len() + (v.len() as *const u8).align_offset(align_of::<u128>()),
} 0,
);
v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64) v.write_u64::<LittleEndian>(keyed_account.rent_epoch()? as u64)
.unwrap(); .unwrap();
} }
@ -188,7 +189,7 @@ pub fn deserialize_parameters_aligned(
.data .data
.clone_from_slice(&buffer[start..end]); .clone_from_slice(&buffer[start..end]);
start += keyed_account.data_len()?; // data 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 start += mem::size_of::<u64>(); // rent_epoch
} else { } else {
start += 7; // padding start += 7; // padding

View File

@ -297,7 +297,7 @@ static bool sol_deserialize(
uint64_t data_len = *(uint64_t *) input; uint64_t data_len = *(uint64_t *) input;
input += sizeof(uint64_t); input += sizeof(uint64_t);
input += data_len; input += data_len;
input += 16 - (data_len % 16); // padding input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
input += sizeof(uint64_t); input += sizeof(uint64_t);
} }
continue; continue;
@ -334,8 +334,7 @@ static bool sol_deserialize(
input += sizeof(uint64_t); input += sizeof(uint64_t);
params->ka[i].data = (uint8_t *) input; params->ka[i].data = (uint8_t *) input;
input += params->ka[i].data_len; input += params->ka[i].data_len;
input = (uint8_t*)(((uint64_t)input + 8 - 1) & ~(8 - 1)); // padding
input += 16 - (params->ka[i].data_len % 16); // padding
// rent epoch // rent epoch
params->ka[i].rent_epoch = *(uint64_t *) input; params->ka[i].rent_epoch = *(uint64_t *) input;

View File

@ -5,7 +5,7 @@ use crate::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubk
use alloc::vec::Vec; use alloc::vec::Vec;
use std::{ use std::{
cell::RefCell, cell::RefCell,
mem::size_of, mem::{align_of, size_of},
rc::Rc, rc::Rc,
// Hide Result from bindgen gets confused about generics in non-generic type declarations // Hide Result from bindgen gets confused about generics in non-generic type declarations
result::Result as ResultGeneric, result::Result as ResultGeneric,
@ -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; let executable = *(input.add(offset) as *const u8) != 0;
offset += size_of::<u8>(); offset += size_of::<u8>();
offset += 4; // padding offset += size_of::<u32>(); // padding to u64
let key: &Pubkey = &*(input.add(offset) as *const Pubkey); let key: &Pubkey = &*(input.add(offset) as *const Pubkey);
offset += size_of::<Pubkey>(); offset += size_of::<Pubkey>();
@ -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) from_raw_parts_mut(input.add(offset), data_len)
})); }));
offset += data_len; offset += data_len;
offset += (offset as *const u8).align_offset(align_of::<u128>()); // padding
offset += 16 - (offset % 16); // padding
#[allow(clippy::cast_ptr_alignment)] #[allow(clippy::cast_ptr_alignment)]
let rent_epoch = *(input.add(offset) as *const u64); let rent_epoch = *(input.add(offset) as *const u64);