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 all 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

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

#### Breaking

- [#732](https://github.com/FuelLabs/fuel-vm/pull/732): Makes the VM generic over the memory type, allowing reuse of relatively expensive-to-allocate VM memories through `VmMemoryPool`. Functions and traits which require VM initalization such as `estimate_predicates` now take either the memory or `VmMemoryPool` as an argument. The `Interpterter::eq` method now only compares accessible memory regions. `Memory` was renamed into `MemoryInstance` and `Memory` is a trait now.


## [Version 0.50.0]

### Changed
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
33 changes: 21 additions & 12 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: 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 Down Expand Up @@ -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: 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 Down Expand Up @@ -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 Down
12 changes: 8 additions & 4 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: 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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cc e5df04baab6c550ca2e411ddfc747461f4b88bb972606280ea9c9358eb48c4b2
20 changes: 13 additions & 7 deletions fuel-vm/src/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use crate::{
};
use derivative::Derivative;

use crate::interpreter::Memory;
use crate::interpreter::{
Memory,
MemoryInstance,
};
use fuel_tx::ScriptExecutionResult;
use fuel_types::{
ContractId,
Expand All @@ -31,7 +34,7 @@ pub struct Backtrace {
call_stack: Vec<CallFrame>,
contract: ContractId,
registers: [Word; VM_REGISTER_COUNT],
memory: Memory,
memory: MemoryInstance,
result: ScriptExecutionResult,
initial_balances: InitialBalances,
}
Expand All @@ -40,10 +43,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: Memory,
{
let call_stack = vm.call_stack().to_owned();
let contract = vm.internal_contract().unwrap_or_default();
let memory = vm.memory().clone();
Expand Down Expand Up @@ -78,7 +84,7 @@ impl Backtrace {
}

/// Memory of the VM when the error occurred.
pub fn memory(&self) -> &Memory {
pub fn memory(&self) -> &MemoryInstance {
&self.memory
}

Expand All @@ -99,7 +105,7 @@ impl Backtrace {
Vec<CallFrame>,
ContractId,
[Word; VM_REGISTER_COUNT],
Memory,
MemoryInstance,
ScriptExecutionResult,
InitialBalances,
) {
Expand Down
Loading
Loading