-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: sloads and increments in assembly for gas optimization. #179 #180
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,14 +26,16 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { | |
function createSwap(Swap calldata swap) public returns (uint256) { | ||
if (swap.owner != msg.sender) revert InvalidAddress(msg.sender); | ||
|
||
uint256 swapId; | ||
|
||
unchecked { | ||
assembly { | ||
sstore(_totalSwaps.slot, add(sload(_totalSwaps.slot), 1)) | ||
|
||
swapId := sload(_totalSwaps.slot) | ||
} | ||
} | ||
|
||
uint256 swapId = _totalSwaps; | ||
|
||
_swaps[swapId] = swap; | ||
|
||
emit SwapCreated(swapId); | ||
|
@@ -65,7 +67,13 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { | |
assets[i].amountOrId | ||
); | ||
unchecked { | ||
i++; | ||
assembly { | ||
i := mload(0x40) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove line 72 |
||
i := add(i, 1) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove line 74 |
||
mstore(0x40, i) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -78,7 +86,13 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { | |
assets[i].amountOrId | ||
); | ||
unchecked { | ||
i++; | ||
assembly { | ||
i := mload(0x40) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove line 91 |
||
i := add(i, 1) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove line 93 |
||
mstore(0x40, i) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -126,6 +140,14 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { | |
* @dev Getter function for _totalSwaps. | ||
*/ | ||
function totalSwaps() public view returns (uint256) { | ||
return _totalSwaps; | ||
uint256 swapId; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. totalSwaps is never called on-chain, therefore there is no gas optm., we can keep the original way to preserve legibility |
||
unchecked { | ||
assembly { | ||
swapId := sload(_totalSwaps.slot) | ||
} | ||
} | ||
|
||
return swapId; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't change gas usage, we can use the previous way for better legibility