Skip to content

Commit

Permalink
Remove variable assignment for constant access in assembly (#722)
Browse files Browse the repository at this point in the history
* Remove variable assignment for constant access in assembly (#719)

* refactor: Remove unnecessary gas cost incurred when loading a constant to a variable

* Updata gas snap data

* Formatting

* refactor: Remove unnecessary parenthesis

* regenerate gas snapshots

---------

Co-authored-by: HC <97586838+havi-kim@users.noreply.github.com>
  • Loading branch information
gretzke and havi-kim authored May 28, 2024
1 parent 35edd8e commit ad4cb38
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
11 changes: 4 additions & 7 deletions src/libraries/Lock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@ import {IHooks} from "../interfaces/IHooks.sol";
/// TODO: This library can be deleted when we have the transient keyword support in solidity.
library Lock {
// The slot holding the unlocked state, transiently. uint256(keccak256("Unlocked")) - 1;
uint256 constant IS_UNLOCKED_SLOT = uint256(0xc090fc4683624cfc3884e9d8de5eca132f2d0ec062aff75d43c0465d5ceeab23);
uint256 constant IS_UNLOCKED_SLOT = 0xc090fc4683624cfc3884e9d8de5eca132f2d0ec062aff75d43c0465d5ceeab23;

function unlock() internal {
uint256 slot = IS_UNLOCKED_SLOT;
assembly {
// unlock
tstore(slot, true)
tstore(IS_UNLOCKED_SLOT, true)
}
}

function lock() internal {
uint256 slot = IS_UNLOCKED_SLOT;
assembly {
tstore(slot, false)
tstore(IS_UNLOCKED_SLOT, false)
}
}

function isUnlocked() internal view returns (bool unlocked) {
uint256 slot = IS_UNLOCKED_SLOT;
assembly {
unlocked := tload(slot)
unlocked := tload(IS_UNLOCKED_SLOT)
}
}
}
16 changes: 6 additions & 10 deletions src/libraries/NonZeroDeltaCount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,29 @@ import {IHooks} from "../interfaces/IHooks.sol";
/// TODO: This library can be deleted when we have the transient keyword support in solidity.
library NonZeroDeltaCount {
// The slot holding the number of nonzero deltas. uint256(keccak256("NonzeroDeltaCount")) - 1
uint256 constant NONZERO_DELTA_COUNT_SLOT =
uint256(0x7d4b3164c6e45b97e7d87b7125a44c5828d005af88f9d751cfd78729c5d99a0b);
uint256 constant NONZERO_DELTA_COUNT_SLOT = 0x7d4b3164c6e45b97e7d87b7125a44c5828d005af88f9d751cfd78729c5d99a0b;

function read() internal view returns (uint256 count) {
uint256 slot = NONZERO_DELTA_COUNT_SLOT;
assembly {
count := tload(slot)
count := tload(NONZERO_DELTA_COUNT_SLOT)
}
}

function increment() internal {
uint256 slot = NONZERO_DELTA_COUNT_SLOT;
assembly {
let count := tload(slot)
let count := tload(NONZERO_DELTA_COUNT_SLOT)
count := add(count, 1)
tstore(slot, count)
tstore(NONZERO_DELTA_COUNT_SLOT, count)
}
}

/// @notice Potential to underflow.
/// Current usage ensures this will not happen because we call decrement with known boundaries (only up to the number of times we call increment).
function decrement() internal {
uint256 slot = NONZERO_DELTA_COUNT_SLOT;
assembly {
let count := tload(slot)
let count := tload(NONZERO_DELTA_COUNT_SLOT)
count := sub(count, 1)
tstore(slot, count)
tstore(NONZERO_DELTA_COUNT_SLOT, count)
}
}
}
5 changes: 2 additions & 3 deletions src/libraries/Reserves.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ library Reserves {
using CustomRevert for bytes4;

/// uint256(keccak256("ReservesOf")) - 1
uint256 constant RESERVES_OF_SLOT = uint256(0x1e0745a7db1623981f0b2a5d4232364c00787266eb75ad546f190e6cebe9bd95);
uint256 constant RESERVES_OF_SLOT = 0x1e0745a7db1623981f0b2a5d4232364c00787266eb75ad546f190e6cebe9bd95;
/// @notice The transient reserves for pools with no balance is set to the max as a sentinel to track that it has been synced.
uint256 public constant ZERO_BALANCE = type(uint256).max;

Expand All @@ -33,9 +33,8 @@ library Reserves {
}

function _getKey(Currency currency) private pure returns (bytes32 key) {
uint256 slot = RESERVES_OF_SLOT;
assembly ("memory-safe") {
mstore(0, slot)
mstore(0, RESERVES_OF_SLOT)
mstore(32, currency)
key := keccak256(0, 64)
}
Expand Down
3 changes: 1 addition & 2 deletions src/libraries/TransientStateLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ library TransientStateLibrary {
/// @dev returns 0 if the reserves are not synced
/// @dev returns type(uint256).max if the reserves are synced but the value is 0
function getReserves(IPoolManager manager, Currency currency) internal view returns (uint256) {
bytes32 slot = RESERVES_OF_SLOT;
bytes32 key;
assembly {
mstore(0, slot)
mstore(0, RESERVES_OF_SLOT)
mstore(32, currency)
key := keccak256(0, 64)
}
Expand Down

0 comments on commit ad4cb38

Please sign in to comment.