가스비 최적화
예시) getAmountsOut 함수
function getAmountsOut(
uint256 amountIn,
address[] calldata path
) public view returns (uint256[] memory amounts) {
amounts = new uint256[](path.length);
assembly {
// to skip unnecessary checks
mstore(add(amounts, 0x20), amountIn)
}
for (uint256 i; i < path.length - 1; ) {
address path_i;
address path_i_plus_1;
assembly {
// to skip range check
path_i := calldataload(add(path.offset, mul(i, 0x20)))
path_i_plus_1 := calldataload(
add(path.offset, mul(add(i, 1), 0x20))
)
}
// ...
{
assembly {
let _amountIn := mload(
add(add(amounts, 0x20), mul(i, 0x20))
)
mstore(
// to skip arithmetic checks — core implementation does checking them
add(add(amounts, 0x20), mul(add(i, 1), 0x20)),
div(
mul(mul(_amountIn, reserve1), 9975),
add(mul(reserve0, 10000), mul(_amountIn, 9975))
)
)
}
}
// ...
// to skip overflow check
unchecked { i++; }
}
}
Last updated