Zero-Copy Accounts
Zero-copy accounts use bytemuck layout and AccountLoader to avoid Borsh serialize/deserialize on large structs, saving CU for big state.
Search across all documentation pages
Zero-copy accounts use bytemuck layout and AccountLoader to avoid Borsh serialize/deserialize on large structs, saving CU for big state.
#[account(zero_copy)]
pub struct Market {
pub bids: [u64; 1024],
}
#[derive(Accounts)]
pub struct Update<'info> {
#[account(mut)]
pub market: AccountLoader<'info, Market>,
}When to reach for this: Account data is large (order books, tick arrays) and Borsh costs too much CU.
use anchor_lang::prelude::*;
#[account(zero_copy)]
#[repr(C)]
pub struct LargeState {
pub head: u64,
pub data: [u8; 10_000],
}
pub fn touch(ctx: Context<Touch>) -> Result<()> {
let mut market = ctx.accounts.state.load_mut()?;
market.head += 1;
Ok(())
}
#[derive(Accounts)]
pub struct Touch<'info> {
#[account(mut)]
pub state: AccountLoader<'info, LargeState>,
}What this demonstrates:
Avoid full account memcpy on each instruction.
| Alternative | Use When | Don't Use When |
|---|---|---|
| Borsh Account<T> | Small configs | Large fixed buffers |
| Account compression off-chain | No on-chain book | On-chain order book |
0.32.1 across advanced topics.
Large fixed layouts when Borsh CU too high.
Not easily; plan forward growth.
No; document separately.
Callee IDL JSON at compile time.
Yes with correct account metas.
Avoid different IDL per feature on mainnet.
LiteSVM, Surfpool, devnet simulation.
No; last resort.
See Related links for zero copy.
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026