From df5b260772c1515302234fa318356e8a10c3ee3d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 3 Feb 2021 19:58:50 +0300 Subject: [PATCH] refactor: simplify gas_counter --- runtime/near-vm-logic/src/gas_counter.rs | 62 +++++++++--------------- 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/runtime/near-vm-logic/src/gas_counter.rs b/runtime/near-vm-logic/src/gas_counter.rs index 3538444e711..d401969a8e5 100644 --- a/runtime/near-vm-logic/src/gas_counter.rs +++ b/runtime/near-vm-logic/src/gas_counter.rs @@ -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 = ::std::result::Result; @@ -95,56 +90,43 @@ 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") { + 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) }