3

To my understanding, delete x merely sets the value of x to the default value defined for the type of x (which is typically 0 in one way or another).

For example:

uint x = 1;
delete x; // sets x = 0

bool x = true;
delete x; // sets x = false

The doc actually says it pretty explicitly:

It is important to note that delete a really behaves like an assignment to a, i.e. it stores a new object in a.

But my question is really with regards to mappings.

Suppose I have mapping (address => bool) validAddresses and some address x.

Are the following two statements equivalent:

  1. validAddresses[x] = false;
  2. delete validAddresses[x];

Or is there any benefit in #2 over #1 (as there would be in "traditional" languages)?

The doc states something which might answer my question:

delete has no effect on whole mappings (as the keys of mappings may be arbitrary and are generally unknown).

But I'm honestly having problems understanding it.

I do understand that delete has no effect on anything other than mappings, in the sense that no memory is being freed as a result of executing it.

But I do not understand whether or not the same thing applies for mappings.

UPDATE:

I went ahead and disassembled each one of these two statements via solc --asm.

Statement #1:

    /* "contracts/MyContract.sol":1490:1538  validAddresses[x] = false */
  dup1
  sload
  0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00
  and
  swap1
  sstore

Statement #2:

    /* "contracts/MyContract.sol":1490:1537  delete validAddresses[x] */
  dup1
  sload
  0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00
  and
  swap1
  sstore

The compiled code indicates that the two statements are identical, so I'm basically just looking for a confirmation on this fact (in opposed to the answer below, which suggests that gas-refund applies only when delete is used explicitly).

2
  • 1
    You should add your finding as an answer.
    – Ismael
    Apr 2 '18 at 5:48
  • @Ismael: I'm still looking for a confirmation on that. At the end of the asm file there's a section named auxdata, which differs between the two cases. So I am not 100% sure that both are indeed identical. Apr 2 '18 at 6:00
3

When you use the delete operation, rather than assigning to 0, you get a 10000 gas refund.

Update

It appears you are correct, and that the refund doesn't matter whether you zero the variable or use delete.

I tested the following contract in remix, and the transaction and execution costs of the d and c functions are nearly identical, although the function which uses delete used 104 less gas. I'm not sure what accounts for this.

pragma solidity ^0.4.20;

contract T{
    mapping (address => bool) validAddresses;
    function set() external{
        validAddresses[0xca35b7d915458ef540ade6068dfe2f44e8fa733c] = true;
    }
    function c() external{
        validAddresses[0xca35b7d915458ef540ade6068dfe2f44e8fa733c] = false;
        //tx 13409
        //ex 5545 
    }
    function d() external{
        delete validAddresses[0xca35b7d915458ef540ade6068dfe2f44e8fa733c];
        //tx 13374
        //ex 5476
    }
}
4
  • Can you give a reference to the docs please? Apr 1 '18 at 14:19
  • Also @AnAllergyToAnalogy The extra cost for c() is because the optimiser realises that the code for d() is essentially the same and c() just does a jump to d() which means that you pay a little extra for the jump, but save a bunch on contract creation gas.
    – norganna
    Apr 2 '18 at 1:53
  • See the yellow paper for documentation at yellowpaper.io. Specifically you're interested in Gsset Gsreset and Rsclear. Gsreset costs you 5,000 gas, but Rsclear refunds you 15,000 gas at the successful conclusion of the execution, leaving you ahead by 10,000 gas. Rsclear specifically states "set to zero from non-zero" not "deleted".
    – norganna
    Apr 2 '18 at 2:48
  • Ah I wasn't aware of the yellow paper, thankyou. That's quite a useful resource! Apr 2 '18 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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