Skip to content

Commit

Permalink
refactor: simplify gas_counter (near#3899)
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad authored Feb 11, 2021
1 parent 3a1a9de commit 1cb9302
Showing 1 changed file with 18 additions and 37 deletions.
55 changes: 18 additions & 37 deletions runtime/near-vm-logic/src/gas_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ thread_local! {

#[cfg(all(feature = "costs_counting", feature = "protocol_feature_evm"))]
pub fn reset_evm_gas_counter() -> u64 {
let mut ret = 0;
EVM_GAS_COUNTER.with(|f| {
ret = *f.borrow();
*f.borrow_mut() = 0;
});
ret
EVM_GAS_COUNTER.with(|f| f.replace(0))
}

type Result<T> = ::std::result::Result<T, VMLogicError>;
Expand Down Expand Up @@ -99,56 +94,42 @@ impl GasCounter {
}
}

#[cfg(all(feature = "costs_counting", feature = "protocol_feature_evm"))]
#[cfg(feature = "protocol_feature_evm")]
#[inline]
pub fn inc_evm_gas_counter(&mut self, value: EvmGas) {
EVM_GAS_COUNTER.with(|f| {
*f.borrow_mut() += value;
})
if cfg!(feature = "costs_counting") {
EVM_GAS_COUNTER.with(|f| {
*f.borrow_mut() += value;
})
}
}

#[cfg(all(not(feature = "costs_counting"), feature = "protocol_feature_evm"))]
#[inline]
pub fn inc_evm_gas_counter(&mut self, _value: EvmGas) {}

#[cfg(feature = "costs_counting")]
#[inline]
fn inc_ext_costs_counter(&mut self, cost: ExtCosts, value: u64) {
#[cfg(feature = "costs_counting")]
EXT_COSTS_COUNTER.with(|f| {
*f.borrow_mut().entry(cost).or_default() += value;
});
}

#[cfg(not(feature = "costs_counting"))]
#[inline]
fn inc_ext_costs_counter(&mut self, _cost: ExtCosts, _value: u64) {}

#[cfg(feature = "costs_counting")]
#[inline]
fn update_profile_host(&mut self, cost: ExtCosts, value: u64) {
match &self.profile {
Some(profile) => profile.add_ext_cost(cost, value),
None => {}
};
if cfg!(feature = "costs_counting") {
if let Some(profile) = &self.profile {
profile.add_ext_cost(cost, value)
}
}
}

#[cfg(not(feature = "costs_counting"))]
#[inline]
fn update_profile_host(&mut self, _cost: ExtCosts, _value: u64) {}

#[cfg(feature = "costs_counting")]
#[inline]
fn update_profile_action(&mut self, action: ActionCosts, value: u64) {
match &self.profile {
Some(profile) => profile.add_action_cost(action, value),
None => {}
};
if cfg!(feature = "costs_counting") {
if let Some(profile) = &self.profile {
profile.add_action_cost(action, value)
}
}
}

#[cfg(not(feature = "costs_counting"))]
#[inline]
fn update_profile_action(&mut self, _action: ActionCosts, _value: u64) {}

pub fn pay_wasm_gas(&mut self, value: u64) -> Result<()> {
self.deduct_gas(value, value)
}
Expand Down

0 comments on commit 1cb9302

Please sign in to comment.