Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Ether1oop opened this issue Aug 19, 2022 · 3 comments

Comments

@Ether1oop
Copy link

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:

  • TimeERC20.sol
    function name:setVault
    event name:  VaultTransferred
    variable:    vault ->vault
  function setVault( address vault_ ) external onlyOwner() {
    require(vault_ != address(0), "IA0");
    _vault = vault_;
    emit VaultTransferred( _vault );
  }

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!

@Pradipmonda
Copy link

Pradipmonda commented Aug 20, 2022 via email

@Ether1oop
Copy link
Author

WHY WONDERLAND DOES-NOT HAVE A CUSTOMER SERVICE?????? V

On Fri, Aug 19, 2022 at 10:33 AM Ether1oop @.> wrote: 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: - TimeERC20.sol https://github.com/Wonderland-Money/contracts/blob/main/TimeERC20.sol function name: setVault event name: VaultTransferred variable: vault ->vault function setVault( address vault_ ) external onlyOwner() { require(vault_ != address(0), "IA0"); vault = vault; emit VaultTransferred( _vault ); } 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! — Reply to this email directly, view it on GitHub <#15>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXDHQN73A4S6N6RLNMHPPKDVZ42EZANCNFSM567XT3OQ . You are receiving this because you are subscribed to this thread.Message ID: @.>

We appreciate your comments!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). We wish our findings are useful. We are constantly improving our prototype and also considering whether such (and similar) gas optimizations can be intergeated into the compiler optimization process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@Ether1oop @Pradipmonda and others