code-423n4 / 2022-05-opensea-seaport-findings

1 stars 0 forks source link

Gas Optimizations #83

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Optimizations

To prevent repeated code revision reverting, the following gas optimizations are done cumulatively, meaning each one adds to the last's codebase. These are also from running forge test --gas-report with a custom Foundry profile that limits fuzz runs to 100 and disables the IR pipeline.

1 - Use shr instead of dividing by 4

Replace the line here with:

route := shr(2, calldataload(BasicOrder_basicOrderType_cdPtr))

Saves on average 3 gas per fulfillBasicOrder() call according to tests. Saves 1 gas on median.

Before: fulfillBasicOrder ┆ 753 ┆ 578873 ┆ 214735 ┆ 3437148
After: fulfillBasicOrder ┆ 751 ┆ 578870 ┆ 214734 ┆ 3437146

2 - Short-Circuit a reverting inequality check

Replace the block starting here, copied below:

if (amount != 1) {
    revert InvalidERC721TransferAmount();
}

// Perform transfer via the token contract directly.
_performERC721Transfer(token, from, to, identifier);

With the following:

if (amount == 1) {
    // Perform transfer via the token contract directly.
    _performERC721Transfer(token, from, to, identifier);
} else {
    revert InvalidERC721TransferAmount();
}       

This again saves extremely small amounts of gas on fulfillBasicOrder(), but hey, it's something.

Before: fulfillBasicOrder ┆ 751 ┆ 578870 ┆ 214734 ┆ 3437146
After: fulfillBasicOrder ┆ 751 ┆ 578870 ┆ 214733 ┆ 3437145

HardlyDifficult commented 2 years ago

These are extremely small savings, but seem like valid suggestions that could be considered.