I have a couple of questions.
- we have int types as
uint8, uint16, uint32, etc.
I read that :
Normally there's no benefit to using these sub-types because Solidity reserves 256 bits of storage regardless of the uint size. For example, using uint8 instead of uint (uint256) won't save you any gas.
So If I type directly in the contract as state variable something like this:
- uint8 test1;
- uint32 test2;
- uint test3;
Why won't uint8 test
have less gas consumption wherever I use it than uint32 test2
? I am asking because with uint8 test1
, I use less storage space.
- Let's say I use struct differently.
// way1
struct Person {
uint8 test1;
uint32 test2;
uint8 test3;
}
// way2
struct Person {
uint8 test1;
uint8 test2;
uint32 test3;
}
Why would the second way consume less gas than the first one ? If we look at the both struct, they both consume the same amount of storage, but still it's said way2 is gonna be much better for less gas consumption . Why ?