Skip to content

Instantly share code, notes, and snippets.

@IllIllI000
Last active September 5, 2023 16:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IllIllI000/ec23a57daa30a8f8ca8b9681c8ccefb0 to your computer and use it in GitHub Desktop.
Save IllIllI000/ec23a57daa30a8f8ca8b9681c8ccefb0 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
/**
* @title GasTestStorageInMapping
* @author IllIllI
*/
contract GasTestStorageInMapping {
struct Foo {
uint x;
uint a;
uint y;
uint b;
uint z;
}
mapping(uint=>Foo) data;
constructor() {
data[100] = Foo({x:0,a:1,y:0,b:2,z:0});
}
// 25988 gas (optimizer 200), 26590 gas (optimizer off)
function bad_6eP(uint idx) external view returns (uint){
uint a = data[idx].a;
a *= 2;
uint b = data[idx].b;
return a + b;
}
// 25946 gas (optimizer 200), 26547 gas (optimizer off)
function good_H7A(uint idx) external view returns (uint) {
Foo storage f = data[idx];
uint a = f.a;
a *= 2;
uint b = f.b;
return a + b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment