Understanding Gas Consumption

i fixed this by replacing the line of code where the error happens with

entityStructs.push(newEntity);
return entityStructs.length - 1

it functions the same way but for some reason solidity now accepts it this way and not the other.

1 Like

I believe the Public vs External video on Gas Consumption isn’t entirely accurate anymore. Prior to Solidity 0.6.9 being released, its true that Gas Consumption for ‘external’ visibility functions could be lower because at the time only ‘external’ functions allowed for the use of calldata in function parameters. However since then ‘public’ functions are also allowed the same privilege. Hence the use of ‘external’ functions with ‘calldata’ parameters is no more a huge gas saving tactic as ‘public’ functions afford you the same costs of execution.

I guess now it can be said simply that using ‘calldata’ over ‘memory’ saves significant gas costs as long as it fits your use case :slight_smile:

1 Like

Hi,

I don’t really understand why in the video, MEMORY => MEMORY is a REFERENCE ???
I tried to verify but for me, it is a COPY.

Can you explain me why is a REFERENCE ?

Here what I tried

pragma solidity 0.7.5;
contract CopyReference{
    function memoryReference(string memory name) public pure returns ( bool ) {
        string memory before = name;
        before="toto";
        // name="tata";
        if (keccak256(abi.encodePacked(before)) == keccak256(abi.encodePacked(name))){
            return true;
        }
        return false; // It return FALSE, even with name = "tata"
    }
}
2 Likes

Check here: https://docs.soliditylang.org/en/latest/introduction-to-smart-contracts.html?highlight=memory#storage-memory-and-the-stack

Carlos Z

1 Like

I see, I read some article on internet, they said also that assign value from memory to memory is a reference. So why in the code below, it’s not the same value ?

Assigning “toto” to before property doesn’t change the value of name property.

pragma solidity 0.7.5;
contract CopyReference{
    function memoryReference(string memory name) public pure returns ( bool ) {
        string memory before = name;
        before="toto";
        // name="tata";
        if (keccak256(abi.encodePacked(before)) == keccak256(abi.encodePacked(name))){
            return true;
        }
        return false; // It return FALSE, even with name = "tata"
    }
}
1 Like