Implement memo-macro and use for tree-dist

Otherwise performance is completely down the drain. It might be anyways.
Larger samples sizes needs testing.