Skip to content

Commit

Permalink
Optimize gas cost subtraction using signed integer check
Browse files Browse the repository at this point in the history
- Switched to i64 for gas computation to simplify OOG (Out of Gas) checks.
- Replaced `overflowing_sub` with direct subtraction and a signed check.
  • Loading branch information
Ayushdubey86 authored Feb 8, 2025
1 parent a492073 commit 6650090
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions crates/interpreter/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ impl Gas {
#[inline]
#[must_use = "prefer using `gas!` instead to return an out-of-gas error on failure"]
pub fn record_cost(&mut self, cost: u64) -> bool {
let (remaining, overflow) = self.remaining.overflowing_sub(cost);
let success = !overflow;
if success {
self.remaining = remaining;
let remaining = self.remaining as i64 - cost as i64;
if remaining < 0 {
return false; // OOG
}
success
self.remaining = remaining as u64;
true
}

/// Record memory expansion
Expand Down

0 comments on commit 6650090

Please sign in to comment.