Skip to content

Commit

Permalink
Optimise actions encoding (#234)
Browse files Browse the repository at this point in the history
* Optimise actions encoding

* PR comment
  • Loading branch information
hensha256 authored Aug 1, 2024
1 parent e77f698 commit c77742c
Show file tree
Hide file tree
Showing 53 changed files with 62 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BaseActionsRouter_mock10commands.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
62960
61756
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
46761
46767
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_empty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
46579
46584
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_nonEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129621
129412
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_burn_nonEmpty_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
122543
122334
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149718
149447
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140870
140599
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149718
149447
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decreaseLiquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115261
115032
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108171
107954
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133487
133172
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126226
125911
Original file line number Diff line number Diff line change
@@ -1 +1 @@
127977
127706
Original file line number Diff line number Diff line change
@@ -1 +1 @@
151197
150926
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132997
132726
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133924
133653
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170080
169809
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
371232
370961
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
335932
335661
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_nativeWithSweep.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
344562
344170
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
313914
313643
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
314556
314285
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
240138
239867
Original file line number Diff line number Diff line change
@@ -1 +1 @@
369400
368863
Original file line number Diff line number Diff line change
@@ -1 +1 @@
319932
319661
Original file line number Diff line number Diff line change
@@ -1 +1 @@
415608
415324
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_Bytecode.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6845
6848
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121090
120831
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120257
119998
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129129
128870
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135959
135700
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
187311
187052
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179274
179015
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238692
238431
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactIn3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
230679
230418
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134844
134585
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119975
119716
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactInputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119120
118861
Original file line number Diff line number Diff line change
@@ -1 +1 @@
126893
126634
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121098
120839
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_oneForZero.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
129970
129711
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut1Hop_zeroForOne.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
134771
134512
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
186732
186473
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut2Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
183655
183396
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238737
238476
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeIn.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
235684
235423
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOut3Hops_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
229889
229628
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133648
133389
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125770
125511
2 changes: 1 addition & 1 deletion .forge-snapshots/V4Router_ExactOutputSingle_nativeOut.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120033
119774
8 changes: 5 additions & 3 deletions src/base/BaseActionsRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ abstract contract BaseActionsRouter is SafeCallback {
}

/// @notice function that is called by the PoolManager through the SafeCallback.unlockCallback
/// @param data Abi encoding of (bytes actions, bytes[] params)
/// where params[i] is the encoded parameters for actions[i]
function _unlockCallback(bytes calldata data) internal override returns (bytes memory) {
// abi.decode(data, (uint256[], bytes[]));
(uint256[] calldata actions, bytes[] calldata params) = data.decodeActionsRouterParams();
// abi.decode(data, (bytes, bytes[]));
(bytes calldata actions, bytes[] calldata params) = data.decodeActionsRouterParams();

uint256 numActions = actions.length;
if (numActions != params.length) revert InputLengthMismatch();

for (uint256 actionIndex = 0; actionIndex < numActions; actionIndex++) {
uint256 action = actions[actionIndex];
uint256 action = uint256(uint8(actions[actionIndex]));

_handleAction(action, params[actionIndex]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/CalldataDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ library CalldataDecoder {
function decodeActionsRouterParams(bytes calldata _bytes)
internal
pure
returns (uint256[] calldata actions, bytes[] calldata params)
returns (bytes calldata actions, bytes[] calldata params)
{
assembly ("memory-safe") {
// The offset of the 0th element is 0, which stores the offset of the length pointer of actions array.
Expand Down
13 changes: 6 additions & 7 deletions test/shared/Planner.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,28 @@ import {Actions} from "../../src/libraries/Actions.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";

struct Plan {
uint256[] actions;
bytes actions;
bytes[] params;
}

library Planner {
using Planner for Plan;

function init() internal pure returns (Plan memory plan) {
return Plan({actions: new uint256[](0), params: new bytes[](0)});
return Plan({actions: bytes(""), params: new bytes[](0)});
}

function add(Plan memory plan, uint256 action, bytes memory param) internal pure returns (Plan memory) {
uint256[] memory actions = new uint256[](plan.actions.length + 1);
bytes memory actions = new bytes(plan.params.length + 1);
bytes[] memory params = new bytes[](plan.params.length + 1);

for (uint256 i; i < actions.length - 1; i++) {
for (uint256 i; i < params.length - 1; i++) {
// Copy from plan.
actions[i] = plan.actions[i];
params[i] = plan.params[i];
actions[i] = plan.actions[i];
}

actions[actions.length - 1] = action;
params[params.length - 1] = param;
actions[params.length - 1] = bytes1(uint8(action));

plan.actions = actions;
plan.params = params;
Expand Down

0 comments on commit c77742c

Please sign in to comment.