...by Daniel Szego
quote
"On a long enough timeline we will all become Satoshi Nakamoto.."
Daniel Szego

Monday, April 2, 2018

Solidity gas optimization - using logs instead of state variables


In Ethereum one of the most expensive operation is storing value in the blockchain itself, costing 20000 gas for creating a new variable and 500 to modify them. However, if you do not need to access the variable from another contract, just from outside of the ethereum virtual machine, like from a javascript UI, you can simply use an event that is written into the log of the blockchain instead of a state variable. Writing a log costs only 375 gas, certainly it works with a little bit different semantics as setting the value of a variable and can not be called from a contract, so it has to be carefully analyzed if it can be used in a certain usecase.

The idea can be executed in a more formal fashion as well, like creating an explicit state variable analysis of the smart contracts which shows for each state variable if it is necessarily needed to be accessed from a smart contract at the moment or in the future.