Skip to content

Commit

Permalink
nit(runtime): get rid of cells inside profile data (near#4626)
Browse files Browse the repository at this point in the history
As we don't pass profiledata everywhere now, we do not need mutate immutable reference every where, so cell can be dropped

Test Plan
----------
CI
  • Loading branch information
ailisp authored Aug 5, 2021
1 parent 5828c0c commit 9dbffbc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
47 changes: 19 additions & 28 deletions core/primitives-core/src/profile.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::rc::Rc;
use std::{cell::Cell, fmt};
use std::fmt;

use crate::config::{ActionCosts, ExtCosts};
use crate::types::Gas;

/// Data for total gas burnt (index 0), and then each external cost and action
type DataArray = [Cell<u64>; ProfileData::LEN];
type DataArray = [u64; ProfileData::LEN];

/// Profile of gas consumption.
#[derive(Clone, PartialEq, Eq)]
pub struct ProfileData {
data: Rc<DataArray>,
data: DataArray,
}

impl Default for ProfileData {
Expand All @@ -26,39 +25,32 @@ impl ProfileData {

#[inline]
pub fn new() -> Self {
// We must manually promote to a constant for array literal to work.
const ZERO: Cell<u64> = Cell::new(0);

let data = Rc::new([ZERO; ProfileData::LEN]);
let data = [0; ProfileData::LEN];
ProfileData { data }
}

#[inline]
pub fn merge(&self, other: &ProfileData) {
for (dst, src) in self.data.iter().zip(other.data.iter()) {
let updated = dst.get().saturating_add(src.get());
dst.set(updated);
pub fn merge(&mut self, other: &ProfileData) {
for i in 0..ProfileData::LEN {
self.data[i] = self.data[i].saturating_add(other.data[i]);
}
}

#[inline]
pub fn add_action_cost(&self, action: ActionCosts, value: u64) {
pub fn add_action_cost(&mut self, action: ActionCosts, value: u64) {
self.add_val(ProfileData::ACTION_START + action as usize, value);
}
#[inline]
pub fn add_ext_cost(&self, ext: ExtCosts, value: u64) {
pub fn add_ext_cost(&mut self, ext: ExtCosts, value: u64) {
self.add_val(ProfileData::EXT_START + ext as usize, value);
}
#[inline]
fn add_val(&self, index: usize, value: u64) {
let slot = &self.data[index];
let old = slot.get();
slot.set(old.saturating_add(value));
fn add_val(&mut self, index: usize, value: u64) {
self.data[index] = self.data[index].saturating_add(value);
}

fn read(&self, index: usize) -> u64 {
let slot = &self.data[index];
slot.get()
self.data[index]
}

pub fn all_gas(&self) -> Gas {
Expand Down Expand Up @@ -92,9 +84,8 @@ impl ProfileData {
self.all_gas() - self.host_gas() - self.action_gas()
}

pub fn set_burnt_gas(&self, burnt_gas: u64) {
let slot = &self.data[0];
slot.set(burnt_gas)
pub fn set_burnt_gas(&mut self, burnt_gas: u64) {
self.data[0] = burnt_gas;
}
}

Expand Down Expand Up @@ -167,14 +158,14 @@ mod test {
use super::*;
#[test]
fn test_profile_all_gas() {
let profile_data = ProfileData::new();
let mut profile_data = ProfileData::new();
profile_data.set_burnt_gas(42);
assert_eq!(profile_data.all_gas(), 42);
}

#[test]
fn test_profile_data_debug() {
let profile_data = ProfileData::new();
let mut profile_data = ProfileData::new();
profile_data.set_burnt_gas(42);
println!("{:#?}", &profile_data);
}
Expand All @@ -187,7 +178,7 @@ mod test {

#[test]
fn test_no_panic_on_overflow() {
let profile_data = ProfileData::new();
let mut profile_data = ProfileData::new();
profile_data.add_action_cost(ActionCosts::function_call, u64::MAX);
profile_data.add_action_cost(ActionCosts::function_call, u64::MAX);

Expand All @@ -197,12 +188,12 @@ mod test {

#[test]
fn test_merge() {
let profile_data = ProfileData::new();
let mut profile_data = ProfileData::new();
profile_data.add_action_cost(ActionCosts::function_call, 111);
profile_data.add_ext_cost(ExtCosts::storage_read_base, 11);
profile_data.set_burnt_gas(1111);

let profile_data2 = ProfileData::new();
let mut profile_data2 = ProfileData::new();
profile_data2.add_action_cost(ActionCosts::function_call, 222);
profile_data2.add_ext_cost(ExtCosts::storage_read_base, 22);
profile_data2.set_burnt_gas(2222);
Expand Down
6 changes: 3 additions & 3 deletions runtime/near-vm-runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn run_vm(
#[cfg(feature = "wasmer1_vm")]
use crate::wasmer1_runner::run_wasmer1;

let (outcome, error) = match vm_kind {
let (mut outcome, error) = match vm_kind {
#[cfg(feature = "wasmer0_vm")]
VMKind::Wasmer0 => run_wasmer(
code,
Expand Down Expand Up @@ -114,8 +114,8 @@ pub fn run_vm(
#[cfg(not(feature = "wasmer1_vm"))]
VMKind::Wasmer1 => panic!("Wasmer1 is not supported, compile with '--features wasmer1_vm'"),
};
if let Some(outcome) = &outcome {
outcome.profile.set_burnt_gas(outcome.burnt_gas)
if let Some(ref mut outcome) = outcome {
(*outcome).profile.set_burnt_gas(outcome.burnt_gas)
}
(outcome, error)
}
Expand Down

0 comments on commit 9dbffbc

Please sign in to comment.