Advanced Anchor Best Practices
Efficient, production-grade Anchor 0.32.1 programs combine optimization with unchanged security bar.
How to Use This List
- Use after basics, accounts, and CPI sections are solid.
- Profile before applying zero_copy or native CPI.
- Security review still required.
A - Performance
- Profile CU on LiteSVM before mainnet data-driven optimization only.
- Store bumps and prefer zero_copy for large fixed state when profiling justifies.
- Strip debug features from mainnet release builds smaller .so and lower CU.
B - Advanced patterns
- Document remaining_accounts protocols not in IDL.
- Plan realloc/migrations with version bytes before growing accounts.
- Prefer declare_program! over manual invoke when IDL exists.
- Keep custom account types rare and tested default to Account<T>.
FAQs
Anchor version?
0.32.1 across advanced topics.
When to use zero_copy?
Large fixed layouts when Borsh CU too high.
Is realloc reversible?
Not easily; plan forward growth.
Are remaining accounts in IDL?
No; document separately.
declare_program! needs what?
Callee IDL JSON at compile time.
Can Anchor CPI to Pinocchio?
Yes with correct account metas.
Feature flags and IDL?
Avoid different IDL per feature on mainnet.
How to measure CU?
LiteSVM, Surfpool, devnet simulation.
Custom account types common?
No; last resort.
Next steps?
See Related links for advanced bp.
Related
- CU & Size Optimization - CU
- Zero-Copy Accounts - zero_copy
- Anchor Basics Best Practices - foundation
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.