From 1cb93029cb41d224b1a6c77caf90414ff64e3d47 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 11 Feb 2021 17:29:44 +0300 Subject: [PATCH] refactor: simplify gas_counter (#3899) --- runtime/near-vm-logic/src/gas_counter.rs | 55 ++++++++---------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/runtime/near-vm-logic/src/gas_counter.rs b/runtime/near-vm-logic/src/gas_counter.rs index 6395ae11b81..279d9223ad7 100644 --- a/runtime/near-vm-logic/src/gas_counter.rs +++ b/runtime/near-vm-logic/src/gas_counter.rs @@ -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 = ::std::result::Result; @@ -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) }