Skip to content

Commit

Permalink
refactor(protocol): i++ to ++i to reduce 5 gas (#2033)
Browse files Browse the repository at this point in the history
* protocol: i++ to ++j to reduce 5 gas

* more
  • Loading branch information
dantaik authored Jan 3, 2023
1 parent 23ff848 commit 0cbe6db
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/libs/LibProving.sol
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ library LibProving {

bytes32 blockHash = evidence.header.hashBlockHeader();

for (uint256 i = 0; i < config.zkProofsPerBlock; i++) {
for (uint256 i = 0; i < config.zkProofsPerBlock; ++i) {
if (!config.skipProofValidation) {
LibZKP.verify({
verificationKey: ConfigManager(
Expand Down Expand Up @@ -300,7 +300,7 @@ library LibProving {
"L1:tooLate"
);

for (uint256 i = 0; i < fc.provers.length; i++) {
for (uint256 i = 0; i < fc.provers.length; ++i) {
require(fc.provers[i] != prover, "L1:prover:dup");
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L1/libs/LibVerifying.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ library LibVerifying {
TkoToken tkoToken
) private {
uint sum = 2 ** fc.provers.length - 1;
for (uint i = 0; i < fc.provers.length; i++) {
for (uint i = 0; i < fc.provers.length; ++i) {
uint weight = (1 << (fc.provers.length - i - 1));
uint proverReward = (reward * weight) / sum;

Expand Down Expand Up @@ -223,7 +223,7 @@ library LibVerifying {
function _cleanUp(TaikoData.ForkChoice storage fc) private {
fc.blockHash = 0;
fc.provenAt = 0;
for (uint i = 0; i < fc.provers.length; i++) {
for (uint i = 0; i < fc.provers.length; ++i) {
fc.provers[i] = address(0);
}
delete fc.provers;
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/L2/TaikoL2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ contract TaikoL2 is AddressResolver, ReentrancyGuard, IHeaderSync {

bytes32[255] memory ancestors;
uint256 number = block.number;
for (uint256 i = 0; i < 255 && number >= i + 2; i++) {
for (uint256 i = 0; i < 255 && number >= i + 2; ++i) {
ancestors[i] = blockhash(number - i - 2);
}

Expand Down Expand Up @@ -159,7 +159,7 @@ contract TaikoL2 is AddressResolver, ReentrancyGuard, IHeaderSync {
uint256 number = block.number;
uint256 chainId = block.chainid;

for (uint256 i = 2; i <= 256 && number >= i; i++) {
for (uint256 i = 2; i <= 256 && number >= i; ++i) {
ancestors[(number - i) % 255] = blockhash(number - i);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/libs/LibReceiptDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ library LibReceiptDecoder {
) internal pure returns (Log[] memory) {
Log[] memory logs = new Log[](logsRlp.length);

for (uint256 i = 0; i < logsRlp.length; i++) {
for (uint256 i = 0; i < logsRlp.length; ++i) {
LibRLPReader.RLPItem[] memory rlpItems = LibRLPReader.readList(
logsRlp[i]
);
Expand All @@ -79,7 +79,7 @@ library LibReceiptDecoder {
) internal pure returns (bytes32[] memory) {
bytes32[] memory topics = new bytes32[](topicsRlp.length);

for (uint256 i = 0; i < topicsRlp.length; i++) {
for (uint256 i = 0; i < topicsRlp.length; ++i) {
topics[i] = LibRLPReader.readBytes32(topicsRlp[i]);
}

Expand Down
8 changes: 4 additions & 4 deletions packages/protocol/contracts/libs/LibTxDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ library LibTxDecoder {
LibRLPReader.RLPItem[] memory txs = LibRLPReader.readList(encoded);

Tx[] memory _txList = new Tx[](txs.length);
for (uint256 i = 0; i < txs.length; i++) {
for (uint256 i = 0; i < txs.length; ++i) {
_txList[i] = decodeTx(chainId, LibRLPReader.readBytes(txs[i]));
}

Expand Down Expand Up @@ -213,7 +213,7 @@ library LibTxDecoder {
LibRLPReader.RLPItem[] memory accessListRLP
) internal pure returns (AccessItem[] memory accessList) {
accessList = new AccessItem[](accessListRLP.length);
for (uint256 i = 0; i < accessListRLP.length; i++) {
for (uint256 i = 0; i < accessListRLP.length; ++i) {
LibRLPReader.RLPItem[] memory items = LibRLPReader.readList(
accessListRLP[i]
);
Expand All @@ -222,7 +222,7 @@ library LibTxDecoder {
items[1]
);
bytes32[] memory slots = new bytes32[](slotListRLP.length);
for (uint256 j = 0; j < slotListRLP.length; j++) {
for (uint256 j = 0; j < slotListRLP.length; ++j) {
slots[j] = LibRLPReader.readBytes32(slotListRLP[j]);
}
accessList[i] = AccessItem(addr, slots);
Expand All @@ -233,7 +233,7 @@ library LibTxDecoder {
TxList memory txList
) internal pure returns (uint256 sum) {
Tx[] memory items = txList.items;
for (uint256 i = 0; i < items.length; i++) {
for (uint256 i = 0; i < items.length; ++i) {
sum += items[i].gasLimit;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/libs/LibTxUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ library LibTxUtils {
transaction.txType == 0 ? txRLPItems.length : txRLPItems.length - 3
);

for (uint256 i = 0; i < list.length; i++) {
for (uint256 i = 0; i < list.length; ++i) {
// For Non-legacy transactions, accessList is always the
// fourth to last item.
if (transaction.txType != 0 && i == list.length - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract TestLibRLPReader {
function readList(bytes memory _in) public pure returns (bytes[] memory) {
LibRLPReader.RLPItem[] memory decoded = LibRLPReader.readList(_in);
bytes[] memory out = new bytes[](decoded.length);
for (uint256 i = 0; i < out.length; i++) {
for (uint256 i = 0; i < out.length; ++i) {
out[i] = LibRLPReader.readRawBytes(decoded[i]);
}
return out;
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/contracts/thirdparty/LibBytesUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ library LibBytesUtils {
) internal pure returns (bytes memory) {
bytes memory nibbles = new bytes(_bytes.length * 2);

for (uint256 i = 0; i < _bytes.length; i++) {
for (uint256 i = 0; i < _bytes.length; ++i) {
nibbles[i * 2] = _bytes[i] >> 4;
nibbles[i * 2 + 1] = bytes1(uint8(_bytes[i]) % 16);
}
Expand All @@ -154,7 +154,7 @@ library LibBytesUtils {
) internal pure returns (bytes memory) {
bytes memory ret = new bytes(_bytes.length / 2);

for (uint256 i = 0; i < ret.length; i++) {
for (uint256 i = 0; i < ret.length; ++i) {
ret[i] = (_bytes[i * 2] << 4) | (_bytes[i * 2 + 1]);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/protocol/contracts/thirdparty/LibMerkleTrie.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ library LibMerkleTrie {
TrieNode memory currentNode;

// Proof is top-down, so we start at the first element (root).
for (uint256 i = 0; i < _proof.length; i++) {
for (uint256 i = 0; i < _proof.length; ++i) {
currentNode = _proof[i];
currentKeyIndex += currentKeyIncrement;

Expand Down Expand Up @@ -285,7 +285,7 @@ library LibMerkleTrie {
LibRLPReader.RLPItem[] memory nodes = LibRLPReader.readList(_proof);
TrieNode[] memory proof = new TrieNode[](nodes.length);

for (uint256 i = 0; i < nodes.length; i++) {
for (uint256 i = 0; i < nodes.length; ++i) {
bytes memory encoded = LibRLPReader.readBytes(nodes[i]);
proof[i] = TrieNode({
encoded: encoded,
Expand Down Expand Up @@ -355,7 +355,7 @@ library LibMerkleTrie {
) private pure returns (uint256 _shared) {
uint256 i = 0;
while (_a.length > i && _b.length > i && _a[i] == _b[i]) {
i++;
++i;
}
return i;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/thirdparty/LibRLPReader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ library LibRLPReader {
}

// Copy over as many complete words as we can.
for (uint256 i = 0; i < _length / 32; i++) {
for (uint256 i = 0; i < _length / 32; ++i) {
assembly {
mstore(dest, mload(src))
}
Expand Down
14 changes: 7 additions & 7 deletions packages/protocol/contracts/thirdparty/LibRLPWriter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ library LibRLPWriter {
uint256 lenLen;
uint256 i = 1;
while (_len / i != 0) {
lenLen++;
++lenLen;
i *= 256;
}

encoded = new bytes(lenLen + 1);
encoded[0] = bytes1(uint8(lenLen) + uint8(_offset) + 55);
for (i = 1; i <= lenLen; i++) {
for (i = 1; i <= lenLen; ++i) {
encoded[i] = bytes1(
uint8((_len / (256 ** (lenLen - i))) % 256)
);
Expand All @@ -172,14 +172,14 @@ library LibRLPWriter {
bytes memory b = abi.encodePacked(_x);

uint256 i = 0;
for (; i < 32; i++) {
for (; i < 32; ++i) {
if (b[i] != 0) {
break;
}
}

bytes memory res = new bytes(32 - i);
for (uint256 j = 0; j < res.length; j++) {
for (uint256 j = 0; j < res.length; ++j) {
res[j] = b[i++];
}

Expand All @@ -200,7 +200,7 @@ library LibRLPWriter {
uint256 i = 0;

bytes memory res = new bytes(32);
for (uint256 j = 0; j < res.length; j++) {
for (uint256 j = 0; j < res.length; ++j) {
res[j] = b[i++];
}

Expand Down Expand Up @@ -253,7 +253,7 @@ library LibRLPWriter {

uint256 len;
uint256 i = 0;
for (; i < _list.length; i++) {
for (; i < _list.length; ++i) {
len += _list[i].length;
}

Expand All @@ -263,7 +263,7 @@ library LibRLPWriter {
flattenedPtr := add(flattened, 0x20)
}

for (i = 0; i < _list.length; i++) {
for (i = 0; i < _list.length; ++i) {
bytes memory item = _list[i];

uint256 listPtr;
Expand Down

0 comments on commit 0cbe6db

Please sign in to comment.