Skip to content

Commit

Permalink
Use unchecked for loop increments.
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSchumacher committed Mar 28, 2024
1 parent 624ab1c commit 5b88dfe
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ abstract contract ERC20AirDropMint is ERC20 {
uint256 value
) public virtual {
uint256 len = addresses.length;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address to = addresses[i];
beforeAirDropMint(to, value);
_mint(to, value);
emit AirDropMint(to, value);
afterAirDropMint(to, value);
unchecked {
++i;
}
}
}

Expand All @@ -52,12 +55,15 @@ abstract contract ERC20AirDropMint is ERC20 {
) public virtual {
uint256 len = addresses.length;
uint256 splitValue = value / len;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address to = addresses[i];
beforeAirDropMint(to, splitValue);
_mint(to, splitValue);
emit AirDropMint(to, splitValue);
afterAirDropMint(to, splitValue);
unchecked {
++i;
}
}
}

Expand All @@ -74,14 +80,17 @@ abstract contract ERC20AirDropMint is ERC20 {
if (len != valLen) {
revert ERC20AirDropMismatch(len, valLen);
}
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address to = addresses[i];
uint256 value = values[i];

beforeAirDropMint(to, value);
_mint(to, value);
afterAirDropMint(to, value);
emit AirDropMint(to, value);
unchecked {
++i;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ abstract contract ERC20AirDropTransfer is ERC20 {
beforeAirDropTransfer();
uint256 len = addresses.length;
address sender = msg.sender;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address addr = addresses[i];
_transfer(sender, addr, value);
emit AirDropTransfer(sender, addr, value);
unchecked {
++i;
}
}
afterAirDropTransfer();
}
Expand All @@ -51,10 +54,13 @@ abstract contract ERC20AirDropTransfer is ERC20 {
uint256 len = addresses.length;
address sender = msg.sender;
uint256 splitValue = value / len;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address addr = addresses[i];
_transfer(sender, addr, splitValue);
emit AirDropTransfer(sender, addr, splitValue);
unchecked {
++i;
}
}
afterAirDropTransfer();
}
Expand All @@ -74,11 +80,14 @@ abstract contract ERC20AirDropTransfer is ERC20 {
}
address sender = msg.sender;
beforeAirDropTransfer();
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address addr = addresses[i];
uint256 value = values[i];
_transfer(sender, addr, value);
emit AirDropTransfer(sender, addr, value);
unchecked {
++i;
}
}
afterAirDropTransfer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ abstract contract ERC20ProofOfBurn is Context, ERC20 {
address[] memory eligibleBurnContracts = _burnContracts;
uint256 addressLength = _burnAddresses.length;
uint256 contractLength = _burnContracts.length;
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20 tokenContract = ERC20(eligibleBurnContracts[i]);
for (uint256 j; j < addressLength; ++j) {
for (uint256 j; j < addressLength;) {
balance += tokenContract.balanceOf(eligibleBurnAddresses[j]);
unchecked {
++j;
}
}
unchecked {
++i;
}
}
return balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ abstract contract ERC20ProofOfBurner is Context, ERC20 {
address[] memory eligibleBurnerContracts = _burnerContracts;
uint256 contractLength = eligibleBurnerContracts.length;
address sender = _msgSender();
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20BurnRegistry tokenContract =
ERC20BurnRegistry(eligibleBurnerContracts[i]);
balance += tokenContract.burnedFrom(sender);
unchecked {
++i;
}
}
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ abstract contract ERC20ProofOfMint is Context, ERC20 {
{
address[] memory eligibleMintContracts = _mintContracts;
uint256 contractLength = _mintContracts.length;
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20 tokenContract = ERC20(eligibleMintContracts[i]);
balance += tokenContract.totalSupply();
unchecked {
++i;
}
}
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ abstract contract ERC20ProofOfMinter is Context, ERC20 {
address[] memory eligibleMinterContracts = _minterContracts;
uint256 contractLength = eligibleMinterContracts.length;
address sender = _msgSender();
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20MintRegistry tokenContract =
ERC20MintRegistry(eligibleMinterContracts[i]);
balance += tokenContract.mintedBy(sender);
unchecked {
++i;
}
}
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ abstract contract ERC20AirDropMintUpgradeable is
uint256 value
) public virtual {
uint256 len = addresses.length;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address to = addresses[i];
beforeAirDropMint(to, value);
_mint(to, value);
emit AirDropMint(to, value);
afterAirDropMint(to, value);
unchecked {
++i;
}
}
}

Expand All @@ -63,12 +66,15 @@ abstract contract ERC20AirDropMintUpgradeable is
) public virtual {
uint256 len = addresses.length;
uint256 splitValue = value / len;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address to = addresses[i];
beforeAirDropMint(to, splitValue);
_mint(to, splitValue);
emit AirDropMint(to, splitValue);
afterAirDropMint(to, splitValue);
unchecked {
++i;
}
}
}

Expand All @@ -85,14 +91,17 @@ abstract contract ERC20AirDropMintUpgradeable is
if (len != valLen) {
revert ERC20AirDropMismatch(len, valLen);
}
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address to = addresses[i];
uint256 value = values[i];

beforeAirDropMint(to, value);
_mint(to, value);
afterAirDropMint(to, value);
emit AirDropMint(to, value);
unchecked {
++i;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ abstract contract ERC20AirDropTransferUpgradeable is
beforeAirDropTransfer();
uint256 len = addresses.length;
address sender = msg.sender;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address addr = addresses[i];
_transfer(sender, addr, value);
emit AirDropTransfer(sender, addr, value);
unchecked {
++i;
}
}
afterAirDropTransfer();
}
Expand All @@ -65,10 +68,13 @@ abstract contract ERC20AirDropTransferUpgradeable is
uint256 len = addresses.length;
address sender = msg.sender;
uint256 splitValue = value / len;
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address addr = addresses[i];
_transfer(sender, addr, splitValue);
emit AirDropTransfer(sender, addr, splitValue);
unchecked {
++i;
}
}
afterAirDropTransfer();
}
Expand All @@ -88,11 +94,14 @@ abstract contract ERC20AirDropTransferUpgradeable is
}
address sender = msg.sender;
beforeAirDropTransfer();
for (uint256 i; i < len; ++i) {
for (uint256 i; i < len;) {
address addr = addresses[i];
uint256 value = values[i];
_transfer(sender, addr, value);
emit AirDropTransfer(sender, addr, value);
unchecked {
++i;
}
}
afterAirDropTransfer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ abstract contract ERC20ProofOfBurnUpgradeable is
address[] memory eligibleBurnContracts = $._burnContracts;
uint256 addressLength = eligibleBurnAddresses.length;
uint256 contractLength = eligibleBurnContracts.length;
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20 tokenContract = ERC20(eligibleBurnContracts[i]);
for (uint256 j; j < addressLength; ++j) {
for (uint256 j; j < addressLength;) {
balance += tokenContract.balanceOf(eligibleBurnAddresses[j]);
unchecked {
++j;
}
}
unchecked {
++i;
}
}
return balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ abstract contract ERC20ProofOfBurnerUpgradeable is
address[] memory eligibleBurnerContracts = $._burnerContracts;
uint256 contractLength = eligibleBurnerContracts.length;
address sender = _msgSender();
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20BurnRegistryUpgradeable tokenContract =
ERC20BurnRegistryUpgradeable(eligibleBurnerContracts[i]);
balance += tokenContract.burnedFrom(sender);
unchecked {
++i;
}
}
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ abstract contract ERC20ProofOfMintUpgradeable is
ERC20ProofOfMintStorage storage $ = _getERC20ProofOfMintStorage();
address[] memory eligibleMintContracts = $._mintContracts;
uint256 contractLength = eligibleMintContracts.length;
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20Upgradeable tokenContract =
ERC20Upgradeable(eligibleMintContracts[i]);
balance += tokenContract.totalSupply();
unchecked {
++i;
}
}
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,13 @@ abstract contract ERC20ProofOfMinterUpgradeable is
address[] memory eligibleMinterContracts = $._minterContracts;
uint256 contractLength = eligibleMinterContracts.length;
address sender = _msgSender();
for (uint256 i; i < contractLength; ++i) {
for (uint256 i; i < contractLength;) {
ERC20MintRegistryUpgradeable tokenContract =
ERC20MintRegistryUpgradeable(eligibleMinterContracts[i]);
balance += tokenContract.mintedBy(sender);
unchecked {
++i;
}
}
return balance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ abstract contract MultiTokenBurnRegistry is IMultiTokenBurnRegistry {
address[] memory burners = new address[](amount);
mapping(uint256 => address) storage burnAddresses = stats.burnAddresses;

for (uint256 i = 0; i < amount; ++i) {
for (uint256 i; i < amount;) {
burners[i] = burnAddresses[i];
unchecked {
++i;
}
}
return burners;
}
Expand All @@ -72,8 +75,11 @@ abstract contract MultiTokenBurnRegistry is IMultiTokenBurnRegistry {
address[] memory burners = new address[](amount);
mapping(uint256 => address) storage burnAddresses = stats.burnAddresses;

for (uint256 i = 0; i < amount; ++i) {
for (uint256 i; i < amount;) {
burners[i] = burnAddresses[burnersLength - amount + i];
unchecked {
++i;
}
}
return burners;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ abstract contract MultiTokenBurnRegistryUpgradeable is
address[] memory burners = new address[](amount);
mapping(uint256 => address) storage burnAddresses = stats.burnAddresses;

for (uint256 i = 0; i < amount; ++i) {
for (uint256 i; i < amount;) {
burners[i] = burnAddresses[i];
unchecked {
++i;
}
}
return burners;
}
Expand All @@ -114,8 +117,11 @@ abstract contract MultiTokenBurnRegistryUpgradeable is
address[] memory burners = new address[](amount);
mapping(uint256 => address) storage burnAddresses = stats.burnAddresses;

for (uint256 i = 0; i < amount; ++i) {
for (uint256 i; i < amount;) {
burners[i] = burnAddresses[burnersLength - amount + i];
unchecked {
++i;
}
}
return burners;
}
Expand Down
Loading

0 comments on commit 5b88dfe

Please sign in to comment.