Hi guys, I am searching for a reason why our bytecode is so immensely big (>3.5 million bytes) and I stumbled upon this really weird pattern, so I would like to know whether this is expected behavior.
As of now, I can only find this behavior in combination with using U256, although this is specific to our contracts and I wouldn’t rule out the effect of other var types. Given this array param behavior, it is not surprising that our bytcodesize is that bloated as we are using similar functions a lot.
To recreate what I’m seeing, you can use the following sample contract and uncomment the function calls in each setup independently (the setups work mutually exclusive!):
contract;
use std::u256::U256;
abi MyContract {
fn foo();
}
impl MyContract for Contract {
fn foo() {
// Declaring a bunch of test vars
let var0: u64 = 1;
let var1: u64 = 2;
let var2: u64 = 3;
// ***** Byte code only var declaration: 68 bytes *****
// Calling the test functions
// Setup 1): One call without array param
// let res0 = multiply_div(var2, var1, var0);
// ***** Byte code setup 1): 14,100 bytes *****
// Setup 2): Two calls without array param
// let res0 = multiply_div(var2, var1, var0);
// let res1 = multiply_div(var2, var1, var0);
// ***** Byte code setup 2): 14,364 bytes *****
// Setup 3): One call WITH array param
// let res0 = multiply_div_array(var2, var1, var0, [1, 1]);
// ***** Byte code setup 3): 14,140 bytes *****
// Setup 4): Two calls WITH array param
// let res0 = multiply_div_array(var2, var1, var0, [1, 1]);
// let res1 = multiply_div_array(var2, var1, var0, [1, 1]);
// ***** Byte code setup 4): 29,940 bytes *****
}
}
fn multiply_div(a: u64, b: u64, c: u64) -> u64 {
let var = (U256::from((0, 0, 0, a)) * U256::from((0, 0, 0, b)));
let result_wrapped = (var / U256::from((0, 0, 0, c))).as_u64();
match result_wrapped {
Result::Ok(inner_value) => inner_value, _ => revert(0),
}
}
fn multiply_div_array(a: u64, b: u64, c: u64, some_array: [u64; 2]) -> u64 {
let var = (U256::from((0, 0, 0, a)) * U256::from((0, 0, 0, b)));
let result_wrapped = (var / U256::from((0, 0, 0, c))).as_u64();
match result_wrapped {
Result::Ok(inner_value) => inner_value, _ => revert(0),
}
}