# Compute Budget

Every Solana transaction has a compute budget measured in Compute Units (CU). Understanding CU costs helps you design efficient Rome contracts.

## Budget Overview

| Mode             | Max CU               | Notes                            |
| ---------------- | -------------------- | -------------------------------- |
| Atomic (VmAt)    | \~1,400,000 CU       | Single Solana transaction        |
| Iterative (VmIt) | Unlimited (multi-tx) | \~500 opcodes per iteration step |

Each Solana transaction has a default budget of 200,000 CU, extendable to \~1.4M CU via compute budget instructions (added automatically by the Rome SDK).

## CU Cost Estimates

### EVM Operations

| Operation                          | Approximate CU       | Notes                        |
| ---------------------------------- | -------------------- | ---------------------------- |
| Signature verification (ecrecover) | \~5,000 CU           | secp256k1 via Solana syscall |
| Simple transfer                    | \~50,000-100,000 CU  | Balance updates only         |
| ERC-20 transfer                    | \~100,000-150,000 CU | Includes SPL precompile call |
| Contract deployment (small)        | \~200,000-400,000 CU | Depends on bytecode size     |
| Storage write (SSTORE)             | \~5,000-20,000 CU    | Cold vs warm access          |

### CPI Operations

| Operation                           | Approximate CU | Notes                            |
| ----------------------------------- | -------------- | -------------------------------- |
| Transfer hook base overhead         | 100,000 CU     | Per transfer                     |
| Native sub-hook                     | 50,000 CU      | Per native Solana hook           |
| EVM sub-hook                        | 200,000 CU     | Per EVM hook                     |
| Recommended EVM transfer with hooks | 800,000 CU     | Safe budget for hooked transfers |

### Precompile Operations

| Precompile      | Approximate CU   |
| --------------- | ---------------- |
| ecrecover       | \~3,000-5,000 CU |
| SHA-256         | \~1,000 CU       |
| BN254 ecAdd     | \~10,000 CU      |
| BN254 ecMul     | \~40,000 CU      |
| BN254 ecPairing | \~200,000+ CU    |

## Optimization Techniques

### 1. Use Yul for Hot Paths

Solidity's optimizer produces reasonable code, but Yul (inline assembly) can reduce CU significantly for critical operations:

```solidity
// Before: ~600K CU
function createPairAccount(bytes32 token0, bytes32 token1) external {
    // Solidity-level operations
}

// After: ~150K CU (Yul optimization)
function createPairAccount(bytes32 token0, bytes32 token1) external {
    assembly {
        // Direct memory manipulation, skip ABI encoding overhead
    }
}
```

### 2. Cache PDA Derivations

PDA derivation via `find_program_address` is expensive. Store derived PDAs in contract storage rather than computing them on every call:

```solidity
mapping(address => bytes32) private cachedPdas;

function getPda(address user) internal returns (bytes32) {
    bytes32 cached = cachedPdas[user];
    if (cached != bytes32(0)) return cached;

    bytes32 pda = RomeEVMAccount.pda(user);
    cachedPdas[user] = pda;
    return pda;
}
```

### 3. Hardcode Known Program IDs

Don't load program IDs from storage — use constants:

```solidity
// Expensive: reads from storage
bytes32 splTokenProgram = storage_program_id;

// Cheap: compile-time constant
bytes32 constant SPL_TOKEN_PROGRAM = 0x06ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9;
```

### 4. Minimize Account Count

Each account in a Solana transaction adds CU overhead. Reduce the number of accounts by:

* Batching operations that share accounts
* Using fewer intermediate accounts
* Avoiding redundant ATA creation checks

### 5. Use Optimizer Settings

```javascript
// hardhat.config.js
solidity: {
  version: "0.8.28",
  settings: {
    optimizer: { enabled: true, runs: 200 },
  },
}
```

## Measuring CU Consumption

Use `eth_estimateGas` to measure CU before submitting:

```bash
cast estimate --rpc-url http://localhost:9090 \
  0xCONTRACT "myFunction(uint256)" 42
```

Or via ethers.js:

```javascript
const gas = await contract.myFunction.estimateGas(42);
console.log("Estimated gas:", gas.toString());
```

## What's Next

* [Constraints](/core-concepts/constraints.md) — full list of limits and boundaries
* [CU Optimization Guide](https://github.com/rome-protocol/docs/blob/main/developer-guides/cu-optimization.md) — detailed optimization cookbook


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rome.builders/core-concepts/compute-budget.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
