> 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/ja/ji-ben-gai-nian/compute-budget.md).

# Compute Budget

Rome EVM操作におけるSolanaのコンピュートユニット（CU）コスト — アトミックとイテレーティブの予算、そしてCU効率の高いSolidityコントラクトの設計方法。

すべての Solana トランザクションには、Compute Units（CU）で測定されるコンピュート予算があります。CU のコストを理解することで、効率的な Rome コントラクトを設計できます。

## 予算の概要

| モード         | 最大 CU                                       | 注記                                                                        |
| ----------- | ------------------------------------------- | ------------------------------------------------------------------------- |
| アトミック（VmAt） | 約 1,400,000 CU                              | 単一の Solana トランザクション                                                       |
| 反復的（VmIt）   | 各レッグあたり最大約 1,400,000 CU、セッションあたり最大 160 回の反復 | 各レッグはそれぞれ独立した Solana トランザクションです。アダプティブなオペコード／ステップを各 tx の CU 予算に合わせて詰め込みます |

各 Solana トランザクションのデフォルト予算は 200,000 CU で、Rome SDK によって自動的に追加される compute budget instructions により約 1.4M CU まで拡張できます。

反復実行は無制限ではありません。1 つのセッションは 160 回の反復で終了し、Rust 版 Rome SDK の反復コンポーザーは最大 128 レッグまで計画します。ウォレット自体が署名するプランは `rome_submitSignedV1Plan`に進み、1 回の呼び出しで最大 64 トランザクションまで受け入れます。ほとんどの呼び出しはそれよりかなり少なくて済みます。レガシートラックの CPI は反復セッション内では拒否されるため、それらを発生させる呼び出しはアトミック予算内に収める必要があります。参照 [中継呼び出しと長い呼び出し](https://docs.rome.builders/ja/gaido/call-evm-from-solana.md#relayed-calls-and-long-calls) および [制約](https://docs.rome.builders/ja/ji-ben-gai-nian/constraints.md).

## CU コストの見積もり

### EVM 操作

| 操作                       | 概算 CU                | 注記                                                              |
| ------------------------ | -------------------- | --------------------------------------------------------------- |
| トランザクション署名チェック（EVM キー経路） | 約 25,000 CU          | 上記の `secp256k1_recover` syscall。以下の `ecrecover` プリコンパイルと同じコストです |
| 単純な送金                    | 約 50,000〜100,000 CU  | 残高更新のみ                                                          |
| ERC-20 送金                | 約 100,000〜150,000 CU | SPL プリコンパイル呼び出しを含みます                                            |
| コントラクトのデプロイ（小規模）         | 約 200,000〜400,000 CU | バイトコードのサイズに依存します                                                |
| ストレージ書き込み（SSTORE）        | 約 5,000〜20,000 CU    | コールドアクセスかウォームアクセスか                                              |

### プリコンパイル操作

| プリコンパイル         | 概算 CU                            | 注記                                                                                                                                                               |
| --------------- | -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ecrecover       | 約 25,700 CU                      |                                                                                                                                                                  |
| SHA-256         | 32 バイトワードごとに約 257 CU             | さらに小さな固定コストがあります。プログラムは SHA-256、RIPEMD-160、そして identity プリコンパイルを、3 つの中で最もコストの高い RIPEMD-160 に合わせた、ワードごとに約 2,200 CU の共通単価で価格付けしているため、SHA-256 呼び出しは実際の使用量より多く課金されます |
| BN254 ecAdd     | ほぼ 0 CU                          | 32 バイトをコピーするより安価                                                                                                                                                 |
| BN254 ecMul     | 約 4,200 CU                       |                                                                                                                                                                  |
| BN254 ecPairing | 約 22,000 CU に加え、ペアごとに約 12,800 CU | 4 ペアのチェックは約 73,000 CU で、1 つのアトミックトランザクションの範囲内に十分収まります                                                                                                            |

これらの数値はプログラム独自のキャリブレーションに基づいており、テストネット展開で Solana ノードから読み戻したコンピュートユニットを使用しています。このページ用に再計測はしていません。次を使って自分の呼び出しを測定してください `computeUnitsConsumed`.

## 最適化手法

### 1. ホットパスには Yul を使う

Solidity のオプティマイザでも十分なコードは生成されますが、Yul（インラインアセンブリ）を使うと重要な処理の CU を大幅に削減できます:

```solidity
// 変更前: 約 600K CU
function createPairAccount(bytes32 token0, bytes32 token1) external {
    // Solidity レベルの操作
}

// 変更後: 約 150K CU（Yul 最適化）
function createPairAccount(bytes32 token0, bytes32 token1) external {
    assembly {
        // 直接メモリを操作し、ABI エンコードのオーバーヘッドを省略
    }
}
```

### 2. PDA 導出をキャッシュする

～による PDA 導出は `find_program_address` 高コストです。導出済みの PDA は、毎回計算するのではなくコントラクトのストレージに保存してください:

```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. 既知のプログラム ID をハードコードする

ストレージからプログラム ID を読み込まず、定数を使ってください:

```solidity
// 高コスト: ストレージから読み取る
bytes32 splTokenProgram = storage_program_id;

// 低コスト: コンパイル時定数
bytes32 constant SPL_TOKEN_PROGRAM = 0x06ddf6e1d765a193d9cbe146ceeb79ac1cb485ed5f5b37913a8cf5857eff00a9;
```

### 4. アカウント数を最小化する

Solana トランザクション内の各アカウントは CU のオーバーヘッドを追加します。次の方法でアカウント数を減らしてください:

* アカウントを共有する操作をバッチ処理する
* 中間アカウントを少なくする
* 冗長な ATA 作成チェックを避ける

### 5. オプティマイザ設定を使う

```typescript
// hardhat.config.ts — ビルドプロファイルでオプティマイザを有効化
solidity: {
  profiles: {
    default: { version: "0.8.28" },
    production: {
      version: "0.8.28",
      settings: { optimizer: { enabled: true, runs: 200 } },
    },
  },
}
```

以下でビルド `npx hardhat compile --build-profile production`.

## CU 消費の測定

`eth_estimateGas` はコンピュートユニットを測定しません。Rome ではガス 1 単位がコスト 1 ランポートに相当するためです。これはガス上限を決めるだけです。EVM ハッシュの背後にある Solana トランザクションからコンピュートを読み取るには、次を使って署名を取得します `rome_solanaTxForEvmTx` で取得し、Solana の `getTransaction` および `maxSupportedTransactionVersion: 1` を使って各トランザクションを取得し、 `computeUnitsConsumed`、またはエクスプローラーで呼び出しを開いてください。反復呼び出しでは、レッグごとに 1 つのトランザクションがあります。送信前にガス上限を見積もるには:

```bash
cast estimate --rpc-url https://hadrian.testnet.romeprotocol.xyz/ \
  0xCONTRACT "myFunction(uint256)" 42
```

または ethers.js 経由で:

```javascript
const gas = await contract.myFunction.estimateGas(42);
console.log("見積もりガス:", gas.toString());
```

## 次は何か

* [制約](https://docs.rome.builders/ja/ji-ben-gai-nian/constraints.md) — 制限と境界の完全な一覧


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rome.builders/ja/ji-ben-gai-nian/compute-budget.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
