<html><head></head><body>As a lurker of this mailing list Im interested in these listings. Mind sharing them, too?<br><br>- Christophe<br><br><div class="gmail_quote">On 29 March 2021 18:52:02 CEST, "Ondřej Adamovský" <oa@cmail.cz> wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">Hello.<br><br>Out of curiosity, I was looking at BEAM assembler listings of some of my modules. To my great dismay, I noticed tail fun calls seem to not be optimized. Regular function tail calls are translated to single call_last or call_only instruction, but fun tail call is translated to call_fun instruction followed by deallocate and return instructions. Also, the call_fun instruction is the same regardless of its position in the function body.<br><br>Can somebody explain to me why? Or if this still is optimized somehow, then how?<br><br>I regularly use a recursive cycle of functions formed by recursive tail calls. With the optimization it effectively uses only one stack frame. It is only natural to vary parts of the cycle using funs depending on options provided to the initial call. If this finding is true, the cycle with funs produces at least one stack frame per cycle which can use up quite substantial portion of memory (or even deplete it in endless cycles).<br><br>This should have a big red warning in the documentation, but I did not find any mention of this there or anywhere. Tail call optimization is discussed here <a href="https://erlang.org/doc/reference_manual/functions.html#tail-recursion">https://erlang.org/doc/reference_manual/functions.html#tail-recursion</a> in chapter about functions with no mention of funs. Function call is defined here <a href="https://erlang.org/doc/reference_manual/expressions.html#function-calls">https://erlang.org/doc/reference_manual/expressions.html#function-calls</a> explicitly stating fun call as a possibility.<br><br>Best regards,<br>Ondřej Adamovský<br></pre></blockquote></div><br>-- <br>Sent from my Android device with K-9 Mail. Please excuse my brevity.</body></html>