DeFiOptions / DeFiOptions-core

Core smart contracts of defi options
GNU General Public License v3.0
111 stars 18 forks source link

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

Open Ether1oop opened 2 years ago

Ether1oop commented 2 years ago

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:

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!

cinquemb commented 2 years ago

Who is "we"? I personally can't tell if what you have done is useful for this without a clear example (like a "before" and "after").

Ether1oop commented 2 years ago

Who is "we"? I personally can't tell if what you have done is useful for this without a clear example (like a "before" and "after").

Thanks a lot for your response. We are a research group on programming languages and software engineering, and we recently have conducted a systematic study about Solidity event usage, evolution, and impact. One of our major findings is that when we use emit operation to store the value of a certain variable, local memory type variable would be preferable to storage type (state) variable if they hold the same value, because this can save gas (an extra SLOAD operation is needed to access the variable if it is storage typ). For example, for the following code,

    function setImplementation(address _implementation) public {

        require(msg.sender == owner && locked != 2);
        address oldImplementation = implementation;
        implementation = _implementation;
-       emit ImplementationUpdated(oldImplementation, implementation);
+       emit ImplementationUpdated(oldImplementation, _implementation);
    }

with regard to event use ImplementationUpdated, we can change the storge type variable implementation into local memory type variable _implementation to save gas.

cinquemb commented 2 years ago

Oh ok, I think i understand now, thank you for making it clear. That is pretty interesting that using using _implementation vs implementation var will save more gas. And this will hold true for all events where part of what is emitted comes from the function arguments?

Ether1oop commented 2 years ago

Oh ok, I think i understand now, thank you for making it clear. That is pretty interesting that using using _implementation vs implementation var will save more gas. And this will hold true for all events where part of what is emitted comes from the function arguments?

We appreciate your comments!Yes,we think so as long as the function argument is memory type.

cinquemb commented 2 years ago

Any place i can read more about your research?

Ether1oop commented 2 years ago

Any place i can read more about your research?

Thanks a lot for your interest in our research, we have submitted our reseach results to a computer science conference and the paper is now under review, we are not allowed to disclose the content before there is a decision about the paper. We will make the result publicy avilable when there is a result about the paper. Thanks again for your time!