Comparing ETH transfer methods

Solidity

Notes

08/18/2022


There are two ways of transferring ETH. Both may be functionally identical - but they are suprisingly different in gas usage. In fact the call() version can pay approximately three times more in extra gas per 32 bytes of data returned!

image1

When returning data from a contract, memory has to be used to place the data to be returned into. The more memory used, the more gas charged for using it.

This cost happens during both techniques of sending ETH, and so doesn't account for any of the difference.

However, when you use the call() method, even with an ignored data return variable in the code, solidity still uses a calldatacopy opcode (with a gas cost that follows the size of the data), and that calldatacopy also pays a scaling memory storage and increase cost..

image2

So cost the cost difference between the two methods comes down to that call() uses three operations that scale with the size of the returned data, and transfer used one.

(The increase isn’t strictly linear since the memory resizing costs increases as more memory is used.)

Acknowledgement: Daniel Von Fange