且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有技术原因,C#不发出"尾QUOT; CIL指令?

更新时间:2023-02-15 19:39:25

检查

为什么没有.NET / C#消除尾递归? / 491463#491463
http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/67b6d908-8811-430f-bc84-0081f4393336?Status$c$c=1
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=166013&wa=wsignin1.0

Why doesn't .net/C# eliminate tail recursion? /491463#491463
http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/67b6d908-8811-430f-bc84-0081f4393336?StatusCode=1
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=166013&wa=wsignin1.0

下面的语句是MS官方(卢克·霍本的Visual C#编译器程序管理器),并从最后一个环节复制

The following statement is MS official (Luke Hoban Visual C# Compiler Program Manager) and copied from last link

感谢您的建议。我们认为尾部冒落调用指令在数       在C#编译器的开发点。不过,也有一些微妙的问题       这已经推动我们避免这种情况至今:1)实际上是一个不平凡的开销       成本使用.tail指令在CLR(它不只是一个跳转指令作为       尾调用最终成为许多不太严格的环境中,如功能       语言运行时环境中尾调用大量优化)。 2)有       一些真正的C#方法,它是合法的排放尾调用(其它语言       鼓励编码具有更多的尾递归模式,和许多严重依赖       在尾调用优化的实际做全球重写(如延续传送       变换),以增加尾递归的量)。 3)部分由于2)       情况下,C#方法栈溢出由于深递归,应该成功       是相当罕见的。

Thanks for the suggestion. We've considered emiting tail call instructions at a number of points in the development of the C# compiler. However, there are some subtle issues which have pushed us to avoid this so far: 1) There is actually a non-trivial overhead cost to using the .tail instruction in the CLR (it is not just a jump instruction as tail calls ultimately become in many less strict environments such as functional language runtime environments where tail calls are heavily optimized). 2) There are few real C# methods where it would be legal to emit tail calls (other languages encourage coding patterns which have more tail recursion, and many that rely heavily on tail call optimization actually do global re-writing (such as Continuation Passing transformations) to increase the amount of tail recursion). 3) Partly because of 2), cases where C# methods stack overflow due to deep recursion that should have succeeded are fairly rare.

所有这一切说,我们继续看看这个,我们可能会在的未来的版本       编译器发现一些模式在有意义发出.tail说明。

All that said, we continue to look at this, and we may in a future release of the compiler find some patterns where it makes sense to emit .tail instructions.