Tokens icon indicating copy to clipboard operation
Tokens copied to clipboard

Use Local Memory Type Variable Instead of Global Storage Type Variable in Event to Save Gas

Open Ether1oop opened this issue 2 years ago • 0 comments

Hi, we recently have conducted a systematic study about Solidity event usage, evolution, and impact, and we are attempting to build a tool to improve the practice of Solidity event use based on our findings. We have tried our prototype tool on some of the most popular GitHub Solidity repositories, and for your repository, we find a potential optimization of gas consumption arisen from event use.

The point is that when we use emit operation to store the value of a certain variable, local memory type variable would be preferable to global storage type (state) variable if they hold the same value. The reason is that an extra SLOAD operation would be needed to access the variable if it is storage type, and the SLOAD operation costs 800 gas.

For your repository, we find that the following event use can be improved:

    function EtherNomin(address _havven, address _oracle,
                        address _beneficiary,
                        uint initialEtherPrice,
                        address _owner, TokenState initialState)
        ExternStateProxyFeeToken("Ether-Backed USD Nomins", "eUSD",
                                 15 * UNIT / 10000, // nomin transfers incur a 15 bp fee
                                 _havven, // the havven contract is the fee authority
                                 initialState,
                                 _owner)
        public
    {
        oracle = _oracle;
        beneficiary = _beneficiary;

        etherPrice = initialEtherPrice;
        lastPriceUpdate = now;
        emit PriceUpdated(etherPrice);

        // It should not be possible to transfer to the nomin contract itself.
        frozen[this] = true;
    }
  • FlexibleToken.sol function name:constructor event name:  Transfer variable:    _totalSupply->_initialSupply
    constructor(string _symbol, string _name, uint8 _decimals, uint _initialSupply) public {
        symbol = _symbol;
        name = _name;
        decimals = _decimals;
        _totalSupply = _initialSupply;
        balances[owner] = _totalSupply;
        emit Transfer(address(0), owner, _totalSupply);
    }

Do you find our results useful? Your reply and invaluable suggestions would be greatly appreciated, and are vital for improving our tool. Thanks a lot for your time!

Ether1oop avatar Aug 19 '22 07:08 Ether1oop