Skip to content

Optimize Offer structure (gas usage) #39

Open
@pauliax

Description

@pauliax

Description

As the gas inefficiencies are also in the scope, I would like to suggest packing variables in struct more efficiently.
OffersBase.sol has an Offer struct:

    struct Offer {
        uint64 expiresAt;
        address bidder;
        uint16 offerCut;
        uint128 total;
        uint128 unsuccessfulFee;
    }

From https://solidity.readthedocs.io/en/v0.4.21/miscellaneous.html
"Statically-sized variables (everything except mapping and dynamically-sized array types) are laid out contiguously in storage starting from position 0. Multiple items that need less than 32 bytes are packed into a single storage slot if possible",
"Finally, in order to allow the EVM to optimize for this, ensure that you try to order your storage variables and struct members such that they can be packed tightly. "

Also, you should take into consideration that smaller data types like uint128 or uint64 are not always more efficient than uint (which defaults to uint256):
"When using elements that are smaller than 32 bytes, your contract’s gas usage may be higher. This is because the EVM operates on 32 bytes at a time. Therefore, if the element is smaller than that, the EVM must use more operations in order to reduce the size of the element from 32 bytes to the desired size."

Impact

Low

Fix

    struct Offer {
        uint128 total;
        uint128 unsuccessfulFee;
        uint64 expiresAt;
        uint16 offerCut;
        address bidder;
    }

or you could try to find a more efficient way to store this structure.

Ethereum address

0x09Cf79Bdf8F68739979C8c825C103A7538Bd4f4b

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions