Implement function inlining optimisations #3722
Replies: 4 comments 10 replies
-
As for my initial thoughts on the two questions:
|
Beta Was this translation helpful? Give feedback.
-
@richard-viney has raised here a really good reason to have inlining: it would make functions like this stack safe in JS import gleam/bool
pub fn main() {
go(10_000)
}
fn go(i: Int) -> Int {
use <- bool.guard(i < 0, -1)
case i {
0 -> 0
_ -> go(i - 1)
}
} |
Beta Was this translation helpful? Give feedback.
-
RE implementing this- perhaps we could figure out just how to do the actual inlining to start and delay figuring out the heuristics for now. Just being able to do it for some predetermined functions in core packages such as the stdlib could be valuable, and we may decide to make it an attribute. |
Beta Was this translation helpful? Give feedback.
-
I had this idea on Discord while thinking about mutual TCO in Javascript:
statically dispatched here means any function value that the compiler can dereference at compile time. In the context of TCO, I meant that all function calls in tail position that call another local function in the same module can be transformed to a My thoughts here were that not caring about circular recursion makes it better actually, since we want to mimic Erlangs' LCO which also happens without recursion going on. You can draw tail calls like a state machine (this is again the example from #3830):
these diagrams can include calls to arguments if those happen to be in call position:
This means that if
Being able to inline
As always, I'm probably just making lots of wrong assumptions on the fly, please yell at me :) |
Beta Was this translation helpful? Give feedback.
-
This was briefly mentioned on Discord as something which would be nice to add in future, but I wanted to create a Github discussion to figure out the details of it as I think it could be a great addition to the language. (See the Discord conversation for an example). I will convert this into an issue once we have a good idea of how this would be implemented.
A couple of initial questions I had:
#[inline]
in rust, for example)Beta Was this translation helpful? Give feedback.
All reactions