solmate icon indicating copy to clipboard operation
solmate copied to clipboard

⚡️ STL Optimizations

Open Vectorized opened this issue 3 years ago • 6 comments

Description

Changes:

  • Assembly reverts for #157.
  • Optimized the success checks.
  • Removed redundant mstore(0x60, 0)s.

You can try copypasta the revert assembly into something like Remix and verify it.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • [x] Ran forge snapshot?
  • [x] Ran npm run lint?
  • [x] Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

Vectorized avatar May 30 '22 01:05 Vectorized

I like it. If we can't get t11s to accept custom errors, let's use assemblored reverts.

z0r0z avatar May 30 '22 16:05 z0r0z

I like it. If we can't get t11s to accept custom errors, let's use assemblored reverts.

lol i opened the issue to do reverts in asm https://github.com/Rari-Capital/solmate/issues/157

transmissions11 avatar May 30 '22 17:05 transmissions11

ok ok ok

z0r0z avatar May 30 '22 19:05 z0r0z

The test fails for ERC1155Test::testFailFuzzSafeBatchTransferInsufficientBalance for certain rare fuzz cases.

You can try the following:

function testFailFuzzSafeBatchTransferInsufficientBalanceRareCase() public {
    uint256[] memory ids = new uint256[](2);
    ids[0] = 0;
    ids[1] = 0;
    uint256[] memory mintAmounts = new uint256[](2);
    mintAmounts[0] = type(uint256).max - 2;
    mintAmounts[1] = 2;
    uint256[] memory transferAmounts = new uint256[](2);
    transferAmounts[0] = type(uint256).max - 2;
    transferAmounts[1] = 2;
    testFailFuzzSafeBatchTransferInsufficientBalance(
        address(0xABCD),
        ids, 
        mintAmounts,
        transferAmounts, 
        bytes(""), 
        bytes("")
    );
}

The fix is to simply add a continue line in the fuzz test like so:

for (uint256 i = 0; i < minLength; i++) {
    uint256 id = ids[i];

    uint256 remainingMintAmountForId = type(uint256).max - userMintAmounts[from][id];

    if (mintAmounts[i] == remainingMintAmountForId) continue; // To mitigate the rare case.

    uint256 mintAmount = bound(mintAmounts[i], 0, remainingMintAmountForId);
    uint256 transferAmount = bound(transferAmounts[i], mintAmount + 1, type(uint256).max);

    normalizedIds[i] = id;
    normalizedMintAmounts[i] = mintAmount;
    normalizedTransferAmounts[i] = transferAmount;

    userMintAmounts[from][id] += mintAmount;
}

Lemme know if you want to add this line into the fuzz test, although it is not related to the PR.

Vectorized avatar Jun 02 '22 15:06 Vectorized

Note that we need 4 mstores for the reverts, as the scratch space may be dirty from those special tricks.

Vectorized avatar Jun 07 '22 23:06 Vectorized

This is unreadable

sambacha avatar Jul 04 '22 04:07 sambacha