solitaire: shrink stack footprint by boxing FromAccounts::from()

This commit is contained in:
Stan Drozd 2022-08-03 16:19:29 +02:00 committed by Csongor Kiss
parent 5db83ea512
commit 2092484e73
3 changed files with 6 additions and 4 deletions

View File

@ -83,7 +83,7 @@ impl CreationLamports {
/// Trait definition that describes types that can be constructed from a list of solana account
/// references. A list of dependent accounts is produced as a side effect of the parsing stage.
pub trait FromAccounts<'a, 'b: 'a> {
fn from<T>(_: &'a Pubkey, _: &mut Iter<'a, AccountInfo<'b>>, _: &'a T) -> Result<Self>
fn from<T>(_: &'a Pubkey, _: &mut Iter<'a, AccountInfo<'b>>, _: &'a T) -> Result<Box<Self>>
where
Self: Sized;
}

View File

@ -64,7 +64,7 @@ macro_rules! solitaire {
let ix_data = BorshDeserialize::try_from_slice(d).map_err(|e| SolitaireError::InstructionDeserializeFailed(e))?;
let mut accounts = FromAccounts::from(p, &mut a.iter(), &())?;
$fn(&ExecutionContext{program_id: p, accounts: a}, &mut accounts, ix_data)?;
Persist::persist(&accounts, p)?;
Persist::persist(accounts.as_ref(), p)?;
Ok(())
}
}

View File

@ -53,7 +53,7 @@ pub fn derive_from_accounts(input: TokenStream) -> TokenStream {
let expanded = quote! {
/// Macro generated implementation of FromAccounts by Solitaire.
impl #combined_impl_g solitaire::FromAccounts #peel_type_g for #name #type_g {
fn from<DataType>(pid: &'a solana_program::pubkey::Pubkey, iter: &mut std::slice::Iter<'a, solana_program::account_info::AccountInfo<'b>>, data: &'a DataType) -> solitaire::Result<Self> {
fn from<DataType>(pid: &'a solana_program::pubkey::Pubkey, iter: &mut std::slice::Iter<'a, solana_program::account_info::AccountInfo<'b>>, data: &'a DataType) -> solitaire::Result<Box<Self>> {
#from_method
}
}
@ -108,7 +108,9 @@ fn generate_fields(name: &syn::Ident, data: &Data) -> TokenStream2 {
use solitaire::trace;
trace!("Peeling:");
#(#recurse;)*
Ok(#name { #(#names,)* })
// Necessary evil; Helps respect the 4K max
// stack frame size of the BPF VM
Ok(Box::new(#name { #(#names,)* }))
}
}