I’m learning Cairo and I want to know more about Gas Optimization. In Solidity compiler, there is a difference between writing uint128 and uint256. Similarly to C and other languages, for example:
contract ThisIsNotAnOptimizedContract{
uint128 Zero;
uint256 One;
uint128 Two;
}
contract ThisIsAGasOptimizedContract{
uint128 Zero;
uint128 Two;
uint256 One;
}
uint128
variables each occupy a separate uint256
just for themselves, wasting 128 bits for each slot (Like the first contract). The Solidity compiler puts 2 uint128
in the one 256-bit memory slot when they are declared next to each other.
I’m curious if there is any similar issue in Cairo. Uint256
uses two felt objects for low/high bits of the value.
struct Uint256 {
// The low 128 bits of the value.
low: felt,
// The high 128 bits of the value.
high: felt,
}
Is there any similar practice in Cairo to optimize felt
and Uint256 objects to save some compiler steps?
AFAIK, there isn't a similar optimization technique in Cairo as what you described for Solidity. It might help to keep in mind that a
felt
is stored using 251 bits so anything bigger than that likeuint256
will need more than onefelt
to represent it.