Open giacomocavalieri opened 4 months ago
Please include the code that doesn't optimise š
Ooh sorry I thought I did š Edited the original message to include the code
This is due to the function capture being used. If you run the formatter it would be removed and the resulting code would be optimised. It would be nice to optimise for the captures too though.
Yeah I think this optimisation would be really nice to have, it bit me with glam
because in the pretty printing loop I did a lot of this:
list.map(docs, fn(doc) { #(indent, mode, doc) })
|> list.append(rest)
|> do_to_string(acc, max_width, current_width, _)
Thinking the do_to_string
function would be optimised and never noticed until someone built a really big document š
I'd like to give this a try!
Using pipes and function captures makes it so the compiler won't TCO a function on the js target. Consider:
This code gets optimised to:
However, if we rewrite the code using a pipe and a function capture it no longer applies the optimisation:
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?