A comprehensive tool for measuring the Compute Unit (CU) costs of Solana's create_program_address function across different seed combinations.
Understanding the computational cost of Program Derived Addresses (PDAs) is crucial for Solana developers. The create_program_address function is widely used in Solana programs for:
- Creating deterministic addresses for program-owned accounts
- Implementing token accounts, metadata accounts, and various program state accounts
- Building complex DeFi protocols that require predictable address derivation
However, the CU cost of create_program_address can vary somewhat depending on:
- Number of seeds used
- Length and content of seeds
- Whether seeds are strings, pubkeys, or mixed types
- The specific combination of seed data
This variability makes it difficult to:
- Budget CU costs accurately in complex programs
- Optimize seed combinations for gas efficiency
- Predict transaction costs for programs with multiple PDA derivations
This tool provides comprehensive CU cost analysis by:
-
Testing >50 different seed combinations including:
- String seeds of varying lengths
- Single and multiple pubkey seeds
- Mixed string + pubkey combinations
- Edge cases (empty strings, very long strings)
- Real-world patterns (token accounts, metadata, governance)
-
Running 20 independent test cycles with fresh randomized pubkeys each time
-
Measuring precise CU costs before and after each
create_program_addresscall -
Calculating absolute minimum and maximum CU costs across all runs
Based on our comprehensive testing across 20 runs with 50 different seed combinations each:
================================================================================
🎯 FINAL RESULTS ACROSS 20 RUNS
================================================================================
📉 ABSOLUTE MINIMUM CU COST: 1644 CUs
📈 ABSOLUTE MAXIMUM CU COST: 1669 CUs
📊 ABSOLUTE CU COST RANGE: 25 CUs
💰 AVERAGE ESTIMATE: 1656 CUs
🔍 Analysis:
• Cheapest create_program_address operation: 1644 CUs
• Most expensive create_program_address operation: 1669 CUs
• Cost variability: 25 CUs difference- Consistent Performance: The CU cost is remarkably stable, varying by only 25 CUs (1.5%) across all test scenarios
- Predictable Budget: Developers can safely budget ~1660 CUs per
create_program_addresscall - Low Variability: Seed combination complexity has minimal impact on CU cost
- Reliable Estimation: For programs with multiple PDA derivations, multiply 1660 CUs by the number of calls
- Rust and Cargo installed
- Solana CLI tools installed (
solana,solana-test-validator) - Node.js/npm (if using Solana toolchain)
- Clone and build the program:
git clone <repository-url>
cd create-program-address-cus
cargo build-bpf -p create-program-address-cus- Start the Solana test validator:
./sh/run-test-validator.shThis script will:
- Reset the validator ledger
- Preload the program with the correct program ID
- Enable logging to see CU measurements
- Run the CU measurement client (in a new terminal):
cargo run -p client-
Client Setup:
- Connects to local test validator
- Requests SOL airdrop for transaction fees
- Generates 3 random pubkeys for seed testing
-
Test Execution (20 cycles):
- Each cycle runs 50 different seed combinations
- Measures CU cost before/after each
create_program_addresscall - Tracks successful vs failed derivations
- Reports min/max CU costs per cycle
-
Final Analysis:
- Calculates absolute minimum and maximum across all 1000 tests (20 × 50)
- Provides cost range and variability analysis
- Gives actionable budgeting recommendations
create-program-address-cus/
├── program/ # Solana program (BPF)
│ ├── src/lib.rs # Main program logic
│ └── Cargo.toml # Program dependencies
├── client/ # Test client
│ ├── src/main.rs # Client that runs 20 test cycles
│ └── Cargo.toml # Client dependencies
├── sh/
│ └── run-test-validator.sh # Script to start validator
├── Cargo.toml # Workspace configuration
└── README.md # This file
- Language: Rust using
solana-programcrate - Program Type: On-chain Solana program (BPF)
- CU Measurement: Uses
sol_remaining_compute_units()syscall - Seed Handling: Accepts 3 pubkey accounts as input for dynamic testing
- Sample Size: 1000 total measurements (20 runs × 50 combinations)
- Seed Variety: Comprehensive coverage of real-world usage patterns
- Measurement Precision: Direct syscall measurement before/after each operation
- Error Handling: Failed derivations are logged but excluded from min/max calculations
- Randomization: Fresh pubkeys generated for each of the 20 test cycles
- Smart Contract Development: Budget CU costs accurately in your programs
- Transaction Planning: Estimate total CU consumption for complex operations
- Gas Optimization: Understand that seed complexity has minimal impact
- Capacity Planning: Plan for maximum CU usage in worst-case scenarios
- Cost Analysis: Calculate transaction fees for applications using PDAs
Feel free to submit issues or pull requests to improve the test coverage or measurement accuracy.
[Add your license here]