[转]C# compiler should optimize tail calls

原文:http://connect.microsoft.com/VisualStudio/feedback/details/166013/c-compiler-should-optimize-tail-calls

 

c45207 - 

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.

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.

Thanks,
Luke Hoban
Visual C# Compiler Program Manager

posted @ 2014-06-22 15:20  Scan.  阅读(202)  评论(0)    收藏  举报