motoko icon indicating copy to clipboard operation
motoko copied to clipboard

perf: optimize for constant arguments in tail call optimization

Open crusso opened this issue 2 years ago • 3 comments

Consider:

  public func find<K, V>(
    map : AssocList<K, V>,
    key : K,
    equal : (K, K) -> Bool
  ) : ?V {
    switch (map) {
      case (?((hd_k, hd_v), tl)) {
        if (equal(key, hd_k)) {
          ?hd_v
        } else {
          find(tl, key, equal)
        }
      };
      case (null) { null }
    }
  };

This is tail recursive and should be optimized to a loop by moc. However, arguments key and equal don't change on recursive calls/loop iterations and should only be set once, not on every iteration. I suspect we don't do that optimization.

crusso avatar Feb 28 '23 11:02 crusso

@kentosugama might be an interesting optimization to consider doing

crusso avatar Feb 28 '23 12:02 crusso

Will look at this once I get to IR level / motoko specific opts. Going to look at WASM level opts first

kentosugama avatar Feb 28 '23 19:02 kentosugama

Btw, the tail calls proposal for Wasm is now at stage 4, which means it's completed and merely needs to be merged into the main spec. I hope this can happen in the next few weeks.

rossberg avatar Mar 02 '23 09:03 rossberg