> 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/ar/almfahym-alasasyh/compute-budget.md).

# ميزانية الحوسبة

تحتوي كل معاملة على Solana على ميزانية حوسبة تُقاس بوحدات الحوسبة (CU). إن فهم تكاليف CU يساعدك على تصميم عقود Rome أكثر كفاءة.

## نظرة عامة على الميزانية

| الوضع         | الحد الأقصى لـ CU           | ملاحظات                                                    |
| ------------- | --------------------------- | ---------------------------------------------------------- |
| ذرّي (VmAt)   | \~1,400,000 CU              | معاملة Solana واحدة                                        |
| تكراري (VmIt) | غير محدود (متعدد المعاملات) | شيفرات تشغيل/خطوة تكيفية — موزعة وفق ميزانية CU لكل معاملة |

لكل معاملة على Solana ميزانية افتراضية قدرها 200,000 CU، ويمكن توسيعها إلى حوالي 1.4 مليون CU عبر تعليمات ميزانية الحوسبة (تُضاف تلقائيًا بواسطة Rome SDK).

## تقديرات تكلفة CU

### عمليات EVM

| العملية                       | CU تقريبية           | ملاحظات                              |
| ----------------------------- | -------------------- | ------------------------------------ |
| التحقق من التوقيع (ecrecover) | \~5,000 CU           | secp256k1 عبر استدعاء نظام Solana    |
| تحويل بسيط                    | \~50,000-100,000 CU  | تحديثات الرصيد فقط                   |
| تحويل ERC-20                  | \~100,000-150,000 CU | يشمل استدعاء precompile الخاص بـ SPL |
| نشر العقد (صغير)              | \~200,000-400,000 CU | يعتمد على حجم الشفرة البايتية        |
| كتابة إلى التخزين (SSTORE)    | \~5,000-20,000 CU    | الوصول البارد مقابل الدافئ           |

### عمليات ما قبل التجميع

| ما قبل التجميع  | 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    |

## تقنيات التحسين

### 1. استخدم Yul للمسارات الساخنة

ينتج مُحسّن Solidity شيفرة معقولة، لكن Yul (التجميع المضمّن) يمكن أن يقلل CU بشكل كبير للعمليات الحرجة:

```solidity
// قبل: حوالي 600 ألف CU
function createPairAccount(bytes32 token0, bytes32 token1) external {
    // عمليات على مستوى Solidity
}

// بعد: حوالي 150 ألف CU (تحسين Yul)
function createPairAccount(bytes32 token0, bytes32 token1) external {
    assembly {
        // معالجة مباشرة للذاكرة، مع تجاوز كلفة ترميز ABI
    }
}
```

### 2. خزّن اشتقاقات PDA مؤقتًا

اشتقاق PDA عبر `find_program_address` مكلف. خزّن الـ PDAs المشتقة في تخزين العقد بدلًا من حسابها في كل استدعاء:

```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. ثبّت معرّفات البرامج المعروفة

لا تقم بتحميل معرّفات البرامج من التخزين — استخدم ثوابت:

```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` لقياس CU قبل الإرسال:

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

أو عبر ethers.js:

```javascript
const gas = await contract.myFunction.estimateGas(42);
console.log("الغاز المقدّر:", gas.toString());
```

## ما التالي

* [القيود](/ar/almfahym-alasasyh/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 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/ar/almfahym-alasasyh/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.
