in-solana by Supanode

Optimizing Compute Units on Solana

Compute units (CUs) measure how much processing a transaction requires. Every transaction has a compute unit limit — if exceeded, the transaction fails. Setting the right limit affects both cost and landing rate.

Why compute units matter

  1. Priority fee cost. Priority fee = computeUnitPrice × computeUnitsConsumed / 1,000,000. A lower CU limit means a lower total fee.
  2. Transaction scheduling. Validators pack transactions to maximize fees within a block’s compute budget. Tighter CU limits make your transaction easier to fit.
  3. Failure prevention. Setting the limit too low causes ComputationalBudgetExceeded. Too high wastes fee budget.

Estimating compute units

Simulate the transaction first:

import { createSolanaRpc, getBase64EncodedWireTransaction } from "@solana/kit";

const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");

const simulation = await rpc.simulateTransaction(
  getBase64EncodedWireTransaction(transaction),
  { encoding: "base64", replaceRecentBlockhash: true }
).send();

const cuUsed = simulation.value.unitsConsumed;
// Set limit to actual + 20% margin
const cuLimit = Math.ceil(Number(cuUsed) * 1.2);

Common CU costs

OperationTypical CUs
SOL transfer150-300
SPL Token transfer3,000-5,000
Token account creation (ATA)20,000-25,000
Simple swap (Jupiter, 1 route)100,000-200,000
Complex swap (multi-hop)200,000-400,000
Anchor program call10,000-200,000

Default limits

  • Per instruction: 200,000 CUs (if setComputeUnitLimit is not set)
  • Per transaction: 1,400,000 CUs (maximum)
  • Block limit: 48,000,000 CUs

Optimization strategies

  1. Always simulate first. Never guess CU limits — simulate and add a small margin.
  2. Set explicit limits. Without setComputeUnitLimit, each instruction gets 200,000 CUs. A 3-instruction transaction defaults to 600,000 CUs — far more than a simple transfer needs.
  3. Reduce CU consumption in programs. Minimize allocations, avoid unnecessary CPIs, precompute values off-chain.
  4. Test on devnet. CU consumption is deterministic for the same inputs — devnet results apply to mainnet.

FAQ

What happens if I do not set a compute unit limit? Each instruction gets 200,000 CUs by default. The total limit is the sum of all instruction defaults, capped at 1,400,000.

Can I set the limit to exactly what simulation shows? Risky. Account state can change between simulation and execution. Add 10-20% margin to avoid failures.

Does unused compute cost anything? No. You only pay for consumed CUs, not allocated CUs. But a high limit reduces your effective priority per CU, which can affect scheduling priority.

Updated: