Skip to content

Commit

Permalink
Use DependentCost for aloc opcode (#1934)
Browse files Browse the repository at this point in the history
PR upgrades the `fuel-vm` to `0.52.0`. A new VM changes `aloc` opcode to
be `DependentCost`. It requires updated benchmarks.

The PR also updates default gas costs according to a new Vultr instance
we use to do benchmarks. It works faster than the previous one, so all
opcodes became cheaper.

The `vm_initialziation` became much cheaper and now has reasonable
values. The `bloc` opcode became expensive and depended on a number of
allocated bytes.

## Checklist
- [x] Breaking changes are clearly marked as such in the PR description
and changelog

### Before requesting review
- [x] I have reviewed the code myself

### After merging, notify other teams

- [x] Both SDKs

---------

Co-authored-by: Hannes Karppila <hannes.karppila@gmail.com>
Co-authored-by: Mitchell Turner <james.mitchell.turner@gmail.com>
  • Loading branch information
3 people committed Jun 6, 2024
1 parent d55088b commit 4986d4d
Show file tree
Hide file tree
Showing 23 changed files with 726 additions and 694 deletions.
11 changes: 5 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Changed
- [#1934](https://github.com/FuelLabs/fuel-core/pull/1934): Updated benchmark for the `aloc` opcode to be `DependentCost`. Updated `vm_initialization` benchmark to exclude growing of memory(It is handled by VM reuse).
- [#1916](https://github.com/FuelLabs/fuel-core/pull/1916): Speed up synchronisation of the blocks for the `fuel-core-sync` service.
- [#1888](https://github.com/FuelLabs/fuel-core/pull/1888): optimization: Reuse VM memory across executions.

#### Breaking

- [#1934](https://github.com/FuelLabs/fuel-core/pull/1934): Changed `GasCosts` endpoint to return `DependentCost` for the `aloc` opcode via `alocDependentCost`.
- [#1934](https://github.com/FuelLabs/fuel-core/pull/1934): Updated default gas costs for the local testnet configuration. All opcodes became cheaper.
- [#1924](https://github.com/FuelLabs/fuel-core/pull/1924): `dry_run_opt` has new `gas_price: Option<u64>` argument
- [#1888](https://github.com/FuelLabs/fuel-core/pull/1888): Upgraded `fuel-vm` to `0.51.0`. See [release](https://github.com/FuelLabs/fuel-vm/releases/tag/v0.51.0) for more information.

### Added
- [#1939](https://github.com/FuelLabs/fuel-core/pull/1939): Added API functions to open a RocksDB in different modes.
Expand All @@ -20,12 +25,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Removed
- [#1913](https://github.com/FuelLabs/fuel-core/pull/1913): Removed dead code from the project.

### Changed
- [#1888](https://github.com/FuelLabs/fuel-core/pull/1888): optimization: Reuse VM memory across executions.

#### Breaking
- [#1888](https://github.com/FuelLabs/fuel-core/pull/1888): Upgraded `fuel-vm` to `0.51.0`. See [release](https://github.com/FuelLabs/fuel-vm/releases/tag/v0.51.0) for more information.

### Fixed
- [#1921](https://github.com/FuelLabs/fuel-core/pull/1921): Fixed unstable `gossipsub_broadcast_tx_with_accept` test.
- [#1915](https://github.com/FuelLabs/fuel-core/pull/1915): Fixed reconnection issue in the dev cluster with AWS cluster.
Expand Down
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fuel-core-wasm-executor = { version = "0.27.0", path = "./crates/services/upgrad
fuel-core-xtask = { version = "0.0.0", path = "./xtask" }

# Fuel dependencies
fuel-vm-private = { version = "0.51.0", package = "fuel-vm", default-features = false }
fuel-vm-private = { version = "0.52.0", package = "fuel-vm", default-features = false }

# Common dependencies
anyhow = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion benches/benches-outputs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fuel_core_types::fuel_tx::{
consensus_parameters::gas::GasCostsValuesV1,
consensus_parameters::gas::GasCostsValuesV2,
DependentCost,
GasCostsValues,
};
Expand Down
22 changes: 21 additions & 1 deletion benches/benches/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use core::iter::successors;
use ethnum::U256;
use fuel_core_types::{
Expand Down Expand Up @@ -80,7 +82,6 @@ pub fn set_full_word(r: RegisterId, v: Word) -> Vec<Instruction> {
const BENCH_RECEIPTS: usize = (u16::MAX - 4) as usize;

/// Testing receipt context
#[allow(dead_code)] // Unsure why this is needed, as the code is used
pub fn make_receipts(rng: &mut StdRng) -> ReceiptsCtx {
let mut ctx = ReceiptsCtx::default();
for _ in 0..BENCH_RECEIPTS {
Expand All @@ -97,3 +98,22 @@ pub fn make_receipts(rng: &mut StdRng) -> ReceiptsCtx {
}
ctx
}

const LAST_VALUE: u64 = 100_000;

pub fn linear_short() -> Vec<u64> {
let mut linear_short = vec![1, 10, 100, 1000, 10_000];
linear_short.push(LAST_VALUE);
linear_short
}

pub fn linear() -> Vec<u64> {
let mut linear: Vec<u64> = vec![1, 10, 100, 1000, 10_000];
let mut l = successors(Some(LAST_VALUE as f64), |n| Some(n / 1.5))
.take(5)
.map(|f| f as u64)
.collect::<Vec<_>>();
l.sort_unstable();
linear.extend(l);
linear
}
62 changes: 5 additions & 57 deletions benches/benches/vm_initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use criterion::{
Criterion,
Throughput,
};
use fuel_core_storage::InterpreterStorage;
use fuel_core_types::{
fuel_asm::{
op,
Expand All @@ -23,17 +22,8 @@ use fuel_core_types::{
checked_transaction::{
Checked,
IntoChecked,
Ready,
},
constraints::reg_key::{
Reg,
RegMut,
},
consts::VM_MAX_RAM,
interpreter::{
MemoryInstance,
NotSupportedEcal,
},
interpreter::NotSupportedEcal,
Interpreter,
},
};
Expand Down Expand Up @@ -110,56 +100,14 @@ pub fn vm_initialization(c: &mut Criterion) {
group.throughput(Throughput::Bytes(tx_size as u64));
group.bench_function(name, |b| {
b.iter(|| {
unoptimized_vm_initialization_with_allocating_full_range_of_memory(
&mut vm, &tx,
#[allow(clippy::unit_arg)]
black_box(
vm.init_script(tx.clone())
.expect("Should be able to execute transaction"),
);
})
});
}

group.finish();
}

#[allow(clippy::unit_arg)]
fn unoptimized_vm_initialization_with_allocating_full_range_of_memory<S>(
vm: &mut Interpreter<MemoryInstance, S, Script>,
ready_tx: &Ready<Script>,
) where
S: InterpreterStorage,
{
black_box(initialize_vm_with_allocated_full_range_of_memory(
black_box(ready_tx.clone()),
vm,
));
}

fn initialize_vm_with_allocated_full_range_of_memory<S>(
ready_tx: Ready<Script>,
vm: &mut Interpreter<MemoryInstance, S, Script>,
) where
S: InterpreterStorage,
{
vm.init_script(ready_tx)
.expect("Should be able to execute transaction");

const POWER_OF_TWO_OF_HALF_VM: u64 = 25;
const VM_MEM_HALF: u64 = 1 << POWER_OF_TWO_OF_HALF_VM;
assert_eq!(VM_MEM_HALF, VM_MAX_RAM / 2);
let mut hp = VM_MAX_RAM;

for i in 0..=POWER_OF_TWO_OF_HALF_VM {
let stack = 1 << i;
let heap = stack / 2;

vm.memory_mut()
.grow_stack(stack)
.expect("Should be able to grow stack");
vm.memory_mut()
.grow_heap_by(Reg::new(&0), RegMut::new(&mut hp), heap)
.expect("Should be able to grow heap");
}

vm.memory_mut()
.grow_heap_by(Reg::new(&0), RegMut::new(&mut hp), VM_MEM_HALF)
.expect("Should be able to grow heap");
}
34 changes: 27 additions & 7 deletions benches/benches/vm_set/alu.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use super::run_group_ref;

use criterion::Criterion;
use criterion::{
Criterion,
Throughput,
};
use ethnum::U256;
use fuel_core_benches::*;
use fuel_core_types::fuel_asm::*;

use crate::utils::{
linear_short,
set_full_word,
};
use fuel_core_types::fuel_asm::wideint::{
CompareArgs,
CompareMode,
Expand All @@ -20,6 +27,8 @@ use super::utils::{
};

pub fn run(c: &mut Criterion) {
let linear_short = linear_short();

run_group_ref(
&mut c.benchmark_group("add"),
"add",
Expand All @@ -34,12 +43,23 @@ pub fn run(c: &mut Criterion) {
.with_prepare_script(vec![op::movi(0x11, 100000)]),
);

run_group_ref(
&mut c.benchmark_group("aloc"),
"aloc",
VmBench::new(op::aloc(0x10))
.with_prepare_script(vec![op::movi(0x10, (1 << 18) - 1)]),
);
// aloc
{
let mut aloc = c.benchmark_group("aloc");

let mut aloc_linear = linear_short.clone();
aloc_linear.push(1_000_000);
aloc_linear.push(10_000_000);
aloc_linear.push(30_000_000);
for i in aloc_linear {
let bench =
VmBench::new(op::aloc(0x10)).with_prepare_script(set_full_word(0x10, i));
aloc.throughput(Throughput::Bytes(i));
run_group_ref(&mut aloc, format!("{i}"), bench);
}

aloc.finish();
}

run_group_ref(
&mut c.benchmark_group("and"),
Expand Down
25 changes: 9 additions & 16 deletions benches/benches/vm_set/blockchain.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{
iter::successors,
sync::Arc,
};
use std::sync::Arc;

use crate::utils::make_receipts;
use crate::utils::{
linear,
linear_short,
make_receipts,
};

use super::run_group_ref;

Expand Down Expand Up @@ -151,16 +152,8 @@ impl BenchDb {
pub fn run(c: &mut Criterion) {
let rng = &mut StdRng::seed_from_u64(2322u64);

const LAST_VALUE: u64 = 100_000;
let mut linear: Vec<u64> = vec![1, 10, 100, 1000, 10_000];
let mut linear_short = linear.clone();
linear_short.push(LAST_VALUE);
let mut l = successors(Some(LAST_VALUE as f64), |n| Some(n / 1.5))
.take(5)
.map(|f| f as u64)
.collect::<Vec<_>>();
l.sort_unstable();
linear.extend(l);
let linear_short = linear_short();
let linear = linear();

let asset = AssetId::zeroed();
let contract: ContractId = VmBench::CONTRACT;
Expand Down Expand Up @@ -287,7 +280,7 @@ pub fn run(c: &mut Criterion) {
let mut call = c.benchmark_group("call");

for i in linear.clone() {
let mut code = vec![0u8; i as usize];
let mut code = vec![123u8; i as usize];

rng.fill_bytes(&mut code);

Expand Down
6 changes: 3 additions & 3 deletions benches/src/bin/collect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use fuel_core_types::fuel_tx::{
consensus_parameters::gas::GasCostsValuesV1,
consensus_parameters::gas::GasCostsValuesV2,
ConsensusParameters,
GasCosts,
};
Expand Down Expand Up @@ -371,7 +371,7 @@ pub const GIT: &str = ""#,
r#"";"#,
r##"
pub fn default_gas_costs() -> GasCostsValues {
GasCostsValuesV1 {"##,
GasCostsValuesV2 {"##,
r##" }.into()
}
"##,
Expand Down Expand Up @@ -495,7 +495,7 @@ impl State {
)
}

fn to_gas_costs(&self) -> GasCostsValuesV1 {
fn to_gas_costs(&self) -> GasCostsValuesV2 {
serde_yaml::from_value(self.to_yaml()).unwrap()
}

Expand Down
Loading

0 comments on commit 4986d4d

Please sign in to comment.