Skip to content

Instantly share code, notes, and snippets.

@drgorillamd
Created March 17, 2023 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drgorillamd/d88f697a9508df0ddb205cbbfc35fe09 to your computer and use it in GitHub Desktop.
Save drgorillamd/d88f697a9508df0ddb205cbbfc35fe09 to your computer and use it in GitHub Desktop.
Quick gas comparison between multiple ways to access struct elements stored in a single word
pragma solidity 0.8.16;
// optim 200 runs
contract Test { //27038
struct StructA {
uint128 _a;
uint128 _b;
}
StructA structA;
constructor() {
structA._a = uint128(block.timestamp);
structA._b = uint128(1 hours);
}
function test() external returns (uint) {
return structA._a + structA._b;
}
}
contract Test2 { //27050
struct StructA {
uint128 _a;
uint128 _b;
}
StructA structA;
constructor() {
structA._a = uint128(block.timestamp);
structA._b = uint128(1 hours);
}
function test() external returns (uint) {
(uint128 _a, uint128 _b) = (structA._a, structA._b);
return _a + _b;
}
}
contract Test3 { //27123
struct StructA {
uint128 _a;
uint128 _b;
}
StructA structA;
constructor() {
structA._a = uint128(block.timestamp);
structA._b = uint128(1 hours);
}
function test() external returns (uint) {
StructA memory _structA = structA;
return _structA._a + _structA._b;
}
}
contract Test4 { //27047
struct StructA {
uint128 _a;
uint128 _b;
}
StructA structA;
constructor() {
structA._a = uint128(block.timestamp);
structA._b = uint128(1 hours);
}
function test() external returns (uint) {
StructA storage _structA = structA;
return _structA._a + _structA._b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment