-
Notifications
You must be signed in to change notification settings - Fork 830
Open
Description
There's a cleanup that can be done in the log2floor
function because the
if l == 0 {
statement is unreachable.
Proof:
This branch could only be reached if value.as_limbs()[i].leading_zeros() == 64
when the loop has been iterated 4 times. But this condition is only true if value.as_limbs()[i] == 0u64
, which is covered above. Meaning that the two branches are mutually exclusive.
Thus, we can just remove the statement.
diff --git a/crates/interpreter/src/gas/calc.rs b/crates/interpreter/src/gas/calc.rs
index 08ec9c0f..17330d84 100644
--- a/crates/interpreter/src/gas/calc.rs
+++ b/crates/interpreter/src/gas/calc.rs
@@ -71,12 +71,7 @@ const fn log2floor(value: U256) -> u64 {
if value.as_limbs()[i] == 0u64 {
l -= 64;
} else {
- l -= value.as_limbs()[i].leading_zeros() as u64;
- if l == 0 {
- return l;
- } else {
- return l - 1;
- }
+ return l - value.as_limbs()[i].leading_zeros() as u64 - 1;
}
if i == 0 {
break;
Tried to optimize further the hotpath by checking for zeroeness of value
as a first condition which would exclude the last limb to be 0
, reducing the loop iteration by one but it made the performance a little bit worse for value != 0
, which makes sense:
diff --git a/crates/interpreter/src/gas/calc.rs b/crates/interpreter/src/gas/calc.rs
index 08ec9c0f..e843f6bb 100644
--- a/crates/interpreter/src/gas/calc.rs
+++ b/crates/interpreter/src/gas/calc.rs
@@ -65,25 +65,22 @@ pub const fn create2_cost(len: usize) -> Option<u64> {
#[inline]
const fn log2floor(value: U256) -> u64 {
+ if value.leading_zeros() == 256 {
+ return 0;
+ }
let mut l: u64 = 256;
let mut i = 3;
loop {
if value.as_limbs()[i] == 0u64 {
l -= 64;
- } else {
- l -= value.as_limbs()[i].leading_zeros() as u64;
- if l == 0 {
+ if i == 2 {
return l;
- } else {
- return l - 1;
}
+ i -= 1;
+ } else {
+ return l - value.as_limbs()[i].leading_zeros() as u64 - 1;
}
- if i == 0 {
- break;
- }
- i -= 1;
}
- l
}
/// `EXP` opcode cost calculation.
Metadata
Metadata
Assignees
Labels
No labels