Does @tailrec affect compiler optimizations?

时光总嘲笑我的痴心妄想 提交于 2021-02-08 11:13:02

问题


I looked at this question trying to better understand @tailrec annotation in scala.

What I'm not sure is whether the annotation also hints the compiler to do some optimizations or it's only used for warnings when you mark a method that is not a tail-recursion?

More specifically - is this annotation might affect performance in any way? For example, if I don't put this annotation, the compiler will compile a tail-recursive function as non-tail recursive?


回答1:


As per the scaladoc:

A method annotation which verifies that the method will be compiled with tail call optimization.

If it is present, the compiler will issue an error if the method cannot be optimized into a loop.

This is a verification to trigger an error if you thought you wrote an optimizable function while you actually did not. Even if you don't put it, the compiler will optimize the code if it is possible.



来源:https://stackoverflow.com/questions/23546300/does-tailrec-affect-compiler-optimizations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!