universal-router icon indicating copy to clipboard operation
universal-router copied to clipboard

Consider removing (or editing) nested if logic in Dispatcher.

Open ewilz opened this issue 2 years ago • 3 comments

Since we're getting contributions and plan to deploy new contracts to support more protocols in the medium future, I'm open to getting rid of the nested if logic. Even when including 15 commands (which would likely be a big outlier), we hardly save 2k gas.

If this was a one time deploy, I think it would make a lot of sense. But for the sake of maintainability I think we should either:

  1. take it out.
  2. at least use > < operators so that certain bits aren't muddying flags for lower numbers.

ewilz avatar Nov 19 '22 22:11 ewilz

To make it more clear and gas efficient I thought of doing a mapping(bytes1 => bytes4) commandToFunction and hardcode the different functions so the user only needs to call a addr.call(abi.encodeWithSignature(commandToFunction[commandType], commandType)). I guess in this case it would be more efficient, the access to the function would be O(1) instead of O(n) where n is the number of functions. Idk if this kind of solution would be as safe as the current one tho.

DennisDv24 avatar Nov 20 '22 11:11 DennisDv24

SLOAD for every command lookup would almost definitely cost more gas IMO. happy to review a gas-snapshotted PoC if you have one and prove me wrong :)

marktoda avatar Nov 20 '22 19:11 marktoda

https://github.com/Uniswap/universal-router/pull/181

ewilz avatar Nov 21 '22 14:11 ewilz