How could i make it more gas efficient?
You can change bits size of your uint
's in struct.
If you are using uint256
you are using whole 32 byte chunk. If you were using uint128
for example, you could fit more data in one SSTORE
operation (due to solidity compiler optimization).
Let me give some examples of gas consumption:
Function:
function getVariables() public {
deposits[msg.sender].a = 1;
deposits[msg.sender].b = 1;
deposits[msg.sender].c = 1;
}
uint256 struct
struct demo {
uint256 a;
uint256 b;
uint256 c;
}
Execution cost: 60455 gas
uint128 struct
struct demo {
uint128 a;
uint128 b;
uint128 c;
}
Execution cost: 46264 gas
uint64 struct
struct demo {
uint64 a;
uint64 b;
uint64 c;
}
Execution cost: 31314 gas
If the Solidity compiler can figure out the size of store variables,
it simply lays them out one after another in storage. If possible, the
compiler packs the data tightly in chunks of 32 bytes.
From Diving Into The Ethereum VM Part 2.