Skip to content

Commit de35fca

Browse files
AurelienFTxgreenx
andauthored
Splitted big main run function into small (#904)
* Splitted big main run function into small * Re-add test-helpers feature for stack raw * Minimize number of changes Applied comments from PR * Missed changes * Updated CHANGELOG.md --------- Co-authored-by: green <[email protected]>
1 parent f8fe20c commit de35fca

File tree

17 files changed

+2673
-953
lines changed

17 files changed

+2673
-953
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1818
- [907](https://github.com/FuelLabs/fuel-vm/pull/907): `StorageRead` and `StorageWrite` traits no longer return the number of bytes written. They already required that the whole buffer is used, but now this is reflected in signature and documentation as well.
1919
- [914](https://github.com/FuelLabs/fuel-vm/pull/914): The built-in profiler is removed. Use the debugger with single-stepping instead.
2020

21+
### Changed
22+
- [904](https://github.com/FuelLabs/fuel-vm/pull/904): Moved the logic of each opcode into its own function. It helps so reduce the size of the `instruction_inner` function, allowing compiler to do better optimizations. The Mira swaps receive performance improvement in 16.5%.
23+
2124
### Fixed
2225
- [895](https://github.com/FuelLabs/fuel-vm/pull/895): Fix elided lifetimes compilation warnings that became errors after the release of rust 1.83.0.
2326
- [895](https://github.com/FuelLabs/fuel-vm/pull/895): Bump proptest-derive to version `0.5.1` to fix non-local impl errors on the derivation of `proptest_derive::Arbitrary` introduced by rust 1.83.0.

fuel-asm/src/macros.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,17 +1233,21 @@ macro_rules! impl_instructions {
12331233
impl core::convert::TryFrom<[u8; 4]> for Instruction {
12341234
type Error = InvalidOpcode;
12351235
fn try_from([op, a, b, c]: [u8; 4]) -> Result<Self, Self::Error> {
1236-
match Opcode::try_from(op)? {
1236+
let op = match op {
12371237
$(
1238-
Opcode::$Op => Ok(Self::$Op({
1238+
$ix => {
12391239
let op = op::$Op([a, b, c]);
12401240
if !op.reserved_part_is_zero() {
12411241
return Err(InvalidOpcode);
12421242
}
1243-
op
1244-
})),
1243+
1244+
Self::$Op(op)
1245+
},
12451246
)*
1246-
}
1247+
_ => return Err(InvalidOpcode),
1248+
};
1249+
1250+
Ok(op)
12471251
}
12481252
}
12491253

fuel-vm/src/constraints/reg_key.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use core::ops::{
1010
use fuel_asm::{
1111
PanicReason,
1212
RegId,
13-
RegisterId,
1413
Word,
1514
};
1615

@@ -34,15 +33,19 @@ pub struct Reg<'r, const INDEX: u8>(&'r Word);
3433
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
3534
/// A key to a writable register that is within
3635
/// the bounds of the writable registers.
37-
pub struct WriteRegKey(usize);
36+
pub struct WriteRegKey(RegId);
3837

3938
impl WriteRegKey {
4039
/// Create a new writable register key if the index is within the bounds
4140
/// of the writable registers.
42-
pub fn new(k: impl Into<usize>) -> Result<Self, PanicReason> {
41+
pub fn new(k: impl Into<RegId>) -> Result<Self, PanicReason> {
4342
let k = k.into();
44-
is_register_writable(&k)?;
45-
Ok(Self(k))
43+
44+
if k >= RegId::WRITABLE {
45+
Ok(Self(k))
46+
} else {
47+
Err(PanicReason::ReservedRegisterNotWritable)
48+
}
4649
}
4750

4851
/// Translate this key from an absolute register index
@@ -51,19 +54,7 @@ impl WriteRegKey {
5154
/// This subtracts the number of system registers from the key.
5255
#[allow(clippy::arithmetic_side_effects)] // Safety: checked in constructor
5356
fn translate(self) -> usize {
54-
self.0 - VM_REGISTER_SYSTEM_COUNT
55-
}
56-
}
57-
58-
/// Check that the register is above the system registers and below the total
59-
/// number of registers.
60-
pub(crate) fn is_register_writable(r: &RegisterId) -> Result<(), PanicReason> {
61-
const W_USIZE: usize = RegId::WRITABLE.to_u8() as usize;
62-
const RANGE: core::ops::Range<usize> = W_USIZE..(W_USIZE + VM_REGISTER_PROGRAM_COUNT);
63-
if RANGE.contains(r) {
64-
Ok(())
65-
} else {
66-
Err(PanicReason::ReservedRegisterNotWritable)
57+
self.0.to_u8() as usize - VM_REGISTER_SYSTEM_COUNT
6758
}
6859
}
6960

@@ -368,10 +359,10 @@ impl<'a> From<ProgramRegisters<'a>> for ProgramRegistersRef<'a> {
368359
}
369360
}
370361

371-
impl TryFrom<RegisterId> for WriteRegKey {
362+
impl TryFrom<RegId> for WriteRegKey {
372363
type Error = PanicReason;
373364

374-
fn try_from(r: RegisterId) -> Result<Self, Self::Error> {
365+
fn try_from(r: RegId) -> Result<Self, Self::Error> {
375366
Self::new(r)
376367
}
377368
}

fuel-vm/src/constraints/reg_key/tests.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ fn can_split_writes(a: usize, b: usize) -> Option<(Word, Word)> {
8282
.unwrap();
8383
let s = VM_REGISTER_SYSTEM_COUNT;
8484
let mut reg = ProgramRegisters(&mut reg);
85-
reg.get_mut_two(WriteRegKey(s + a), WriteRegKey(s + b))
86-
.map(|(a, b)| (*a, *b))
85+
reg.get_mut_two(
86+
WriteRegKey(RegId::new((s + a) as u8)),
87+
WriteRegKey(RegId::new((s + b) as u8)),
88+
)
89+
.map(|(a, b)| (*a, *b))
8790
}

fuel-vm/src/interpreter/alu.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use crate::{
1010
error::SimpleResult,
1111
};
1212

13-
use fuel_asm::PanicReason;
14-
use fuel_types::{
15-
RegisterId,
16-
Word,
13+
use fuel_asm::{
14+
PanicReason,
15+
RegId,
1716
};
17+
use fuel_types::Word;
1818

1919
mod muldiv;
2020
mod wideint;
@@ -26,7 +26,7 @@ where
2626
/// Stores the overflowed wrapped value into RegId::OF
2727
pub(crate) fn alu_capture_overflow<F, B, C>(
2828
&mut self,
29-
ra: RegisterId,
29+
ra: RegId,
3030
f: F,
3131
b: B,
3232
c: C,
@@ -48,7 +48,7 @@ where
4848
/// Set RegId::OF to true and zero the result register if overflow occurred.
4949
pub(crate) fn alu_boolean_overflow<F, B, C>(
5050
&mut self,
51-
ra: RegisterId,
51+
ra: RegId,
5252
f: F,
5353
b: B,
5454
c: C,
@@ -69,7 +69,7 @@ where
6969

7070
pub(crate) fn alu_error<F, B, C>(
7171
&mut self,
72-
ra: RegisterId,
72+
ra: RegId,
7373
f: F,
7474
b: B,
7575
c: C,
@@ -89,7 +89,7 @@ where
8989
alu_error(dest, flag.as_ref(), common, f, b, c, err_bool)
9090
}
9191

92-
pub(crate) fn alu_set(&mut self, ra: RegisterId, b: Word) -> SimpleResult<()> {
92+
pub(crate) fn alu_set(&mut self, ra: RegId, b: Word) -> SimpleResult<()> {
9393
let (SystemRegisters { of, err, pc, .. }, mut w) =
9494
split_registers(&mut self.registers);
9595
let dest = &mut w[ra.try_into()?];

fuel-vm/src/interpreter/alu/muldiv.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use crate::{
99
error::SimpleResult,
1010
};
1111

12-
use fuel_asm::PanicReason;
13-
use fuel_types::{
14-
RegisterId,
15-
Word,
12+
use fuel_asm::{
13+
PanicReason,
14+
RegId,
1615
};
16+
use fuel_types::Word;
1717

1818
impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
1919
where
@@ -22,7 +22,7 @@ where
2222
/// Stores the overflowed wrapped value into RegId::OF
2323
pub(crate) fn alu_muldiv(
2424
&mut self,
25-
ra: RegisterId,
25+
ra: RegId,
2626
lhs: Word,
2727
rhs: Word,
2828
divider: Word,

fuel-vm/src/interpreter/alu/wideint.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ use ethnum::U256;
33
use fuel_asm::{
44
wideint::*,
55
PanicReason,
6+
RegId,
67
};
7-
use fuel_types::{
8-
RegisterId,
9-
Word,
10-
};
8+
use fuel_types::Word;
119

1210
use super::super::{
1311
internal::inc_pc,
@@ -70,7 +68,7 @@ macro_rules! wideint_ops {
7068
{
7169
pub(crate) fn [<alu_wideint_cmp_ $t:lower>](
7270
&mut self,
73-
ra: RegisterId,
71+
ra: RegId,
7472
b: Word,
7573
c: Word,
7674
args: CompareArgs,

fuel-vm/src/interpreter/blob.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use fuel_asm::{
2-
RegisterId,
2+
RegId,
33
Word,
44
};
55
use fuel_tx::PanicReason;
@@ -35,7 +35,7 @@ where
3535
{
3636
pub(crate) fn blob_size(
3737
&mut self,
38-
dst: RegisterId,
38+
dst: RegId,
3939
blob_id_ptr: Word,
4040
) -> IoResult<(), S::DataError> {
4141
let gas_cost = self

fuel-vm/src/interpreter/blockchain.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use alloc::vec::Vec;
5050
use fuel_asm::{
5151
Imm06,
5252
PanicReason,
53+
RegId,
5354
};
5455
use fuel_storage::StorageSize;
5556
use fuel_tx::{
@@ -69,7 +70,6 @@ use fuel_types::{
6970
BlockHeight,
7071
Bytes32,
7172
ContractId,
72-
RegisterId,
7373
Word,
7474
};
7575

@@ -237,7 +237,7 @@ where
237237
)
238238
}
239239

240-
pub(crate) fn block_height(&mut self, ra: RegisterId) -> IoResult<(), S::DataError> {
240+
pub(crate) fn block_height(&mut self, ra: RegId) -> IoResult<(), S::DataError> {
241241
let (SystemRegisters { pc, .. }, mut w) = split_registers(&mut self.registers);
242242
let result = &mut w[WriteRegKey::try_from(ra)?];
243243
Ok(block_height(&self.context, pc, result)?)
@@ -277,11 +277,7 @@ where
277277
.code_root(a, b)
278278
}
279279

280-
pub(crate) fn code_size(
281-
&mut self,
282-
ra: RegisterId,
283-
b: Word,
284-
) -> IoResult<(), S::DataError> {
280+
pub(crate) fn code_size(&mut self, ra: RegId, b: Word) -> IoResult<(), S::DataError> {
285281
let gas_cost = self.gas_costs().csiz();
286282
// Charge only for the `base` execution.
287283
// We will charge for the contracts size in the `code_size`.
@@ -308,7 +304,7 @@ where
308304
pub(crate) fn state_clear_qword(
309305
&mut self,
310306
a: Word,
311-
rb: RegisterId,
307+
rb: RegId,
312308
c: Word,
313309
) -> IoResult<(), S::DataError> {
314310
let contract_id = self.internal_contract();
@@ -327,8 +323,8 @@ where
327323

328324
pub(crate) fn state_read_word(
329325
&mut self,
330-
ra: RegisterId,
331-
rb: RegisterId,
326+
ra: RegId,
327+
rb: RegId,
332328
c: Word,
333329
) -> IoResult<(), S::DataError> {
334330
let (SystemRegisters { fp, pc, .. }, mut w) =
@@ -361,7 +357,7 @@ where
361357
pub(crate) fn state_read_qword(
362358
&mut self,
363359
a: Word,
364-
rb: RegisterId,
360+
rb: RegId,
365361
c: Word,
366362
d: Word,
367363
) -> IoResult<(), S::DataError> {
@@ -396,7 +392,7 @@ where
396392
pub(crate) fn state_write_word(
397393
&mut self,
398394
a: Word,
399-
rb: RegisterId,
395+
rb: RegId,
400396
c: Word,
401397
) -> IoResult<(), S::DataError> {
402398
let new_storage_gas_per_byte = self.gas_costs().new_storage_per_byte();
@@ -433,7 +429,7 @@ where
433429
pub(crate) fn state_write_qword(
434430
&mut self,
435431
a: Word,
436-
rb: RegisterId,
432+
rb: RegId,
437433
c: Word,
438434
d: Word,
439435
) -> IoResult<(), S::DataError> {
@@ -468,11 +464,7 @@ where
468464
)
469465
}
470466

471-
pub(crate) fn timestamp(
472-
&mut self,
473-
ra: RegisterId,
474-
b: Word,
475-
) -> IoResult<(), S::DataError> {
467+
pub(crate) fn timestamp(&mut self, ra: RegId, b: Word) -> IoResult<(), S::DataError> {
476468
let block_height = self.get_block_height()?;
477469
let (SystemRegisters { pc, .. }, mut w) = split_registers(&mut self.registers);
478470
let result = &mut w[WriteRegKey::try_from(ra)?];

fuel-vm/src/interpreter/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::{
3636
};
3737
use fuel_asm::{
3838
PanicReason,
39-
RegisterId,
39+
RegId,
4040
Word,
4141
};
4242
use fuel_storage::StorageSize;
@@ -60,7 +60,7 @@ where
6060
{
6161
pub(crate) fn contract_balance(
6262
&mut self,
63-
ra: RegisterId,
63+
ra: RegId,
6464
b: Word,
6565
c: Word,
6666
) -> Result<(), RuntimeError<S::DataError>> {

0 commit comments

Comments
 (0)