2

I am experimenting on saving gas by releasing some storage on my smart contract.

In the smart contract there is a mapping, which i fill with data by calling the function setStorage. The other function testFunction is deleting the value of the passed in bytes32 keys of the mapping and doing some useless work so simulate a real function using gas.

I am using remix and looking up the gas cost in the execution cost field of the console output.

Here are the tx gas costs for different scenarios:

  • deleting 1 item, which is non-zero: 60302
  • deleting 1 item, which is zero: 62302
  • deleting 3 items, which are non-zero: 64054
  • not deleting at all, by commenting out the delete part of the function: 60015 I do not understand why commenting out the delete part of the function is cheaper than deleting an item of the mapping, which holds data in it.

Also I dont get why he tx cost goes up, if 3 items are deleted instead of 1.

Here is my contract

contract ReleasingStorage 
{
        mapping (bytes32 => uint256) bytesNumberMapping;

        function setMappingStorage(bytes32 _bytes32, uint256 _number) public returns (uint256) 
    {        
        bytesNumberMapping[_bytes32] = _number;
        return _number;
    }

    function mappingTest(bytes32[] memory items) public returns (uint256)
    {        
        for (uint i=0; i<items.length; i++) 
        {
            delete bytesNumberMapping[items[i]];
        }
        uint256 result;
        for (uint256 i; i < 100; i++) 
        {
            result = result + 109237091237091237091;
        }
        return result;
    }
}

1 Answer 1

0

Deleting an item still costs gas.

Let's analyse exactly the gas cost, following the formula (from evm.codes):

# value: value from the stack input.
# current_value: current value of the storage slot.
# original_value: value of the storage slot before the current transaction.

static_gas = 0

if value == current_value
    if key is warm
        base_dynamic_gas = 100
    else
        base_dynamic_gas = 100
else if current_value == original_value
    if original_value == 0
        base_dynamic_gas = 20000
    else
        base_dynamic_gas = 2900
else
    base_dynamic_gas = 100

if key is cold
    base_dynamic_gas += 2100
# refunds
if value != current_value
    if current_value == original_value
        if original_value != 0 and value == 0
            gas_refunds += 4800
    else
        if original_value != 0
            if current_value == 0
                gas_refunds -= 4800
            else if value == 0
                gas_refunds += 4800
        if value == original_value
            if original_value == 0
                if key is warm
                    gas_refunds += 20000 - 100
                else
                    gas_refunds += 19900
            else
                if key is warm
                    gas_refunds += 5000 - 2100 - 100
                else
                    gas_refunds += 4900
  • deleting 1 item, which is zero: gas = 100+2100, refund = 0, total = 2200
  • deleting 1 item, which is non-zero: gas = 2900+2100, refund=4800, total = 200

So, going from not deleting at all (60015) to deleting a non-zero (60302) should cost 200 gas more (~100 extra must be to calldata/stack manipulations). Going from deleting a non-zero (60302) to deleting a zero (62302) should cost 2200-200 = 2000 more. So the theory works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.