
Unveiling Huff
Optimized Dispatchers

Part II
Extreme Dispatching
Function dispatching can be easily implemented using a linear if-else flow, and more often than not, this is usually sufficient.
However, we can also choose to implement more advanced dispatching logic.
Binary Search Dispatchers: If we order our selectors, we can hardcode a binary search tree to quickly dispatch to the correct function. If we have
nfunctions, this will takeO(log(n))jumps. For a largen, this can be a decent optimization in gas.Look-up Dispatchers: There is one more extremely unorthodox method of dispatching, proposed by Philogy. Although this can be argued to be an over-optimization, it is still an interesting technique to study.
TownDefenseV2.huff
Are you ready to upgrade the town's defense? TownDefenseV2.huff wants to implement a much lengthier interface (see interfaces/ITownDefenseV2.huff).
Like before, the functions in ITownDefenseV2 can be implemented with the macros in Orders.huff. For example:
raiseWall1() → 0x01 RAISE_WALL()
raiseWall2() → 0x02 RAISE_WALL()
raiseWall3() → 0x03 RAISE_WALL()
raiseWall4() → 0x04 RAISE_WALL()
However, unlike before, there are now strict gas restrictions enforced on tests. The only way to pass these tests will be to implement an overly efficient dispatcher...
Your Task
Implement a look-up dispatcher in contracts/TownDefenseV2.huff.
Run tests in Questplay
Submit work in Questplay

You have the wood you need. Now organize the builders and raise the wall...