Tail calls of funs not optimized?

Christophe De Troyer christophe@REDACTED
Tue Mar 30 09:15:05 CEST 2021


As a lurker of this mailing list Im interested in these listings. Mind sharing them, too?

- Christophe

On 29 March 2021 18:52:02 CEST, "Ondřej Adamovský" <oa@REDACTED> wrote:
>Hello.
>
>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.
>
>Can somebody explain to me why? Or if this still is optimized somehow,
>then how?
>
>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).
>
>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
>https://erlang.org/doc/reference_manual/functions.html#tail-recursion
>in chapter about functions with no mention of funs. Function call is
>defined here
>https://erlang.org/doc/reference_manual/expressions.html#function-calls
>explicitly stating fun call as a possibility.
>
>Best regards,
>Ondřej Adamovský

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20210330/f87fc27e/attachment.htm>


More information about the erlang-questions mailing list