From 00ca4df594b847fd8eb3288432e5d8b0cca4ac09 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Tue, 19 Mar 2024 22:12:31 +0000 Subject: [PATCH] Try fix via-ir --- .gas-snapshot | 38 +++++++++++++++++++------------------- src/DN420.sol | 50 ++++++++++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 1146bfe..0290eea 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -67,7 +67,7 @@ BenchTest:testMintDN420_13() (gas: 111640) BenchTest:testMintDN420_14() (gas: 113114) BenchTest:testMintDN420_15() (gas: 114496) BenchTest:testMintDN420_16() (gas: 115948) -BenchTest:test__codesize() (gas: 28700) +BenchTest:test__codesize() (gas: 28709) DN404CustomUnitTest:testInitializeCorrectUnitSuccess() (gas: 129992) DN404CustomUnitTest:testInitializeWithUnitTooLargeReverts() (gas: 33824) DN404CustomUnitTest:testInitializeWithZeroUnitReverts() (gas: 13897) @@ -145,37 +145,37 @@ DN404Test:test__codesize() (gas: 57359) DN420OnlyERC20Test:testApprove() (gas: 35770) DN420OnlyERC20Test:testApprove(address,uint256) (runs: 258, μ: 30086, ~: 31321) DN420OnlyERC20Test:testBurn() (gas: 50438) -DN420OnlyERC20Test:testBurn(address,uint256,uint256) (runs: 258, μ: 51564, ~: 51639) -DN420OnlyERC20Test:testBurnInsufficientBalanceReverts(address,uint256,uint256) (runs: 258, μ: 44631, ~: 44709) +DN420OnlyERC20Test:testBurn(address,uint256,uint256) (runs: 258, μ: 51572, ~: 51639) +DN420OnlyERC20Test:testBurnInsufficientBalanceReverts(address,uint256,uint256) (runs: 258, μ: 44603, ~: 44709) DN420OnlyERC20Test:testInfiniteApproveTransferFrom() (gas: 102630) DN420OnlyERC20Test:testMaxSupplyTrick(uint256) (runs: 258, μ: 541, ~: 541) DN420OnlyERC20Test:testMetadata() (gas: 10112) DN420OnlyERC20Test:testMint() (gas: 45958) DN420OnlyERC20Test:testMintOverMaxLimitReverts() (gas: 41962) -DN420OnlyERC20Test:testMintz(address,uint256) (runs: 258, μ: 46262, ~: 46380) +DN420OnlyERC20Test:testMintz(address,uint256) (runs: 258, μ: 46356, ~: 46380) DN420OnlyERC20Test:testTransfer() (gas: 75434) -DN420OnlyERC20Test:testTransfer(address,uint256) (runs: 258, μ: 75795, ~: 75891) +DN420OnlyERC20Test:testTransfer(address,uint256) (runs: 258, μ: 75781, ~: 75891) DN420OnlyERC20Test:testTransferFrom() (gas: 85088) -DN420OnlyERC20Test:testTransferFrom(address,address,address,uint256,uint256) (runs: 258, μ: 105778, ~: 108058) +DN420OnlyERC20Test:testTransferFrom(address,address,address,uint256,uint256) (runs: 258, μ: 105790, ~: 108058) DN420OnlyERC20Test:testTransferFromInsufficientAllowanceReverts() (gas: 68658) -DN420OnlyERC20Test:testTransferFromInsufficientAllowanceReverts(address,uint256,uint256) (runs: 258, μ: 69181, ~: 69768) +DN420OnlyERC20Test:testTransferFromInsufficientAllowanceReverts(address,uint256,uint256) (runs: 258, μ: 69182, ~: 69768) DN420OnlyERC20Test:testTransferFromInsufficientBalanceReverts() (gas: 75577) -DN420OnlyERC20Test:testTransferFromInsufficientBalanceReverts(address,uint256,uint256) (runs: 258, μ: 76635, ~: 76698) +DN420OnlyERC20Test:testTransferFromInsufficientBalanceReverts(address,uint256,uint256) (runs: 258, μ: 76742, ~: 76707) DN420OnlyERC20Test:testTransferInsufficientBalanceReverts() (gas: 67140) -DN420OnlyERC20Test:testTransferInsufficientBalanceReverts(address,uint256,uint256) (runs: 258, μ: 68267, ~: 68231) +DN420OnlyERC20Test:testTransferInsufficientBalanceReverts(address,uint256,uint256) (runs: 258, μ: 68173, ~: 68236) DN420OnlyERC20Test:test__codesize() (gas: 26873) -DN420Test:testERC1155Methods(uint256) (runs: 258, μ: 4370769, ~: 4604805) -DN420Test:testERC1155MethodsSelfTransfers(uint256) (runs: 258, μ: 2527117, ~: 2684325) +DN420Test:testERC1155Methods(uint256) (runs: 258, μ: 4342587, ~: 4594887) +DN420Test:testERC1155MethodsSelfTransfers(uint256) (runs: 258, μ: 2488046, ~: 2684316) DN420Test:testFindOwnedIds() (gas: 2647505) DN420Test:testMintNext() (gas: 2124073) -DN420Test:testMintToNonERC155RecipientReverts(uint256) (runs: 258, μ: 981585, ~: 799362) -DN420Test:testMintToRevertingERC155RecipientReverts(uint256) (runs: 258, μ: 1447973, ~: 981555) -DN420Test:testMintToZeroReverts(uint256) (runs: 258, μ: 711313, ~: 664263) -DN420Test:testMixed(uint256) (runs: 258, μ: 10721971, ~: 6383788) -DN420Test:testSafeBatchTransferFromToERC1155Recipient(uint256) (runs: 258, μ: 2219189, ~: 2180542) -DN420Test:testSafeTransferFromToERC1155Recipient(uint256) (runs: 258, μ: 1748321, ~: 1474285) -DN420Test:testTransferFromToERC1155Recipient(uint256) (runs: 258, μ: 2880183, ~: 2866558) -DN420Test:testTransferMixedReverts(uint256) (runs: 258, μ: 3915640, ~: 3302456) +DN420Test:testMintToNonERC155RecipientReverts(uint256) (runs: 258, μ: 946376, ~: 799823) +DN420Test:testMintToRevertingERC155RecipientReverts(uint256) (runs: 258, μ: 1431254, ~: 981555) +DN420Test:testMintToZeroReverts(uint256) (runs: 258, μ: 744606, ~: 664230) +DN420Test:testMixed(uint256) (runs: 258, μ: 9920348, ~: 6381320) +DN420Test:testSafeBatchTransferFromToERC1155Recipient(uint256) (runs: 258, μ: 2239315, ~: 2180533) +DN420Test:testSafeTransferFromToERC1155Recipient(uint256) (runs: 258, μ: 1761508, ~: 1474285) +DN420Test:testTransferFromToERC1155Recipient(uint256) (runs: 258, μ: 2878883, ~: 2866404) +DN420Test:testTransferMixedReverts(uint256) (runs: 258, μ: 4150516, ~: 3296858) DN420Test:test__codesize() (gas: 66288) MappingsTest:testAddressPairMapSetAndGet(address[2],address[2],uint256,uint256) (runs: 258, μ: 45763, ~: 47075) MappingsTest:testBitmapSetAndGet(uint256) (runs: 258, μ: 453208, ~: 413769) diff --git a/src/DN420.sol b/src/DN420.sol index 341d8b8..6688482 100644 --- a/src/DN420.sol +++ b/src/DN420.sol @@ -823,33 +823,35 @@ abstract contract DN420 { if (!isApprovedForAll(from, by)) revert NotOwnerNorApproved(); } - uint256 upTo; uint256 amount; - AddressData storage fromAddressData = $.addressData[from]; - AddressData storage toAddressData = $.addressData[to]; unchecked { - uint256 n = ids.length; - amount = n * _unit(); - Bitmap storage fromOwned = $.owned[from]; - Bitmap storage toOwned = $.owned[to]; - while (n != 0) { - uint256 id = _get(ids, --n); - if (!_owns(fromOwned, id)) revert TransferFromIncorrectOwner(); - _set(fromOwned, id, false); - _set(toOwned, id, true); - _afterNFTTransfer(from, to, id); - upTo = _max(upTo, id); + uint256 upTo; + AddressData storage fromAddressData = $.addressData[from]; + AddressData storage toAddressData = $.addressData[to]; + { + uint256 n = ids.length; + amount = n * _unit(); + Bitmap storage fromOwned = $.owned[from]; + Bitmap storage toOwned = $.owned[to]; + while (n != 0) { + uint256 id = _get(ids, --n); + if (!_owns(fromOwned, id)) revert TransferFromIncorrectOwner(); + _set(fromOwned, id, false); + _set(toOwned, id, true); + _afterNFTTransfer(from, to, id); + upTo = _max(upTo, id); + } + } + /// @solidity memory-safe-assembly + assembly { + let diff := or(shl(160, amount), shl(128, mload(ids))) + sstore(fromAddressData.slot, sub(sload(fromAddressData.slot), diff)) + let toPacked := sload(toAddressData.slot) + let toCheckpoint := and(0xffffffff, shr(96, toPacked)) + // forgefmt: disable-next-item + sstore(toAddressData.slot, add(diff, + xor(toPacked, shl(96, mul(gt(upTo, toCheckpoint), xor(upTo, toCheckpoint)))))) } - } - /// @solidity memory-safe-assembly - assembly { - let diff := or(shl(160, amount), shl(128, mload(ids))) - sstore(fromAddressData.slot, sub(sload(fromAddressData.slot), diff)) - let toPacked := sload(toAddressData.slot) - let toCheckpoint := and(0xffffffff, shr(96, toPacked)) - // forgefmt: disable-next-item - sstore(toAddressData.slot, add(diff, - xor(toPacked, shl(96, mul(gt(upTo, toCheckpoint), xor(upTo, toCheckpoint)))))) } _batchTransferEmit(from, to, ids); /// @solidity memory-safe-assembly