gleam-lang/gleam

Apply tail call optimsation on JS when piping into self with a function capture

Open

#3,167 opened on May 20, 2024

View on GitHub
 (5 comments) (1 reaction) (0 assignees)Rust (21,417 stars) (960 forks)batch import
help wanted

Description

Using pipes and function captures makes it so the compiler won't TCO a function on the js target. Consider:

pub fn do_len(list, acc) {
  case list {
    [] -> acc
    [_, ..rest] -> do_len(rest, acc + 1)
  }
}

This code gets optimised to:

export function do_len(loop$list, loop$acc) {
  while (true) {
    let list = loop$list;
    let acc = loop$acc;
    if (list.hasLength(0)) {
      return acc;
    } else {
      let rest = list.tail;
      loop$list = rest;
      loop$acc = acc + 1;
    }
  }
}

However, if we rewrite the code using a pipe and a function capture it no longer applies the optimisation:

pub fn do_len(list, acc) {
  case list {
    [] -> acc
    [_, ..rest] -> rest |> do_len(_, acc + 1)
    // Even the opposite wouldn't be optimised:
    // { acc + 1 } |> do_len(rest, _)
  }
}
export function do_len(list, acc) {
  if (list.hasLength(0)) {
    return acc;
  } else {
    let rest = list.tail;
    let _pipe = rest;
    return ((_capture) => { return do_len(_capture, acc + 1); })(_pipe);
  }
}

I would expect the generated code to look roughly the same for both examples. I had a look at the codegen code and it looks like the fix would be matching on TypedExpr::Fn where the body is just a single call (that's what pipes get translated to in the typed AST). That does feel a bit hacky though... maybe this is another point that would benefit from having pipes in the typed AST, then we could be smarter with the code generation and avoid this problem altogether?

Contributor guide