Tail calls of funs not optimized?

Ondřej Adamovský <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <[email protected]>
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ý
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.