Measuring and reducing compute units in Solana programs
How the compute budget works, four ways to measure what an instruction costs, and the changes that cut it most: stored bumps, less logging, zero-copy accounts and fewer CPIs.
The budget
My Android work, like pinning threads to cores, starts from one question: where is the time going? On Solana the question has a hard limit attached. Every instruction runs under a compute budget, and the transaction fails when it runs out. Priority fees are also charged per compute unit requested, so cost is money as well as time.
A compute unit (CU) is roughly one eBPF instruction executed. Syscalls cost a fixed amount on top. The numbers I work from:
| Limit or cost | Value |
|---|---|
| Default limit per instruction | 200,000 CU |
| Maximum per transaction | 1,400,000 CU |
create_program_address (one PDA hash) |
1,500 CU |
Logging a string with msg! |
~100 CU, more with formatting |
| Cross-program invocation | 1,000 CU base, plus the callee's cost |
A transaction can change its limit with the Compute Budget program's SetComputeUnitLimit. Without it, the limit is the per-instruction default times the number of instructions, capped at 1.4M.
Four ways to measure
From quickest to most precise.
Program logs. Every transaction's logs end with:
Program 7xKX...q9 consumed 18342 of 200000 compute units
Enough to compare two versions of an instruction.
Inside the program. sol_log_compute_units() logs the remaining budget. Two calls around a block give its cost:
use solana_program::log::sol_log_compute_units;
sol_log_compute_units();
let total = ctx.accounts.pool.recalculate()?;
sol_log_compute_units();
These cost compute themselves, so they come out before shipping.
In tests. LiteSVM and Mollusk run the SVM in-process and report the units each transaction consumed, so a budget can be a test:
let result = svm.send_transaction(tx).unwrap();
assert!(
result.compute_units_consumed < 25_000,
"deposit used {} CU",
result.compute_units_consumed,
);
Mollusk's bencher writes a Markdown table of units per instruction. Commit it and every change shows up as a diff in review.
Before sending. simulateTransaction returns unitsConsumed. The client sets the limit to that plus a margin.
Where the units go
Searching for bumps
find_program_address tries bumps from 255 down at about 1,500 CU per attempt. In Anchor, a bare bump constraint runs that search on every call. Store the canonical bump at creation and check against it:
#[account(
seeds = [b"vault", user.key().as_ref()],
bump = vault.bump, // one hash instead of a search
)]
pub vault: Account<'info, Vault>,
The cheapest fix there is, and almost every program needs it. Background in program derived addresses.
Logging
msg! with format arguments runs Rust's formatter on-chain. Logging a Pubkey base58-encodes 32 bytes, which is worse. Log less, log fixed strings, and put anything a client needs in events or return data.
Deserialising large accounts
Account<T> Borsh-deserialises the whole account on entry and serialises it on exit. Fine for a few hundred bytes. For an order book or a large array it dominates the instruction. Zero-copy accounts (#[account(zero_copy)] with AccountLoader) read the bytes in place as a struct. The cost: a fixed layout, Pod fields, and no String or Vec.
Cross-program invocations
A CPI pays a base cost, the serialisation of accounts and data across the boundary, and whatever the callee spends. Moving SOL out of an account your program owns needs no CPI: the owner can debit lamports directly. Batching transfers into one instruction helps too.
Rust habits
- No heap allocation in hot paths.
VecandStringcost compute, and the heap is 32 KiB by default. - Integers, not floats. The VM has no floating-point unit; floats are emulated in software.
opt-level = 3, and if size allows,lto = "fat"andcodegen-units = 1.- Box large account structs in the accounts context. That's the 4 KiB stack frame limit rather than compute, but the same programs hit it.
The hottest programs drop Anchor for minimal libraries like Pinocchio that avoid allocations and copies entirely. I want the Anchor version understood and measured before I go there.
Request the right limit
The priority fee is charged on the limit requested, not on the units used. A tight limit saves money and helps scheduling, because block producers pack blocks by compute:
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 30_000 }),
ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 5_000 }),
depositInstruction,
);
How that price turns into a fee is the next thing to work through.