1 个不稳定版本
0.1.0 | 2024 年 5 月 9 日 |
---|
#22 在 #invoke
12KB
142 行
solana-invoke
是 solana_program::program::invoke*
的一个即插即用替代品,具有更好的计算和堆效率
摘要
当前的 CPI 函数 solana_program::program::invoke*
执行了不必要的复制和分配。这个包以100%向后兼容的方式消除了这些低效之处。
计算和堆节省的规模与通过 CPI 传递的账户和数据量成正比。即使在 test-program/
中的测试程序中,它只传递了两个账户和 O(16 字节) 的数据,也观察到显著的节省(开销从 536 cus -> 197 cus 减少)。
// test-program schematic. logs and asserts are redacted.
// A simple solana program that transfers 1 lamport twice
fn process_instruction(
_program_id: &Pubkey,
accounts: &[AccountInfo],
_data: &[u8],
) -> ProgramResult {
// Send from account zero to account one, thrice.
// 1) First with standard invoke.
// 2) Then with our invoke
// 3) Then with our invoke_signed_unchecked
let transfer =
solana_program::system_instruction::transfer(accounts[0].key, accounts[1].key, 1);
// 1) First with standard invoke_signed.
solana_program::program::invoke(&transfer, &accounts[..2])?;
// 2) Then with our invoke_signed
solana_invoke::invoke(&transfer, &accounts[..2])?;
// 3) Then with our invoke_signed
solana_invoke::invoke_unchecked(&transfer, &accounts[..2])?;
Ok(())
}
输出
Program 1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM invoke [1]
Program log: invoking system program via solana_program::program::invoke
Program 11111111111111111111111111111111 invoke [2]
Program 11111111111111111111111111111111 success
Program log: invoked system program via solana_program::program::invoke successfully: 536 cus
Program log: invoking system program via our invoke
Program 11111111111111111111111111111111 invoke [2]
Program 11111111111111111111111111111111 success
Program log: invoked system program via our invoke successfully: 392 cus
Program log: invoking system program via our invoke
Program 11111111111111111111111111111111 invoke [2]
Program 11111111111111111111111111111111 success
Program log: invoked system program via our invoke successfully: 197 cus
Program 1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM consumed 7864 of 200000 compute units
Program 1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM success
依赖项
~16–25MB
~422K SLoC