Skip to content

Commit

Permalink
refactor: simplify gas_counter
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Feb 3, 2021
1 parent 91bba07 commit fa4ca2f
Showing 1 changed file with 24 additions and 40 deletions.
64 changes: 24 additions & 40 deletions runtime/near-vm-logic/src/gas_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,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 @@ -95,56 +90,45 @@ 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!(all(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) {
EXT_COSTS_COUNTER.with(|f| {
*f.borrow_mut().entry(cost).or_default() += value;
});
if 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") {
match &self.profile {
Some(profile) => profile.add_ext_cost(cost, value),
None => {}
}
}
}

#[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") {
match &self.profile {
Some(profile) => profile.add_action_cost(action, value),
None => {}
}
}
}

#[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 fa4ca2f

Please sign in to comment.