Use Local Memory Type Variable Instead of Global Storage Type Variable in Event to Save Gas
Ether1oop opened this issue · 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:
- sOlympusERC20.sol
function name: initialize
event name: Transfer LogStakingContractUpdated
variable: stakingContract->_stakingContract
function initialize(address _stakingContract, address _treasury) external {
require(msg.sender == initializer, "Initializer: caller is not initializer");
require(_stakingContract != address(0), "Staking");
stakingContract = _stakingContract;
_gonBalances[stakingContract] = TOTAL_GONS;
require(_treasury != address(0), "Zero address: Treasury");
treasury = _treasury;
emit Transfer(address(0x0), stakingContract, _totalSupply);
emit LogStakingContractUpdated(stakingContract);
initializer = address(0);
}
- OlympusAuthority.sol
function name: constructor
event name: GovernorPushed GuardianPushed PolicyPushed VaultPushed
variable: governor->_governor guardian->_guardian policy->_policy vault->_vault
constructor(
address _governor,
address _guardian,
address _policy,
address _vault
) OlympusAccessControlled(IOlympusAuthority(address(this))) {
governor = _governor;
emit GovernorPushed(address(0), governor, true);
guardian = _guardian;
emit GuardianPushed(address(0), guardian, true);
policy = _policy;
emit PolicyPushed(address(0), policy, true);
vault = _vault;
emit VaultPushed(address(0), vault, true);
}
}
function name: pushGovernor
event name: GovernorPushed
variable: newGovernor->_newGovernor
function pushGovernor(address _newGovernor, bool _effectiveImmediately) external onlyGovernor {
if (_effectiveImmediately) governor = _newGovernor;
newGovernor = _newGovernor;
emit GovernorPushed(governor, newGovernor, _effectiveImmediately);
}
function name: pushGuardian
event name: GuardianPushed
variable: newGuardian->_newGuardian
function pushGuardian(address _newGuardian, bool _effectiveImmediately) external onlyGovernor {
if (_effectiveImmediately) guardian = _newGuardian;
newGuardian = _newGuardian;
emit GuardianPushed(guardian, newGuardian, _effectiveImmediately);
}
function name: pushPolicy
event name: PolicyPushed
variable: newPolicy->_newPolicy
function pushPolicy(address _newPolicy, bool _effectiveImmediately) external onlyGovernor {
if (_effectiveImmediately) policy = _newPolicy;
newPolicy = _newPolicy;
emit PolicyPushed(policy, newPolicy, _effectiveImmediately);
}
function name: pushVault
event name: VaultPushed
variable: newVault->_newVault
function pushVault(address _newVault, bool _effectiveImmediately) external onlyGovernor {
if (_effectiveImmediately) vault = _newVault;
newVault = _newVault;
emit VaultPushed(vault, newVault, _effectiveImmediately);
}
- Timelock.sol
function name: setDelay
event name: NewDelay
variable: delay->delay_
function setDelay(uint256 delay_) public {
require(msg.sender == address(this), "Timelock::setDelay: Call must come from Timelock.");
require(delay_ >= MINIMUM_DELAY, "Timelock::setDelay: Delay must exceed minimum delay.");
require(delay_ <= MAXIMUM_DELAY, "Timelock::setDelay: Delay must not exceed maximum delay.");
delay = delay_;
emit NewDelay(delay);
}
function name: setPendingAdmin
event name: NewPendingAdmin
variable: pendingAdmin->pendingAdmin_
function setPendingAdmin(address pendingAdmin_) public {
require(msg.sender == address(this), "Timelock::setPendingAdmin: Call must come from Timelock.");
pendingAdmin = pendingAdmin_;
emit NewPendingAdmin(pendingAdmin);
}
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!