当引发异常时返回堆栈跟踪时,C# 尾递归优化如何实现
我看到一些关于 C# 中缺少尾部调用优化的问题,据说这使得该语言不适合递归算法实现。然而,这引出了一个问题,我们如何进行尾部调用优化,并在引发异常或可以使用反射来检查调用堆栈并对其采取行动时仍然提供合理的堆栈跟踪。
I'be seen a few questions regarding missing tail call optimization in C# supposedly making the language ill suited for recursive algorithm implementations. this, however,begs the question, how can we do tail call optimizations and still provide sensible stack traces when exceptions are raised or when reflection may be used to inspect the call stack and act upon it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,只有当您希望获得准确的堆栈跟踪时才重要:)
尾部调用优化并不是唯一可以破坏堆栈跟踪的东西 - 最简单的例子是内联,这肯定会影响事物。基本上,任何依赖于堆栈跟踪准确的事情都会冒一些风险。
这是一个非常简单的例子来说明这个问题:
在没有调试器的情况下构建和运行,你可能会得到这样的结果:
基本上,要小心你对堆栈跟踪的处理。
Well, it only matters if you expect to get an accurate stack trace :)
Tail-call optimizations aren't the only things that can destroy a stack trace - the simplest example is inlining, which can certainly affect things. Basically, anything which relies on the stack trace being accurate is taking a bit of a risk.
Here's a very simple example of exactly that problem:
Build and run without the debugger, and you may get this:
Basically, be careful what you do with stack traces.