> For the complete documentation index, see [llms.txt](https://docs.rome.builders/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rome.builders/id/konsep-inti/compute-budget.md).

# Anggaran Compute

Setiap transaksi Solana memiliki anggaran komputasi yang diukur dalam Unit Komputasi (CU). Memahami biaya CU membantu Anda merancang kontrak Rome yang efisien.

## Gambaran Anggaran

| Mode            | CU Maks                   | Catatan                                                         |
| --------------- | ------------------------- | --------------------------------------------------------------- |
| Atomik (VmAt)   | \~1.400.000 CU            | Satu transaksi Solana                                           |
| Iteratif (VmIt) | Tidak terbatas (multi-tx) | Opcode/langkah adaptif — disesuaikan dengan anggaran CU tiap tx |

Setiap transaksi Solana memiliki anggaran default 200.000 CU, yang dapat diperluas hingga \~1,4 juta CU melalui instruksi anggaran komputasi (ditambahkan secara otomatis oleh SDK Rome).

## Perkiraan Biaya CU

### Operasi EVM

| Operasi                             | Perkiraan CU         | Catatan                           |
| ----------------------------------- | -------------------- | --------------------------------- |
| Verifikasi tanda tangan (ecrecover) | \~5.000 CU           | secp256k1 melalui syscall Solana  |
| Transfer sederhana                  | \~50.000-100.000 CU  | Hanya pembaruan saldo             |
| Transfer ERC-20                     | \~100.000-150.000 CU | Termasuk panggilan precompile SPL |
| Penerapan kontrak (kecil)           | \~200.000-400.000 CU | Tergantung pada ukuran bytecode   |
| Penulisan storage (SSTORE)          | \~5.000-20.000 CU    | Akses dingin vs hangat            |

### Operasi Precompile

| Precompile      | Perkiraan 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    |

## Teknik Optimisasi

### 1. Gunakan Yul untuk Jalur Panas

Optimizer Solidity menghasilkan kode yang cukup baik, tetapi Yul (inline assembly) dapat mengurangi CU secara signifikan untuk operasi kritis:

```solidity
// Sebelum: ~600K CU
function createPairAccount(bytes32 token0, bytes32 token1) external {
    // Operasi tingkat Solidity
}

// Sesudah: ~150K CU (optimisasi Yul)
function createPairAccount(bytes32 token0, bytes32 token1) external {
    assembly {
        // Manipulasi memori langsung, lewati overhead encoding ABI
    }
}
```

### 2. Cache Derivasi PDA

Derivasi PDA melalui `find_program_address` mahal. Simpan PDA yang telah diturunkan di storage kontrak, bukan menghitungnya pada setiap panggilan:

```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 ID Program yang Diketahui

Jangan memuat ID program dari storage — gunakan konstanta:

```solidity
// Mahal: membaca dari storage
bytes32 splTokenProgram = storage_program_id;

// Murah: konstanta saat kompilasi
bytes32 constant SPL_TOKEN_PROGRAM = 0x06ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9;
```

### 4. Minimalkan Jumlah Akun

Setiap akun dalam transaksi Solana menambah overhead CU. Kurangi jumlah akun dengan:

* Mengelompokkan operasi yang berbagi akun
* Menggunakan lebih sedikit akun perantara
* Menghindari pemeriksaan pembuatan ATA yang redundan

### 5. Gunakan Pengaturan Optimizer

```typescript
// hardhat.config.ts — aktifkan optimizer dalam profil build
solidity: {
  profiles: {
    default: { version: "0.8.28" },
    production: {
      version: "0.8.28",
      settings: { optimizer: { enabled: true, runs: 200 } },
    },
  },
}
```

Bangun dengan `npx hardhat compile --build-profile production`.

## Mengukur Konsumsi CU

Gunakan `eth_estimateGas` untuk mengukur CU sebelum mengirim:

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

Atau melalui ethers.js:

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

## Apa Selanjutnya

* [Kendala](/id/konsep-inti/constraints.md) — daftar lengkap batasan dan batas-batas


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.rome.builders/id/konsep-inti/compute-budget.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
