0xMiden / 0xMiden/protocol

perf(protocol): eliminate temporary heap allocation and double serialization in AccountCode::get_size_hint

オープン
#3,629 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Rust
スター
132
フォーク
167
平均マージ
1日 23時間
マージ済み PR(30日)
110

説明

### Problem Description

In `crates/miden-protocol/src/account/code/mod.rs` (lines 301–315), `AccountCode::get_size_hint` is implemented by fully serializing the internal `MastForest` into a temporary `Vec` on the heap simply to measure its byte length:

```rust
fn get_size_hint(&self) -> usize {
// TODO: Replace with proper calculation.
let mut mast_forest_target = Vec::new();
self.mast.write_into(&mut mast_forest_target);

// Size of the serialized procedures length.
let u8_size = 0u8.get_size_hint();
let mut size = u8_size + mast_forest_target.len();

for procedure in self.procedures() {
size += procedure.get_size_hint();
}

size
}
```

When `AccountCode::to_bytes()` is called:
1. `Serializable::to_bytes(&self)` calls `self.get_size_hint()` to pre-allocate capacity in the destination vector.
2. `get_size_hint()` allocates a fresh heap buffer, serializes the entire `MastForest`, reads its length, and immediately drops the allocation.
3. `to_bytes` then calls `self.write_into(&mut target)`, serializing the exact same `MastForest` a second time.

This causes redundant heap allocations and duplicate serialization overhead on every `AccountCode` serialization.

### Proposed Solution
- Provide an accurate and non-allocating size estimation for `AccountCode::get_size_hint` based on the procedure count and component structure, avoiding the temporary heap allocation and double serialization.
- Add unit tests verifying `get_size_hint` accuracy against actual `to_bytes().len()`.

I would like to work on this issue.

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。