Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ ERC1155: Optimize Loops #406

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 43 additions & 43 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,34 @@ DSTestPlusTest:testFailBoundMinBiggerThanMax(uint256,uint256,uint256) (runs: 256
DSTestPlusTest:testRelApproxEqBothZeroesPasses() (gas: 425)
ERC1155Test:testApproveAll() (gas: 31009)
ERC1155Test:testApproveAll(address,bool) (runs: 256, μ: 22771, ~: 31309)
ERC1155Test:testBatchBalanceOf() (gas: 157631)
ERC1155Test:testBatchBalanceOf(address[],uint256[],uint256[],bytes) (runs: 256, μ: 3564574, ~: 2943600)
ERC1155Test:testBatchBurn() (gas: 151074)
ERC1155Test:testBatchBurn(address,uint256[],uint256[],uint256[],bytes) (runs: 256, μ: 3627558, ~: 3049938)
ERC1155Test:testBatchMintToEOA() (gas: 137337)
ERC1155Test:testBatchMintToEOA(address,uint256[],uint256[],bytes) (runs: 256, μ: 3072824, ~: 2525648)
ERC1155Test:testBatchMintToERC1155Recipient() (gas: 995703)
ERC1155Test:testBatchMintToERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 7395823, ~: 6396323)
ERC1155Test:testBatchBalanceOf() (gas: 157613)
ERC1155Test:testBatchBalanceOf(address[],uint256[],uint256[],bytes) (runs: 256, μ: 3564536, ~: 2943441)
ERC1155Test:testBatchBurn() (gas: 151038)
ERC1155Test:testBatchBurn(address,uint256[],uint256[],uint256[],bytes) (runs: 256, μ: 3626781, ~: 3049611)
ERC1155Test:testBatchMintToEOA() (gas: 137319)
ERC1155Test:testBatchMintToEOA(address,uint256[],uint256[],bytes) (runs: 256, μ: 3095970, ~: 2548895)
ERC1155Test:testBatchMintToERC1155Recipient() (gas: 995685)
ERC1155Test:testBatchMintToERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 7395580, ~: 6450545)
ERC1155Test:testBurn() (gas: 38598)
ERC1155Test:testBurn(address,uint256,uint256,bytes,uint256) (runs: 256, μ: 39631, ~: 42098)
ERC1155Test:testBurn(address,uint256,uint256,bytes,uint256) (runs: 256, μ: 40113, ~: 42098)
ERC1155Test:testFailBalanceOfBatchWithArrayMismatch() (gas: 7933)
ERC1155Test:testFailBalanceOfBatchWithArrayMismatch(address[],uint256[]) (runs: 256, μ: 58022, ~: 58788)
ERC1155Test:testFailBatchBurnInsufficientBalance() (gas: 136156)
ERC1155Test:testFailBatchBurnInsufficientBalance(address,uint256[],uint256[],uint256[],bytes) (runs: 256, μ: 1401354, ~: 590599)
ERC1155Test:testFailBatchBurnWithArrayLengthMismatch() (gas: 135542)
ERC1155Test:testFailBatchBurnWithArrayLengthMismatch(address,uint256[],uint256[],uint256[],bytes) (runs: 256, μ: 86139, ~: 76411)
ERC1155Test:testFailBatchMintToNonERC1155Recipient() (gas: 167292)
ERC1155Test:testFailBatchMintToNonERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 3190100, ~: 2673077)
ERC1155Test:testFailBatchMintToRevertingERC1155Recipient() (gas: 358811)
ERC1155Test:testFailBatchMintToRevertingERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 3381638, ~: 2864613)
ERC1155Test:testFailBatchMintToWrongReturnDataERC1155Recipient() (gas: 310743)
ERC1155Test:testFailBatchMintToWrongReturnDataERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 3333596, ~: 2816572)
ERC1155Test:testFailBatchMintToZero() (gas: 131737)
ERC1155Test:testFailBatchMintToZero(uint256[],uint256[],bytes) (runs: 256, μ: 3130600, ~: 2612336)
ERC1155Test:testFailBatchBurnInsufficientBalance() (gas: 136135)
ERC1155Test:testFailBatchBurnInsufficientBalance(address,uint256[],uint256[],uint256[],bytes) (runs: 256, μ: 1400967, ~: 590599)
ERC1155Test:testFailBatchBurnWithArrayLengthMismatch() (gas: 135524)
ERC1155Test:testFailBatchBurnWithArrayLengthMismatch(address,uint256[],uint256[],uint256[],bytes) (runs: 256, μ: 86138, ~: 76411)
ERC1155Test:testFailBatchMintToNonERC1155Recipient() (gas: 167274)
ERC1155Test:testFailBatchMintToNonERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 3189236, ~: 2672822)
ERC1155Test:testFailBatchMintToRevertingERC1155Recipient() (gas: 358793)
ERC1155Test:testFailBatchMintToRevertingERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 3380774, ~: 2864358)
ERC1155Test:testFailBatchMintToWrongReturnDataERC1155Recipient() (gas: 310725)
ERC1155Test:testFailBatchMintToWrongReturnDataERC1155Recipient(uint256[],uint256[],bytes) (runs: 256, μ: 3332732, ~: 2816317)
ERC1155Test:testFailBatchMintToZero() (gas: 131719)
ERC1155Test:testFailBatchMintToZero(uint256[],uint256[],bytes) (runs: 256, μ: 3129736, ~: 2612081)
ERC1155Test:testFailBatchMintWithArrayMismatch() (gas: 9600)
ERC1155Test:testFailBatchMintWithArrayMismatch(address,uint256[],uint256[],bytes) (runs: 256, μ: 66450, ~: 66511)
ERC1155Test:testFailBurnInsufficientBalance() (gas: 34852)
ERC1155Test:testFailBurnInsufficientBalance(address,uint256,uint256,uint256,bytes) (runs: 256, μ: 35106, ~: 38209)
ERC1155Test:testFailBurnInsufficientBalance(address,uint256,uint256,uint256,bytes) (runs: 256, μ: 35189, ~: 38209)
ERC1155Test:testFailMintToNonERC155Recipient() (gas: 68191)
ERC1155Test:testFailMintToNonERC155Recipient(uint256,uint256,bytes) (runs: 256, μ: 68507, ~: 69197)
ERC1155Test:testFailMintToRevertingERC155Recipient() (gas: 259435)
Expand All @@ -78,18 +78,18 @@ ERC1155Test:testFailMintToWrongReturnDataERC155Recipient() (gas: 259389)
ERC1155Test:testFailMintToWrongReturnDataERC155Recipient(uint256,uint256,bytes) (runs: 256, μ: 259706, ~: 260397)
ERC1155Test:testFailMintToZero() (gas: 33705)
ERC1155Test:testFailMintToZero(uint256,uint256,bytes) (runs: 256, μ: 33815, ~: 34546)
ERC1155Test:testFailSafeBatchTransferFromToNonERC1155Recipient() (gas: 321377)
ERC1155Test:testFailSafeBatchTransferFromToNonERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3541141, ~: 2963551)
ERC1155Test:testFailSafeBatchTransferFromToRevertingERC1155Recipient() (gas: 512956)
ERC1155Test:testFailSafeBatchTransferFromToRevertingERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3732678, ~: 3155082)
ERC1155Test:testFailSafeBatchTransferFromToWrongReturnDataERC1155Recipient() (gas: 464847)
ERC1155Test:testFailSafeBatchTransferFromToWrongReturnDataERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3684595, ~: 3107003)
ERC1155Test:testFailSafeBatchTransferFromToZero() (gas: 286556)
ERC1155Test:testFailSafeBatchTransferFromToZero(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3505960, ~: 2928396)
ERC1155Test:testFailSafeBatchTransferFromWithArrayLengthMismatch() (gas: 162674)
ERC1155Test:testFailSafeBatchTransferFromToNonERC1155Recipient() (gas: 321341)
ERC1155Test:testFailSafeBatchTransferFromToNonERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3538399, ~: 2963215)
ERC1155Test:testFailSafeBatchTransferFromToRevertingERC1155Recipient() (gas: 512920)
ERC1155Test:testFailSafeBatchTransferFromToRevertingERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3729936, ~: 3154746)
ERC1155Test:testFailSafeBatchTransferFromToWrongReturnDataERC1155Recipient() (gas: 464811)
ERC1155Test:testFailSafeBatchTransferFromToWrongReturnDataERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3681853, ~: 3106667)
ERC1155Test:testFailSafeBatchTransferFromToZero() (gas: 286520)
ERC1155Test:testFailSafeBatchTransferFromToZero(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 3503452, ~: 2928060)
ERC1155Test:testFailSafeBatchTransferFromWithArrayLengthMismatch() (gas: 162656)
ERC1155Test:testFailSafeBatchTransferFromWithArrayLengthMismatch(address,uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 79144, ~: 76711)
ERC1155Test:testFailSafeBatchTransferInsufficientBalance() (gas: 163555)
ERC1155Test:testFailSafeBatchTransferInsufficientBalance(address,uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 1808833, ~: 648144)
ERC1155Test:testFailSafeBatchTransferInsufficientBalance() (gas: 163534)
ERC1155Test:testFailSafeBatchTransferInsufficientBalance(address,uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 1809161, ~: 648121)
ERC1155Test:testFailSafeTransferFromInsufficientBalance() (gas: 63245)
ERC1155Test:testFailSafeTransferFromInsufficientBalance(address,uint256,uint256,uint256,bytes,bytes) (runs: 256, μ: 62944, ~: 67391)
ERC1155Test:testFailSafeTransferFromSelfInsufficientBalance() (gas: 34297)
Expand All @@ -101,21 +101,21 @@ ERC1155Test:testFailSafeTransferFromToRevertingERC1155Recipient(uint256,uint256,
ERC1155Test:testFailSafeTransferFromToWrongReturnDataERC1155Recipient() (gas: 239587)
ERC1155Test:testFailSafeTransferFromToWrongReturnDataERC1155Recipient(uint256,uint256,uint256,bytes,bytes) (runs: 256, μ: 239707, ~: 243598)
ERC1155Test:testFailSafeTransferFromToZero() (gas: 62014)
ERC1155Test:testFailSafeTransferFromToZero(uint256,uint256,uint256,bytes,bytes) (runs: 256, μ: 62068, ~: 66037)
ERC1155Test:testFailSafeTransferFromToZero(uint256,uint256,uint256,bytes,bytes) (runs: 256, μ: 62146, ~: 66037)
ERC1155Test:testMintToEOA() (gas: 34765)
ERC1155Test:testMintToEOA(address,uint256,uint256,bytes) (runs: 256, μ: 35273, ~: 35907)
ERC1155Test:testMintToEOA(address,uint256,uint256,bytes) (runs: 256, μ: 35284, ~: 35907)
ERC1155Test:testMintToERC1155Recipient() (gas: 661411)
ERC1155Test:testMintToERC1155Recipient(uint256,uint256,bytes) (runs: 256, μ: 691094, ~: 684374)
ERC1155Test:testSafeBatchTransferFromToEOA() (gas: 297822)
ERC1155Test:testSafeBatchTransferFromToEOA(address,uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 5017684, ~: 4010502)
ERC1155Test:testSafeBatchTransferFromToERC1155Recipient() (gas: 1175327)
ERC1155Test:testSafeBatchTransferFromToERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 7759463, ~: 6618414)
ERC1155Test:testSafeBatchTransferFromToEOA() (gas: 297786)
ERC1155Test:testSafeBatchTransferFromToEOA(address,uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 5018368, ~: 4010169)
ERC1155Test:testSafeBatchTransferFromToERC1155Recipient() (gas: 1175291)
ERC1155Test:testSafeBatchTransferFromToERC1155Recipient(uint256[],uint256[],uint256[],bytes,bytes) (runs: 256, μ: 7756106, ~: 6618111)
ERC1155Test:testSafeTransferFromSelf() (gas: 64177)
ERC1155Test:testSafeTransferFromSelf(uint256,uint256,bytes,uint256,address,bytes) (runs: 256, μ: 64681, ~: 68564)
ERC1155Test:testSafeTransferFromSelf(uint256,uint256,bytes,uint256,address,bytes) (runs: 256, μ: 65206, ~: 68564)
ERC1155Test:testSafeTransferFromToEOA() (gas: 93167)
ERC1155Test:testSafeTransferFromToEOA(uint256,uint256,bytes,uint256,address,bytes) (runs: 256, μ: 93963, ~: 97450)
ERC1155Test:testSafeTransferFromToEOA(uint256,uint256,bytes,uint256,address,bytes) (runs: 256, μ: 93520, ~: 97450)
ERC1155Test:testSafeTransferFromToERC1155Recipient() (gas: 739583)
ERC1155Test:testSafeTransferFromToERC1155Recipient(uint256,uint256,bytes,uint256,bytes) (runs: 256, μ: 769591, ~: 765729)
ERC1155Test:testSafeTransferFromToERC1155Recipient(uint256,uint256,bytes,uint256,bytes) (runs: 256, μ: 769280, ~: 765729)
ERC20Invariants:invariantBalanceSum() (runs: 256, calls: 3840, reverts: 2349)
ERC20Test:invariantMetadata() (runs: 256, calls: 3840, reverts: 2566)
ERC20Test:testApprove() (gas: 31058)
Expand Down Expand Up @@ -565,4 +565,4 @@ WETHTest:testFallbackDeposit() (gas: 63249)
WETHTest:testFallbackDeposit(uint256) (runs: 256, μ: 62879, ~: 65604)
WETHTest:testPartialWithdraw() (gas: 73281)
WETHTest:testWithdraw() (gas: 54360)
WETHTest:testWithdraw(uint256,uint256) (runs: 256, μ: 75313, ~: 78076)
WETHTest:testWithdraw(uint256,uint256) (runs: 256, μ: 75313, ~: 78076)
8 changes: 4 additions & 4 deletions src/tokens/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ abstract contract ERC1155 {
uint256 id;
uint256 amount;

for (uint256 i = 0; i < ids.length; ) {
for (uint256 i = 0; i != ids.length; ) {
id = ids[i];
amount = amounts[i];

Expand Down Expand Up @@ -128,7 +128,7 @@ abstract contract ERC1155 {
// Unchecked because the only math done is incrementing
// the array index counter which cannot possibly overflow.
unchecked {
for (uint256 i = 0; i < owners.length; ++i) {
for (uint256 i = 0; i != owners.length; ++i) {
balances[i] = balanceOf[owners[i]][ids[i]];
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ abstract contract ERC1155 {

require(idsLength == amounts.length, "LENGTH_MISMATCH");

for (uint256 i = 0; i < idsLength; ) {
for (uint256 i = 0; i != idsLength; ) {
balanceOf[to][ids[i]] += amounts[i];

// An array can't have a total length
Expand Down Expand Up @@ -208,7 +208,7 @@ abstract contract ERC1155 {

require(idsLength == amounts.length, "LENGTH_MISMATCH");

for (uint256 i = 0; i < idsLength; ) {
for (uint256 i = 0; i != idsLength; ) {
balanceOf[from][ids[i]] -= amounts[i];

// An array can't have a total length
Expand Down
Loading