status-network-token icon indicating copy to clipboard operation
status-network-token copied to clipboard

Low - MiniMeToken.sol updateValueAtNow() optimization

Open k4ms opened this issue 8 years ago • 0 comments
trafficstars

In 'MiniMeToken.sol' the updateValueAtNow() function adds levels of complexity than can be avoided:

  • calling push() is cheaper than increasing the checkpoint length and assigning value to the struct
  • too many local variables are used

Proposed implementation:

    function updateValueAtNow(Checkpoint[] storage checkpoints, uint _value) internal  {
        if ((checkpoints.length == 0)
        || (checkpoints[checkpoints.length -1].fromBlock < getBlockNumber())) {
             checkpoints.push(Checkpoint({
                 fromBlock: uint128(getBlockNumber()),
                 value: uint128(_value)
             }));
        } else {
            checkpoints[checkpoints.length-1].value = uint128(_value);     
        }
    }

k4ms avatar Jun 18 '17 21:06 k4ms