Compute Unit Optimization in Solana Swap Execution
Setting a high priority fee on Solana without explicitly requesting an accurate Compute Unit (CU) limit is one of the most common mistakes in transaction building. Over-requesting CUs throttles transaction scheduling, while under-requesting causes instant runtime execution failure.
When routing DEX swaps across multiple liquidity pools, static CU allocations degrade both landing reliability and execution cost. Tuning CU limits programmatically is a fundamental requirement for production Solana infrastructure.
How the Solana Scheduler Evaluates Compute Requests
Every instruction in a Solana transaction consumes Compute Units. Simple System Program transfers require 150 CUs, while complex DEX routing involving multiple Automated Market Maker (AMM) state reads, token account validations, and price checks can consume anywhere from 60,000 to 300,000 CUs.
By default, if no SetComputeUnitLimit instruction is included, the runtime assigns a default limit of 200,000 CUs per instruction, up to a maximum transaction cap of 1,400,000 CUs.
The Compute Budget program provides two instruction types to control execution mechanics:
-
SetComputeUnitLimit: Declares the maximum CUs the transaction may consume. -
SetComputeUnitPrice: Sets the priority fee rate in micro-lamports per requested CU.
Total Priority Fee (lamports) = (Requested CU Limit * Micro-Lamports per CU) / 1,000,000
Notice the critical parameter: total priority fee is calculated on requested CUs, not actual consumed CUs.
If a swap consumes 75,000 CUs but requests the default 1,400,000 CUs at a price of 50,000 micro-lamports/CU:
- Declared Limit: 1,400,000 CUs
- Priority Fee Paid: 70,000 lamports
- Consumed CUs: 75,000 CUs
You pay for 1,400,000 CUs of block space reservation regardless of execution consumption.
The Block Scheduler Contention Problem
The impact extends beyond unnecessary fee spend. Solana block producers (validators) schedule transactions for parallel execution based on local write-lock contention and thread availability.
When a validator thread evaluates a transaction, it reserves the declared CU limit against the total block CU cap (48 million CUs per block) and per-account write-lock limits (12 million CUs per account per block).
An over-allocated transaction claiming 1,400,000 CUs takes up a massive scheduling footprint. If a heavily requested liquidity pool account has 11 million CUs already scheduled in the current block, a transaction requesting 1.4 million CUs cannot fit into the remaining 1 million CU allowance—even if its real execution footprint is only 80,000 CUs. The scheduler defers the transaction to a subsequent block, causing execution delay or timeouts.
Profile-Based CU Estimation Pattern
To maximize landing probability while minimizing priority fee spend, swap execution pipelines simulate the built transaction before final assembly.
import { Connection, VersionedTransaction } from '@solana/web3.js';
async function estimateSwapComputeUnits(
connection: Connection,
transaction: VersionedTransaction
): Promise<number> {
const simulation = await connection.simulateTransaction(transaction, {
replaceRecentBlockhash: true,
sigVerify: false,
});
if (simulation.value.err) {
throw new Error(`Simulation failed: ${JSON.stringify(simulation.value.err)}`);
}
const unitsConsumed = simulation.value.unitsConsumed;
if (!unitsConsumed) {
return 200000; // Safe fallback default
}
// Add a 12% safety margin to account for state variations between slots
return Math.ceil(unitsConsumed * 1.12);
}
Why a Safety Buffer Matters
State on-chain changes continuously between slots. A multi-hop swap route traversing Token-2022 mints with dynamic transfer hooks, or AMM pools with changing tick arrays, may consume slightly different CU amounts depending on account initialization states and reserve ratios.
A 10% to 15% safety buffer prevents execution failures when block state shifts between simulation and block landing.
Practical Execution
At Verixia, execution pipelines profile route graph depth and simulate transaction payloads to set optimal compute bounds prior to user wallet signatures.
Explicitly requesting precise CU bounds ensures that priority fees directly translate to block scheduler priority without wasting SOL or clogging validator execution threads.
Written by the team at Verixia, a Solana swap interface routing through Jupiter.
Top comments (0)