Skip to content

Pinocchio

Pinocchio

Pinocchio is a zero-dependency Rust library for Solana development.

If you want to write high-performance, lightweight (small binary, low CU) on-chain programs on Solana, Pinocchio is a suitable choice.

Core Features

Zero Dependencies - Does not depend on solana-sdk - Smaller compiled binary size - Faster compilation speed - Fewer dependency conflicts

Performance Optimization - Hand-written optimized serialization - Reduced memory allocation - Faster instruction building

Lightweight - Minimal library size - Contains only essential functionality - Suitable for resource-constrained environments

Use Cases

High-Performance On-chain Programs Pinocchio is designed for on-chain programs with strict performance and size requirements: - DeFi protocol core logic - High-frequency trading programs - Programs with tight CU budgets - Scenarios requiring extreme optimization

Production-Grade Programs - Smaller program size means lower deployment costs - Lower CU consumption means cheaper transaction fees - Faster compilation speed improves development efficiency

Comparison with Other Frameworks

Feature Pinocchio Anchor Native (solana-program)
Dependencies Zero Many Moderate
Compile Time Very fast Slow Moderate
Program Size Very small Large Moderate
Developer Experience Low-level High-level Low-level
CU Consumption Very low Moderate Low
Learning Curve Steep Gentle Steep
Use Case Performance-critical General development Fine-grained control

Why Choose Pinocchio

1. Size Advantage Without solana-sdk dependencies, compiled programs can be 50%+ smaller:

Anchor program: ~200KB
Pinocchio program: ~50-100KB

2. CU Advantage Hand-written optimized serialization and deserialization reduces unnecessary computation: - Reduced memory copies - Optimized data layout - Minimized system calls

3. Compilation Speed Zero dependencies means extremely fast compilation:

Anchor: 1-2 minutes
Pinocchio: 10-20 seconds

Code Examples

Building Instructions

use pinocchio::{
    instruction::{AccountMeta, Instruction},
    pubkey::Pubkey,
};

let instruction = Instruction {
    program_id: system_program::ID,
    accounts: vec![
        AccountMeta::writable(from),
        AccountMeta::writable(to),
    ],
    data: transfer_data,
};

Serialization

// Efficient hand-written serialization
let mut data = vec![0u8; size];
data[0] = instruction_discriminator;
data[1..9].copy_from_slice(&amount.to_le_bytes());

Zero-Copy Pinocchio supports zero-copy techniques for directly operating on account data:

use pinocchio::account_info::AccountInfo;

// Read account data directly without deserialization
let data = account_info.data();
let value = u64::from_le_bytes(data[0..8].try_into().unwrap());

Real-World Examples

DeFi Program Optimization A DEX program rewrote its core logic using Pinocchio: - Program size: Reduced from 180KB to 85KB - CU consumption: Reduced from 45,000 to 28,000 - Compilation time: Reduced from 90 seconds to 15 seconds

High-Frequency Trading Bots On-chain arbitrage programs using Pinocchio: - Extremely low CU consumption makes small arbitrage profitable - Fast compilation supports rapid strategy iteration

Development Recommendations

When to use Pinocchio: - Extremely sensitive to CU consumption (e.g., arbitrage programs) - Need to minimize program size (lower deployment costs) - Team has extensive Rust and Solana experience - Pursuing extreme performance optimization - Existing mature programs that need optimization

When NOT to use Pinocchio: - Beginner projects (use Anchor instead) - Rapid prototyping (Anchor is more efficient) - Need rich toolchain support - Team lacks low-level optimization experience - High program logic complexity (Anchor's constraints are safer)

Using Pinocchio with Anchor

Pinocchio and Anchor can be used together: - Develop most business logic with Anchor - Rewrite performance-critical paths with Pinocchio - Call each other via CPI

Learning Resources

Official Resources - GitHub: https://github.com/anza-xyz/pinocchio - Example code: The examples directory in the repository

Community - Solana Discord #dev-questions channel - Anchor Discord (with Pinocchio discussions)

Important Notes

1. Security Zero dependencies means you need to handle many edge cases yourself: - Manually validate all inputs - Carefully handle memory boundaries - Thoroughly test edge cases

2. Maintenance Cost - Code is low-level and harder to maintain - Requires the team to have deep Rust expertise - Higher onboarding cost for new members

3. Debugging - Lacks Anchor's rich error messages - Requires more logging and testing

Performance Optimization Tips

1. Use the zero_copy macro Operate directly on memory, avoiding serialization overhead.

2. Pre-compute Data Layout Determine data structure layout at compile time.

3. Inline Critical Functions Use #[inline(always)] to reduce function call overhead.

4. Batch Operations Combine multiple operations to reduce system calls.

  • Anchor: Solana's mainstream high-level development framework, feature-rich but larger in size
  • solana-program: Solana's official native development library, which Pinocchio does not depend on
  • Zero Dependencies: No reliance on external libraries, all functionality self-implemented
  • Zero-Copy: Performance optimization technique that operates directly on memory, avoiding data copies
  • CU (Compute Units): The unit measuring program execution cost on Solana
  • Serialization: The process of converting data structures to byte streams, hand-written optimized in Pinocchio