G01 No need to explicitly initialize variables with default values
If a variable is not initialized it is automatically set to the default value (0 for uint, false for bool, address(0) for address...).
Explicitly initializing it with its default value is an anti-pattern and wastes gas.
Example Instance:
uint256 extraCeiling = 0; -> uint256 extraCeiling;
G02 In some places, loop memory variables which don't change are not cached
Calling storage/memory variable which does not change, every loop iteration,
is wrong and wastes gas. In most places this is taken care of, however not everywhere.
// Retrieve array of offer items for the order in question.
OfferItem[] memory offer = advancedOrder.parameters.offer;
// Iterate over each offer item on the order.
for (uint256 j = 0; j < offer.length; ++j) {
// Retrieve the offer item.
OfferItem memory offerItem = offer[j];
instead, cache the length:
// Retrieve array of offer items for the order in question.
OfferItem[] memory offer = advancedOrder.parameters.offer;
uint256 totalOffers = offer.length
// Iterate over each offer item on the order.
for (uint256 j = 0; j < totalOffers; ++j) {
// Retrieve the offer item.
OfferItem memory offerItem = offer[j];
G01 No need to explicitly initialize variables with default values
If a variable is not initialized it is automatically set to the default value (0 for uint, false for bool, address(0) for address...). Explicitly initializing it with its default value is an anti-pattern and wastes gas.
Example Instance:
uint256 extraCeiling = 0;
->uint256 extraCeiling;
In AmountDeriver._locateCurrentAmount
This pattern is used everywhere in the protocol.
G02 In some places, loop memory variables which don't change are not cached
Calling storage/memory variable which does not change, every loop iteration, is wrong and wastes gas. In most places this is taken care of, however not everywhere.
For example, OrderCombiner.sol#L247:
instead, cache the length:
Other Instances Include (but not limited to):
OrderCombiner.sol#L291
OrderCombiner.sol#L621
OrderFulfiller.sol#L217
OrderFulfiller.sol#L306
ReferenceBasicOrderFulfiller.sol#L643
ReferenceBasicOrderFulfiller.sol#L712