You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Framework version: v11 Description: cmp_bytes and cmp_bcs_bytes calls the cmp_u8 function for each byte, the CALL operation consumes much gas. We did a test, putting the compare codes into cmp_bytes and cmp_bcs_bytes, we got a 20% gas reduction when comparing 100 bytes once. Code Location: sources/Compare.move, line 38,57
public fun cmp_bcs_bytes(v1: &vector<u8>, v2: &vector<u8>): u8 {
let i1 = Vector::length(v1);
let i2 = Vector::length(v2);
let len_cmp = cmp_u64(i1, i2);
// BCS uses little endian encoding for all integer types, so we choose to compare from left
// to right. Going right to left would make the behavior of Compare.cmp diverge from the
// bytecode operators < and > on integer values (which would be confusing).
while (i1 > 0 && i2 > 0) {
i1 = i1 - 1;
i2 = i2 - 1;
let elem_cmp = cmp_u8(*Vector::borrow(v1, i1), *Vector::borrow(v2, i2));
if (elem_cmp != 0) return elem_cmp
// else, compare next element
};
// all compared elements equal; use length comparison to break the tie
len_cmp
}
public fun cmp_bytes(v1: &vector<u8>, v2: &vector<u8>): u8 {
let l1 = Vector::length(v1);
let l2 = Vector::length(v2);
let len_cmp = cmp_u64(l1, l2);
let i1 = 0;
let i2 = 0;
while (i1 < l1 && i2 < l2) {
let elem_cmp = cmp_u8(*Vector::borrow(v1, i1), *Vector::borrow(v2, i2));
if (elem_cmp != 0) {
return elem_cmp
};
// else, compare next element
i1 = i1 + 1;
i2 = i2 + 1;
};
// all compared elements equal; use length comparison to break the tie
len_cmp
}
Recommendation: Remove the cmp_u8 call, and write the comparing codes in line.
The text was updated successfully, but these errors were encountered:
Framework version: v11
Description: cmp_bytes and cmp_bcs_bytes calls the cmp_u8 function for each byte, the CALL operation consumes much gas. We did a test, putting the compare codes into cmp_bytes and cmp_bcs_bytes, we got a 20% gas reduction when comparing 100 bytes once.
Code Location: sources/Compare.move, line 38,57
Recommendation: Remove the cmp_u8 call, and write the comparing codes in line.
The text was updated successfully, but these errors were encountered: