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

Gas Optimizations #95

Open
code423n4 opened this issue Apr 27, 2022 · 1 comment
Open

Gas Optimizations #95

code423n4 opened this issue Apr 27, 2022 · 1 comment
Labels

Comments

@code423n4
Copy link
Contributor

decrementWeightUntilFree can be optimized by using _userGauges[user].at(i) and _userGauges[user].length() directly instead of storing _userGauges[user].values() as gaugeList and calling gaugeList.length and gaugeList[i].

Implementation:

function _decrementWeightUntilFree(address user, uint256 weight) internal {
    uint256 userFreeWeight = balanceOf[user] - getUserWeight[user];

    // early return if already free
    if (userFreeWeight >= weight) return;

    uint32 currentCycle = _getGaugeCycleEnd();

    // cache totals for batch updates
    uint112 userFreed;
    uint112 totalFreed;

    // Loop through all user gauges, live and deprecated
    //address[] memory gaugeList = _userGauges[user].values();

    // Free gauges until through entire list or under weight
    uint256 size = _userGauges[user].length();
    //uint256 size = gaugeList.length;
    for (uint256 i = 0; i < size && (userFreeWeight + totalFreed) < weight; ) {
        //address gauge = gaugeList[i];
        address gauge = _userGauges[user].at(i);
        uint112 userGaugeWeight = getUserGaugeWeight[user][gauge];
        if (userGaugeWeight != 0) {
            // If the gauge is live (not deprecated), include its weight in the total to remove
            if (!_deprecatedGauges.contains(gauge)) {
                totalFreed += userGaugeWeight;
            }
            userFreed += userGaugeWeight;
            _decrementGaugeWeight(user, gauge, userGaugeWeight, currentCycle);

            unchecked {
                i++;
            }
        }
    }

    getUserWeight[user] -= userFreed;
    _writeGaugeWeight(_totalWeight, _subtract, totalFreed, currentCycle);
}

Gas testing function:

function testGasGauge() public {
    token.mint(address(this), 100e18);
    token.setMaxGauges(5);

    uint160 i = 1; // address 0 causes reverts
    while (i < 3) {
        token.addGauge(address(i));
        token.incrementGauge(address(i), 50e18);
        i++;
    }

    startMeasuringGas("transfer call");
    token.transfer(address(1000000), 1);
    stopMeasuringGas();
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 27, 2022
code423n4 added a commit that referenced this issue Apr 27, 2022
@Joeysantoro
Copy link
Collaborator

good optimization

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

No branches or pull requests

2 participants