storageのstruct型配列とpush時のgasの関係
・packingとは別問題で、uint8はuint32に比べて微妙にgasが多く掛かるらしい(圧縮のためだとか)。
参考
https://www.bitdegree.org/learn/solidity-layout/
https://gitter.im/ethereum/solidity?at=5a1454772837ee5106af8fbb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
//Write your own contracts here. Currently compiles using solc v0.4.15+commit.bbb8e64f. pragma solidity ^0.4.18; contract SimpleBirdsStore { /** structs */ struct GoodBird { uint128 gene; uint32 sireId; uint32 matronId; uint32 cooldownEndTime; uint16 cooldownIndex; uint16 generation; } struct BadBird { uint128 gene; uint32 sireId; uint32 matronId; uint32 cooldownEndTime; uint16 cooldownIndex; uint32 generation; } struct TestBird { uint128 gene; uint32 sireId; uint32 matronId; uint16 generation; uint16 cooldownIndex; uint32 cooldownEndTime; } struct VeryBadBird { uint128 gene; uint32 sireId; uint32 matronId; uint16 generation; uint16 cooldownIndex; uint32 cooldownEndTime; uint32 cooldownEndTime2; } struct HeavyBird { uint128 gene; uint32 sireId; uint32 matronId; uint32 generation; uint32 cooldownIndex; uint32 cooldownEndTime; } struct VeryHeavyBird { uint128 gene; uint32 sireId; uint32 matronId; uint32 generation; uint32 cooldownIndex; uint32 cooldownEndTime; uint32 cooldownEndTime2; } GoodBird[] public goodBirds; BadBird[] public badBirds; TestBird[] public testBirds; VeryBadBird[] public veryBadBirds; HeavyBird[] public heavyBirds; VeryHeavyBird[] public veryHeavyBirds; event AddGoodBird(address indexed msgSender, uint indexed id); event AddBadBird(address indexed msgSender, uint indexed id); event AddTestBird(address indexed msgSender, uint indexed id); event AddVeryBadBird(address indexed msgSender, uint indexed id); event AddHeavyBird(address indexed msgSender, uint indexed id); event AddVeryHeavyBird(address indexed msgSender, uint indexed id); function addGoodBird() public { uint id = goodBirds.push( GoodBird(0, 0, 0, 0, 0, 0) ) - 1; AddGoodBird(msg.sender, id); } function addBadBird() public { uint id = badBirds.push( BadBird(0, 0, 0, 0, 0, 0) ) - 1; AddBadBird(msg.sender, id); } function addTestBird() public { uint id = testBirds.push( TestBird(0, 0, 0, 0, 0, 0) ) - 1; AddTestBird(msg.sender, id); } function addVeryBadBird() public { uint id = veryBadBirds.push( VeryBadBird(0, 0, 0, 0, 0, 0, 0) ) - 1; AddVeryBadBird(msg.sender, id); } function addHeavyBird() public { uint id = heavyBirds.push( HeavyBird(0, 0, 0, 0, 0, 0) ) - 1; AddHeavyBird(msg.sender, id); } function addVeryHeavyBird() public { uint id = veryHeavyBirds.push( VeryHeavyBird(0, 0, 0, 0, 0, 0, 0) ) - 1; AddVeryHeavyBird(msg.sender, id); } } |