I have this function:

function foo(uint256 _a, uint256 _b, uint256 _c) {
    require(map[msg.sender].a == 1);
    require(map[msg.sender].b < 1);
    require(map[msg.sender].c > 1);

    deposits[msg.sender].a = _a;
    deposits[msg.sender].b = _b;
    deposits[msg.sender].c = _c;

Is this extremely wasteful?

How could i make it more gas efficient?


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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.