Hi there, suppose I have a struct with variables given here, I’m wondering if it is better to change uint16
to uint256
when I’m pretty sure _a1
etc are within the range of an uint16
. Is using an uint16
variable causing more troubles or saving more gas? This is a question since EVM is a 256-bit machine.
struct Info {
uint16 _a1;
uint16 _b1;
uint16 _c1;
string _name;
address _owner;
address _addr;
}
If this struct will be in storage, it’s definitely worth using uint16
. The reason is that more values will be packed in the same slot in storage, and specially after the Berlin repricing of SLOAD and SSTORE, packing data in storage provides huge gas savings.
Yes, since EVM is a 256-bit machine, operating with these numbers may incur greater overhead, but it will be a lot less than what you get from saving in storage slots, besides now that Solidity 0.8 does overflow checking the difference with using uint256
is probably very small.
1 Like
Thanks, @frangio The overheading part is what I was concerned about. Good to hear it is not the primary concern.