remix-project icon indicating copy to clipboard operation
remix-project copied to clipboard

solc gas output different from remix

Open PatrickAlphaC opened this issue 3 years ago • 10 comments

I have a file that looks as such:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

contract BasicSolContract {
    uint256 public myVar;

    function updateVar(uint256 myNewVar) external {
        myVar = myNewVar;
    }
}

When I compile it with solc and look for a gas estimate, I get the following:

solc --gas --base-path . -o ./build ./contracts/solidity/BasicSolContract.sol

Gas estimation:
construction:
   111 + 61400 = 61511
external:
   myVar():     2429
   updateVar(uint256):  22498

However in remix, when I deploy it, I can see the gas is different.

Screen Shot 2022-06-15 at 11 07 24 AM

The optimizer is enabled with 200 runs in remix.

PatrickAlphaC avatar Jun 15 '22 15:06 PatrickAlphaC

@PatrickAlphaC Have you compared with other ways of sending transactions?

Aniket-Engg avatar Jun 24 '22 12:06 Aniket-Engg

I think this is not specific to Remix or VM. Without optimization, Remix shows gas cost as 119683 which is similar If contract is deployed on Goerli Screenshot 2022-06-28 at 12 42 25 PM Screenshot 2022-06-28 at 12 49 09 PM

Aniket-Engg avatar Jun 28 '22 07:06 Aniket-Engg

Yeah... I'm not sure who is right. I'm thinking someone's compiler is doing something funky.

PatrickAlphaC avatar Jun 29 '22 23:06 PatrickAlphaC

@PatrickAlphaC I have asked Solidity team to have a look. Let's see.

Aniket-Engg avatar Jul 04 '22 07:07 Aniket-Engg

This is known issue: https://github.com/ethereum/solidity/issues/8920

Aniket-Engg avatar Jul 04 '22 08:07 Aniket-Engg

Wow! Thanks for this!

PatrickAlphaC avatar Jul 06 '22 00:07 PatrickAlphaC

Are there any alternative libraries that Remix could use for gas estimation? Honestly, there's so much that the estimator in the compiler does not cover that we're debating if it's even useful in practice. And even if we fix the constructor quirk, it's unlikely we'll be putting much more work into it in the near future...

cameel avatar Jul 06 '22 19:07 cameel

It looks like the remix gas estimator is actually "good" right?

PatrickAlphaC avatar Jul 07 '22 14:07 PatrickAlphaC

Ah, right. I mean, it's not estimation, it's the cost of actual execution, but yeah, the numbers from screenshots do not need fixing.

I just thought that Remix uses compiler's estimator for something but maybe I was wrong about that. I vaguely remember someone complaining about wrong estimations from Remix that in the end turned out to be compiler's estimations.

cameel avatar Jul 07 '22 15:07 cameel

Makes sense, I think people might have just confused the two. I thought remix was wrong too ahah. Yeah it looks like remix just mocks deploying the contract to do gas estimation (I think). In any case, it looks like this issue can be closed in favor of https://github.com/ethereum/solidity/issues/8920 @Aniket-Engg

Thanks for everyone's work on these!

PatrickAlphaC avatar Jul 07 '22 16:07 PatrickAlphaC