Skip to content

Commit

Permalink
fix(pool): fix memory size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-miao committed Sep 20, 2023
1 parent 726f107 commit 55e780a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
5 changes: 2 additions & 3 deletions crates/pool/src/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,10 @@ impl PoolOperation {
})
}

/// Compute the amount of memory the PoolOperation takes up.
/// Compute the amount of heap memory the PoolOperation takes up.
pub fn mem_size(&self) -> usize {
std::mem::size_of::<Self>()
+ self.uo.mem_size()
+ std::mem::size_of::<Vec<EntityType>>()
+ self.uo.heap_size()
+ self.entities_needing_stake.len() * std::mem::size_of::<EntityType>()
}

Expand Down
40 changes: 29 additions & 11 deletions crates/pool/src/mempool/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl PoolInner {
.filter(|(bn, _)| *bn < block_number)
{
if let Some((op, _)) = self.mined_at_block_number_by_hash.remove(&hash) {
self.cache_size -= op.size();
self.cache_size -= op.mem_size();
}
self.mined_hashes_with_block_numbers.remove(&(bn, hash));
}
Expand Down Expand Up @@ -268,7 +268,7 @@ impl PoolInner {
let hash = pool_op
.uo()
.op_hash(self.config.entry_point, self.config.chain_id);
self.pool_size += pool_op.size();
self.pool_size += pool_op.mem_size();
self.by_hash.insert(hash, pool_op.clone());
self.by_id.insert(pool_op.uo().id(), pool_op.clone());
self.best.insert(pool_op);
Expand All @@ -293,7 +293,7 @@ impl PoolInner {
self.by_id.remove(&op.uo().id());
self.best.remove(&op);
if let Some(block_number) = block_number {
self.cache_size += op.size();
self.cache_size += op.mem_size();
self.mined_at_block_number_by_hash
.insert(hash, (op.clone(), block_number));
self.mined_hashes_with_block_numbers
Expand All @@ -303,7 +303,7 @@ impl PoolInner {
self.decrement_address_count(e.address);
}

self.pool_size -= op.size();
self.pool_size -= op.mem_size();
Some(op.po)
}

Expand Down Expand Up @@ -361,8 +361,8 @@ impl OrderedPoolOperation {
&self.po.uo
}

fn size(&self) -> usize {
self.po.mem_size()
fn mem_size(&self) -> usize {
std::mem::size_of::<OrderedPoolOperation>() + self.po.mem_size()
}
}

Expand Down Expand Up @@ -664,7 +664,14 @@ mod tests {
}

assert_eq!(pool.address_count(sender), 1);
assert_eq!(pool.pool_size, po1.mem_size());
assert_eq!(
pool.pool_size,
OrderedPoolOperation {
po: Arc::new(po1),
submission_id: 0
}
.mem_size()
);
}

#[test]
Expand All @@ -687,7 +694,14 @@ mod tests {
assert_eq!(pool.address_count(sender), 1);
assert_eq!(pool.address_count(paymaster1), 0);
assert_eq!(pool.address_count(paymaster2), 1);
assert_eq!(pool.pool_size, po2.mem_size());
assert_eq!(
pool.pool_size,
OrderedPoolOperation {
po: Arc::new(po2),
submission_id: 0
}
.mem_size()
);
}

#[test]
Expand All @@ -712,12 +726,16 @@ mod tests {
chain_id: 1,
max_userops_per_sender: 16,
min_replacement_fee_increase_percentage: 10,
max_size_of_pool_bytes: 20 * mem_size_of_op(),
max_size_of_pool_bytes: 20 * mem_size_of_ordered_pool_op(),
}
}

fn mem_size_of_op() -> usize {
create_op(Address::random(), 1, 1).mem_size()
fn mem_size_of_ordered_pool_op() -> usize {
OrderedPoolOperation {
po: Arc::new(create_op(Address::random(), 1, 1)),
submission_id: 1,
}
.mem_size()
}

fn create_op(sender: Address, nonce: usize, max_fee_per_gas: usize) -> PoolOperation {
Expand Down
7 changes: 3 additions & 4 deletions crates/types/src/user_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ impl UserOperation {
+ pad_len(&self.signature)
}

/// Calculates the size of the user operation in memory
pub fn mem_size(&self) -> usize {
std::mem::size_of::<Self>()
+ self.init_code.len()
/// Compute the amount of heap memory the UserOperation takes up.
pub fn heap_size(&self) -> usize {
self.init_code.len()
+ self.call_data.len()
+ self.paymaster_and_data.len()
+ self.signature.len()
Expand Down

0 comments on commit 55e780a

Please sign in to comment.