Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory pool/reuse #732

Merged
merged 27 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1e205b6
Add memory reset function
Dentosal May 8, 2024
ebc60c6
Add a test case
Dentosal May 8, 2024
39fa053
Add changelog entry
Dentosal May 8, 2024
ab936e6
Add VmPool and use it for predicates and execution
Dentosal May 10, 2024
1fafd09
Add no_std support to pool
Dentosal May 10, 2024
f48b99d
Fix heap reallocation optimization on reset
Dentosal May 13, 2024
aca6a88
Move memory impl into a trait
Dentosal May 13, 2024
fc4a17e
Refactor Interpreter to allow using &mut Memory as well
Dentosal May 13, 2024
d7d1a54
Change grow_heap to grow_heap_by
Dentosal May 13, 2024
e82441b
Change the Interpreter memory to a generic
Dentosal May 13, 2024
41542f5
Cleanup
Dentosal May 13, 2024
fae708a
Feature-gating fix
Dentosal May 13, 2024
80a0616
Re-add interpreter clone for tests
Dentosal May 13, 2024
a29e3f7
Address pr feedback
Dentosal May 14, 2024
3f1a334
Cleanup
Dentosal May 14, 2024
452d439
Cleanup
Dentosal May 14, 2024
c8a7edf
Make MemoryPool to have a concrete return type. Improve docs.
Dentosal May 14, 2024
e2aa95c
Merge branch 'master' into dento/memory-reset
Dentosal May 19, 2024
6bbb741
Clippy
Dentosal May 20, 2024
ea78818
Use `Memory` trait.
xgreenx May 31, 2024
6667f0a
Simplified `VmMemoryPool` constrains
xgreenx May 31, 2024
5faa1ac
Merge branch 'refs/heads/master' into dento/memory-reset
xgreenx May 31, 2024
65f1b10
Simplify API
xgreenx May 31, 2024
0781848
Make clippy happy
xgreenx May 31, 2024
ac78892
Removed redundant addition
xgreenx May 31, 2024
694721e
Updated CHANGELOG.md
xgreenx May 31, 2024
7197ed9
Unify the signature
xgreenx May 31, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- [#732](https://github.com/FuelLabs/fuel-vm/pull/732): Adds `reset` method to VM memory.

#### Breaking

- [#725](https://github.com/FuelLabs/fuel-vm/pull/725): `UtxoId::from_str` now rejects inputs with multiple `0x` prefixes. Many `::from_str` implementations also reject extra data in the end of the input, instead of silently ignoring it. `UtxoId::from_str` allows a single `:` between the fields. Unused `GasUnit` struct removed.
- [#726](https://github.com/FuelLabs/fuel-vm/pull/726): Removed code related to Binary Merkle Sum Trees (BMSTs). The BMST is deprecated and not used in production environments.
- [#732](https://github.com/FuelLabs/fuel-vm/pull/732): Makes the VM generic over the memory type, allowing to use `VmPool` to reuse relatively expensive-to-allocate VM memories. Functions and traits which require VM initalization such as `estimate_predicates` now take the `VmPool` as an argument. Removes `Clone` from the VM to prevent accidental allocations. The `Interpterter::eq` method now only compares accessible memory regions.


## [Version 0.49.0]

Expand Down
5 changes: 1 addition & 4 deletions fuel-types/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use core::{
};

/// Formatting utility to truncate a vector of bytes to a hex string of max length `N`
pub fn fmt_truncated_hex<const N: usize>(
data: &Vec<u8>,
f: &mut Formatter,
) -> fmt::Result {
pub fn fmt_truncated_hex<const N: usize>(data: &[u8], f: &mut Formatter) -> fmt::Result {
let formatted = if data.len() > N {
let mut s = hex::encode(&data[0..N.saturating_sub(3)]);
s.push_str("...");
Expand Down
1 change: 1 addition & 0 deletions fuel-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ rand = { version = "0.8", optional = true }
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
serde_with = { version = "3.7", optional = true }
sha3 = { version = "0.10", default-features = false }
spin = "0.9"
static_assertions = "1.1"
strum = { version = "0.24", features = ["derive"], default-features = false }
tai64 = { version = "4.0", default-features = false }
Expand Down
39 changes: 24 additions & 15 deletions fuel-vm/examples/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ use fuel_tx::{
};
use fuel_vm::{
error::SimpleResult,
interpreter::EcalHandler,
interpreter::{
EcalHandler,
Memory,
},
prelude::{
Interpreter,
IntoChecked,
Expand All @@ -46,13 +49,16 @@ use fuel_vm::{
pub struct FileReadEcal;

impl EcalHandler for FileReadEcal {
fn ecal<S, Tx>(
vm: &mut Interpreter<S, Tx, Self>,
fn ecal<M, S, Tx>(
vm: &mut Interpreter<M, S, Tx, Self>,
a: RegId,
b: RegId,
c: RegId,
d: RegId,
) -> SimpleResult<()> {
) -> SimpleResult<()>
where
M: AsRef<Memory> + AsMut<Memory>,
{
let a = vm.registers()[a]; // Seek offset
let b = vm.registers()[b]; // Read length
let c = vm.registers()[c]; // File path pointer in vm memory
Expand Down Expand Up @@ -81,7 +87,7 @@ impl EcalHandler for FileReadEcal {
}

fn example_file_read() {
let vm: Interpreter<MemoryStorage, Script, FileReadEcal> =
let vm: Interpreter<_, MemoryStorage, Script, FileReadEcal> =
Interpreter::with_memory_storage();

let script_data: Vec<u8> = file!().bytes().collect();
Expand All @@ -104,7 +110,7 @@ fn example_file_read() {
.maturity(Default::default())
.add_random_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.into_checked(Default::default(), &consensus_params, Memory::new())
.expect("failed to generate a checked tx");
client.transact(tx);
let receipts = client.receipts().expect("Expected receipts");
Expand All @@ -124,13 +130,16 @@ pub struct CounterEcal {
}

impl EcalHandler for CounterEcal {
fn ecal<S, Tx>(
vm: &mut Interpreter<S, Tx, Self>,
fn ecal<M, S, Tx>(
vm: &mut Interpreter<M, S, Tx, Self>,
a: RegId,
_b: RegId,
_c: RegId,
_d: RegId,
) -> SimpleResult<()> {
) -> SimpleResult<()>
where
M: AsRef<Memory> + AsMut<Memory>,
{
vm.registers_mut()[a] = vm.ecal_state().counter;
vm.ecal_state_mut().counter += 1;
vm.gas_charge(1)?;
Expand All @@ -139,7 +148,7 @@ impl EcalHandler for CounterEcal {
}

fn example_counter() {
let mut vm: Interpreter<MemoryStorage, Script, CounterEcal> =
let mut vm: Interpreter<_, MemoryStorage, Script, CounterEcal> =
Interpreter::with_memory_storage();

vm.ecal_state_mut().counter = 5;
Expand All @@ -163,7 +172,7 @@ fn example_counter() {
.maturity(Default::default())
.add_random_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.into_checked(Default::default(), &consensus_params, Memory::new())
.expect("failed to generate a checked tx");
client.transact(tx);
let receipts = client.receipts().expect("Expected receipts");
Expand All @@ -184,8 +193,8 @@ pub struct SharedCounterEcal {
}

impl EcalHandler for SharedCounterEcal {
fn ecal<S, Tx>(
vm: &mut Interpreter<S, Tx, Self>,
fn ecal<M, S, Tx>(
vm: &mut Interpreter<M, S, Tx, Self>,
a: RegId,
_b: RegId,
_c: RegId,
Expand All @@ -202,7 +211,7 @@ impl EcalHandler for SharedCounterEcal {
}

fn example_shared_counter() {
let vm: Interpreter<MemoryStorage, Script, SharedCounterEcal> =
let vm: Interpreter<_, MemoryStorage, Script, SharedCounterEcal> =
Interpreter::with_memory_storage_and_ecal(SharedCounterEcal {
counter: Arc::new(Mutex::new(5)),
});
Expand All @@ -226,7 +235,7 @@ fn example_shared_counter() {
.maturity(Default::default())
.add_random_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.into_checked(Default::default(), &consensus_params, Memory::new())
.expect("failed to generate a checked tx");
client.transact(tx);
let receipts = client.receipts().expect("Expected receipts");
Expand Down
14 changes: 9 additions & 5 deletions fuel-vm/examples/single_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,25 @@ use fuel_tx::{
use fuel_vm::{
interpreter::{
Interpreter,
Memory,
NotSupportedEcal,
},
prelude::*,
};

fn get_next_instruction<S, Tx>(
vm: &Interpreter<S, Tx, NotSupportedEcal>,
) -> Option<Instruction> {
fn get_next_instruction<M, S, Tx>(
vm: &Interpreter<M, S, Tx, NotSupportedEcal>,
) -> Option<Instruction>
where
M: AsRef<Memory>,
{
let pc = vm.registers()[RegId::PC];
let instruction = RawInstruction::from_be_bytes(vm.memory().read_bytes(pc).ok()?);
Instruction::try_from(instruction).ok()
}

fn main() {
let mut vm = Interpreter::<_, _, NotSupportedEcal>::with_memory_storage();
let mut vm = Interpreter::<_, _, _, NotSupportedEcal>::with_memory_storage();
vm.set_single_stepping(true);

let script_data: Vec<u8> = file!().bytes().collect();
Expand All @@ -46,7 +50,7 @@ fn main() {
.maturity(Default::default())
.add_random_fee_input()
.finalize()
.into_checked(Default::default(), &consensus_params)
.into_checked(Default::default(), &consensus_params, Memory::new())
.expect("failed to generate a checked tx")
.into_ready(
0,
Expand Down
9 changes: 6 additions & 3 deletions fuel-vm/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ impl Backtrace {
/// Create a backtrace from a vm instance and instruction result.
///
/// This isn't copy-free and shouldn't be provided by default.
pub fn from_vm_error<S, Tx, Ecal>(
vm: &Interpreter<S, Tx, Ecal>,
pub fn from_vm_error<M, S, Tx, Ecal>(
vm: &Interpreter<M, S, Tx, Ecal>,
result: ScriptExecutionResult,
) -> Self {
) -> Self
where
M: AsRef<Memory>,
{
let call_stack = vm.call_stack().to_owned();
let contract = vm.internal_contract().unwrap_or_default();
let memory = vm.memory().clone();
Expand Down
Loading
Loading