Context
I have recently introduced a new constant getter in my contract, and I have thought that that it would lead to an increase in gas costs. Below you can find those snippets of code that reproduce the scenario from my actual code repository.
V1
struct MyStruct {
address owner;
}
mapping(uint256 => MyStruct) internal myStructs;
modifier onlyOwner(uint256 id) {
if (myStructs[id].owner != msg.sender) {
revert Foo__NotAuthorized(msg.sender);
}
_;
}
V2
struct MyStruct {
address owner;
}
mapping(uint256 => MyStruct) internal myStructs;
modifier onlyOwner(uint256 id) {
if (getOwner(id) != msg.sender) {
revert Foo__NotAuthorized(msg.sender);
}
_;
}
function getOwner(uint256 id) public view returns (address theOwner) {
theOwner = myStructs[id].owner;
}
Question
There's also an additional method getBlockTimestamp
onto which I applied the onlyOwner
modifier. You can see the full code in this GitHub gist.
Now, here's the kicker. V1 and V2 cost the same amount of gas. My question is, why?
I expected V2 to cost more than V1 because it contains the same storage read, but on top of that it involves a call to the public function getOwner
. Shouldn't that translate to additional opcodes in the bytecode representation of the smart contract?
Does Solidity "inline" the getter function?