code-423n4 / 2022-02-aave-lens-findings

0 stars 0 forks source link

Gas Optimizations #39

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

GAS

  1. The following structs could be optimized moving the position of certains values in order to save slot storages:

  2. Avoid declare variables before use it:

  3. Emit the event before change the state in order to save one variable:

before:

    function setEmergencyAdmin(address newEmergencyAdmin) external override onlyGov {
        address prevEmergencyAdmin = _emergencyAdmin;
        _emergencyAdmin = newEmergencyAdmin;
        emit Events.EmergencyAdminSet(
            msg.sender,
            prevEmergencyAdmin,
            newEmergencyAdmin,
            block.timestamp
        );
    }

after:

    function setEmergencyAdmin(address newEmergencyAdmin) external override onlyGov {
        emit Events.EmergencyAdminSet(
            msg.sender,
            _emergencyAdmin,
            newEmergencyAdmin,
            block.timestamp
        );
        _emergencyAdmin = newEmergencyAdmin;
    }

This logic could be used to save gas in:

  1. Use delete instead of set to default value (false or 0)

  2. Use storage keyword for save gas in order to cache a storage pointer.

Zer0dot commented 2 years ago

First point is invalid, those are not stored but are only ever used calldata. Second point is technically valid, but the impact is not enough to be worth the readability change as the only time this would be helpful is when a user attempts to call the implementation. We emit events at the end by convention for readability, since these are governance/admin functions anyway, we don't see much value in implementing this.

Using delete doesn't seem to make sense to me either, as this would add logic in certain cases and I find setting a boolean to false is clearer for readability. This should be handled by the optimizer and the impact appears negligible, if any. Finally using the storage pointer actually increased gas, so we're not doing that either.

Still, though these points are not valid for our case, they are still pretty well thought out!