Skip to content

Instantly share code, notes, and snippets.

Created January 25, 2018 06:35
Show Gist options
  • Save anonymous/5e852e938aff5901fcb2c191f30fd5a7 to your computer and use it in GitHub Desktop.
Save anonymous/5e852e938aff5901fcb2c191f30fd5a7 to your computer and use it in GitHub Desktop.
Created using browser-solidity: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.19+commit.c4cbbb05.js&optimize=false&gist=
pragma solidity ^0.4.18;
contract Test {
uint8 x;
function get() public view returns (uint8) {
return x;
}
function set(uint8 _x) public {
x = _x;
}
}
pragma solidity ^0.4.18;
contract TestArray {
uint128[100] public foos;
uint128[100] public bars;
// * 40942
// * 10962
function set(uint idx, uint128 _foo, uint128 _bar) public {
foos[idx] = _foo;
bars[idx] = _bar;
}
}
pragma solidity ^0.4.18;
contract TestBytes {
struct Test {
uint128 foo;
uint128 bar;
}
bytes32[100] public testBytes;
// * 21348
// * 6348
function set(uint idx, uint128 _foo, uint128 _bar) public {
Test memory test = fromBytes(testBytes[idx]);
test.foo = _foo;
test.bar = _bar;
testBytes[idx] = toBytes(test);
}
function toBytes(Test test) internal pure returns (bytes32) {
bytes32 foo = bytes32(test.foo) << 128;
bytes32 bar = bytes32(test.bar);
return foo | bar;
}
function fromBytes(bytes32 bs) internal pure returns (Test) {
bytes16 fooBytes = bytes16(bs);
bytes16 barBytes = bytes16(bs << 128);
uint128 foo = uint128(fooBytes);
uint128 bar = uint128(barBytes);
return Test(foo, bar);
}
function getFoo(uint idx) public view returns (uint128) {
return fromBytes(testBytes[idx]).foo;
}
function getBar(uint idx) public view returns (uint128) {
return fromBytes(testBytes[idx]).bar;
}
}
pragma solidity ^0.4.18;
contract TestStruct1 {
struct Test {
uint128 foo;
uint128 bar;
}
Test[100] public tests;
// * 25894
// * 10894
function set(uint idx, uint128 _foo, uint128 _bar) public {
tests[idx].foo = _foo;
tests[idx].bar = _bar;
}
}
pragma solidity ^0.4.18;
contract TestStruct2 {
struct Test {
uint128 foo;
uint128 bar;
}
Test[100] public tests;
// * 26741
// * 11741
function set(uint idx, uint128 _foo, uint128 _bar) public {
Test memory test = tests[idx];
test.foo = _foo;
test.bar = _bar;
tests[idx] = test;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment