Skip to main content
ClaudeWave
Slash Command65 estrellas del repoactualizado yesterday

profile-cu

Profile compute unit usage per instruction in a Solana program

Instalar en Claude Code
Copiar
mkdir -p ~/.claude/commands && curl -fsSL https://raw.githubusercontent.com/solanabr/solana-ai-kit/HEAD/.claude/commands/profile-cu.md -o ~/.claude/commands/profile-cu.md
Después abre una sesión nueva de Claude Code; el slash command carga automáticamente.

profile-cu.md

You are profiling CU (compute unit) consumption for a Solana program. This identifies expensive instructions and optimization opportunities.

## Related Skills

- [ext/solana-dev/skill/references/programs/pinocchio.md](../skills/ext/solana-dev/skill/references/programs/pinocchio.md) - CU optimization patterns
- [ext/solana-dev/skill/references/testing.md](../skills/ext/solana-dev/skill/references/testing.md) - Test framework CU measurement

## Step 1: Verify Program

```bash
echo "Detecting program..."

if [ ! -f "Anchor.toml" ]; then
    echo "No Anchor.toml found. This command requires an Anchor project."
    echo "For native programs, use 'solana program show <PROGRAM_ID>' and manual CU logging."
    exit 1
fi

# Extract program IDs from Anchor.toml
echo "Programs in Anchor.toml:"
grep -A 5 '\[programs' Anchor.toml

# Check if deployed (optional)
CLUSTER=$(grep -m1 'cluster' Anchor.toml | sed 's/.*= *//' | tr -d '"')
echo "Cluster: ${CLUSTER:-localnet}"
```

## Step 2: Build Program

```bash
echo "Building program..."
anchor build

if [ $? -ne 0 ]; then
    echo "Build failed. Fix compilation errors before profiling."
    exit 1
fi

# Report binary size
echo ""
echo "Program binary sizes:"
ls -lh target/deploy/*.so | awk '{print $5, $9}'
```

## Step 3: Run Tests with CU Logging

```bash
echo "Running tests with CU profiling enabled..."

# Set environment for verbose CU logging
export SBF_OUT_DIR=target/deploy
export RUST_LOG=solana_runtime::message_processor=trace

# Run Anchor tests and capture output
anchor test --skip-deploy 2>&1 | tee /tmp/cu-profile-output.txt

TEST_STATUS=$?
if [ $TEST_STATUS -ne 0 ]; then
    echo "Tests failed. Fix test failures before profiling CU."
    exit 1
fi
```

## Step 4: Parse CU Usage

Extract CU consumption from test output. Look for patterns like:
- `consumed X of Y compute units`
- `Program <ID> consumed <N> of <M> compute units`

```bash
echo ""
echo "=== CU Profile Results ==="
echo ""

# Extract CU lines from test output
grep -i "consumed.*compute units" /tmp/cu-profile-output.txt | \
    sed 's/.*Program //' | \
    sort -t' ' -k3 -n -r | \
    head -50

echo ""
echo "--- Per-Instruction Breakdown ---"
echo ""

# Parse into table format
grep -i "consumed.*compute units" /tmp/cu-profile-output.txt | \
    awk '{
        for(i=1;i<=NF;i++) {
            if($i=="consumed") { cu=$(i+1) }
            if($i=="of") { limit=$(i+1) }
        }
        if(cu && limit) {
            pct = (cu/limit)*100
            printf "%-60s %8s / %-8s (%5.1f%%)\n", $0, cu, limit, pct
        }
    }'
```

## Step 5: Compare with Baseline

```bash
BASELINE=".claude/benchmarks/cu-baseline.json"

if [ -f "$BASELINE" ]; then
    echo ""
    echo "=== Baseline Comparison ==="
    echo ""
    echo "Baseline file: $BASELINE"
    echo ""

    # Read baseline and compare
    # Format expected: {"instruction_name": cu_value, ...}
    echo "Instruction             | Baseline | Current  | Delta    | Status"
    echo "------------------------|----------|----------|----------|--------"

    # Parse current results into comparable format
    # The agent should read the baseline JSON and compare instruction-by-instruction
    cat "$BASELINE"
else
    echo ""
    echo "No baseline found at $BASELINE"
    echo "To create a baseline, save current results:"
    echo "  mkdir -p .claude/benchmarks"
    echo "  echo '{\"instruction_name\": cu_value}' > $BASELINE"
fi
```

## Step 6: Optimization Analysis

Analyze the CU profile and flag optimization opportunities:

### CU Thresholds

| Range | Status | Action |
|-------|--------|--------|
| < 50,000 CU | Efficient | No action needed |
| 50,000 - 150,000 CU | Normal | Review if hot path |
| 150,000 - 300,000 CU | Heavy | Optimize data access, reduce allocations |
| > 300,000 CU | Critical | Refactor: split instruction, use zero-copy, cache PDAs |

### Common CU Optimizations

1. **Store PDA bumps** - `find_program_address` costs ~1,500 CU per call; `create_program_address` with stored bump costs ~200 CU
2. **Zero-copy deserialization** - Use `AccountLoader` instead of `Account` for large structs
3. **Reduce logging** - `msg!()` costs ~100 CU per call; use `#[cfg(feature = "debug")]` guards
4. **Minimize account loads** - Each account deserialization has a CU cost proportional to data size
5. **Use Pinocchio** - For CU-critical paths, consider Pinocchio over Anchor (~50-70% CU reduction)

## After Profiling

- [ ] CU usage per instruction documented
- [ ] High-CU instructions identified
- [ ] Baseline saved to `.claude/benchmarks/cu-baseline.json`
- [ ] Optimization plan created for instructions > 150,000 CU
- [ ] Re-profile after optimizations to verify improvement
anchor-engineerSubagent

Anchor framework specialist for rapid Solana program development. Use for building programs with Anchor macros, IDL generation, account validation, and standardized patterns. Prioritizes developer experience while maintaining security.\\n\\nUse when: Building new programs quickly, team projects needing standardization, projects requiring IDL for client generation, or when developer experience is prioritized over maximum CU optimization.

defi-engineerSubagent

DeFi integration specialist for composing with Solana protocols including Jupiter, Drift, Kamino, Raydium, Orca, Meteora, Marginfi, and Sanctum. Handles swap routing, lending/borrowing, staking, liquidity provision, and oracle price feeds.\n\nUse when: Integrating DeFi protocols, building swap interfaces, implementing lending/borrowing, setting up yield strategies, working with Pyth/Switchboard oracles, or composing multi-protocol transactions.

devops-engineerSubagent

CI/CD, infrastructure, and deployment specialist for Solana projects. Handles GitHub Actions, Docker, monitoring, RPC management, and Cloudflare Workers edge deployment.\n\nUse when: Setting up CI/CD pipelines, containerizing Solana validators or programs, configuring monitoring and alerting, managing RPC infrastructure, deploying edge workers, or automating build and deploy workflows.

game-architectSubagent

Senior Solana game architect for game system design, Unity/C# architecture, on-chain game state, player progression, NFT integration, and PlaySolana ecosystem. Use for high-level game design decisions, architecture reviews, and planning complex game systems.\n\nUse when: Designing new Solana games from scratch, planning game state on-chain, Unity project architecture, integrating with PlaySolana/PSG1, or deciding between implementation approaches.

mobile-engineerSubagent

React Native and Expo specialist for building Solana mobile dApps. Handles mobile wallet adapter integration, transaction signing UX, deep linking, and mobile-specific performance optimization.\n\nUse when: Building React Native or Expo mobile apps with Solana integration, implementing mobile wallet adapter flows, setting up deep links for transaction signing, or optimizing mobile dApp performance.

pinocchio-engineerSubagent

CU optimization specialist using Pinocchio framework. Use for performance-critical programs requiring 80-95% CU reduction vs Anchor. Specializes in zero-copy access, manual validation, and minimal binary size.\\n\\nUse when: CU limits are being hit, transaction costs are significant at scale, binary size must be minimized, or maximum throughput is required.

rust-backend-engineerSubagent

Rust backend specialist for building async services that interact with Solana blockchain. Builds APIs, indexing services, and off-chain processing using Axum, Tokio, and modern async patterns.\n\nUse when: Building REST/WebSocket APIs for Solana dApps, implementing transaction indexers, creating webhook services, or any Rust backend that interacts with Solana.

solana-architectSubagent

Senior Solana program architect for system design, account structures, PDA schemes, token economics, and cross-program composability. Use for high-level design decisions, architecture reviews, and planning complex multi-program systems.\n\nUse when: Designing new programs from scratch, planning account structures, optimizing PDA schemes, reviewing architecture for security, or deciding between implementation approaches.