gleam-lang/gleam

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

Open

#3,167 创建于 2024年5月20日

在 GitHub 查看
 (5 评论) (1 反应) (0 负责人)Rust (960 fork)batch import
help wanted

仓库指标

Star
 (21,417 star)
PR 合并指标
 (平均合并 10天 19小时) (30 天内合并 69 个 PR)

描述

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?

贡献者指南