argent-contracts icon indicating copy to clipboard operation
argent-contracts copied to clipboard

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

Open HighBe opened this issue 1 year ago • 2 comments

Hi, we are a research group on programming languages and software engineering. 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 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 several event uses can be improved.

HighBe avatar Aug 28 '22 02:08 HighBe

Interesting, which code analysis tool did you use to detect this?

delaaxe avatar Aug 29 '22 09:08 delaaxe

Interesting, which code analysis tool did you use to detect this?

Thanks a lot for your reply. We initially find this problem by collecting and analyzing commit changes made to event use in popular Github Solidity projects, we find that a good percentage of changes are replacing Storage type variable with Memory type variable, we thus further analyse the corresponding EVM bytecode and get the underlying reason. Based on this analysis, we devlope a prototype tool to detect code that suffers from this problem.

HighBe avatar Sep 05 '22 13:09 HighBe