TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Tail Call Recursion in Java with ASM (2023)

96 点作者 hyperbrainer大约 2 个月前

7 条评论

fsckboy大约 2 个月前
the &quot;lambda the ultimate&quot; papers and the birth of scheme was a loong time ago, so it grates on my ears to hear this topic presented as &quot;an optimization&quot;. Yes, it is sometimes an optimization a compiler can make, but the idea is much better presented as a useful semantic of a language.<p>in the same way that passing parameters to a subfunction &quot;creates&quot; a special set of local variables for the subfunction, the tail recursion semantic updates this set of local variables in an especially clean way for loop semantics, allowing &quot;simultaneous assignment&quot; from old values to new ones.<p>(yes, it would be confusing with side effected C&#x2F;C++ operators like ++ because then you&#x27;d need to know order of evaluation or know not to do that, but those are already issues in those languages quite apart from tail recursion)<p>because it&#x27;s the way I learned it, I tend to call the semantic &quot;tail recursion&quot; and the optimization &quot;tail call elimination&quot;, but since other people don&#x27;t do the same it&#x27;s somewhat pointless; but I do like to crusade for awareness of the semantic beyond the optimization. If it&#x27;s an optimization, you can&#x27;t rely on it because you could blow the stack on large loops. If it&#x27;s a semantic, you can rely on it.<p>(the semantic is not entirely &quot;clean&quot; either. it&#x27;s a bit of a subtle point that you need to return straightaway the return value of the tail call or it&#x27;s not a tail call. fibonacci is the sum of the current with the next so it&#x27;s not a tail call unless you somewhat carefully arrange the values you pass&#x2F;keep around. also worth pointing out that all &quot;tail calls&quot; are up for consideration, not just recursive ones)
评论 #43532972 未加载
评论 #43536661 未加载
评论 #43533023 未加载
评论 #43527746 未加载
bradley13大约 2 个月前
Every compiler should recognize and optimize for tail recursion. It&#x27;s not any harder than most other optimizations, and some algorithms are far better expressed recursively.<p>Why is this not done?
评论 #43524923 未加载
评论 #43525505 未加载
评论 #43524861 未加载
评论 #43527848 未加载
cempaka大约 2 个月前
Very nice article demonstrating a neat use of ASM bytecode. The Java language devs are also working on Project Babylon (code reflection), which will bring additional techniques to manipulate the output from the Java compiler: <a href="https:&#x2F;&#x2F;openjdk.org&#x2F;projects&#x2F;babylon&#x2F;articles&#x2F;code-models" rel="nofollow">https:&#x2F;&#x2F;openjdk.org&#x2F;projects&#x2F;babylon&#x2F;articles&#x2F;code-models</a>
评论 #43525586 未加载
1932812267大约 2 个月前
Scala has been using this technique for years with its scala.annotation.tailrec annotation. Regardless, it&#x27;s cool to see this implemented as a bytecode pass.
评论 #43525559 未加载
ncruces大约 2 个月前
It&#x27;s been a long time since I&#x27;ve messed with Java bytecode [1], but shouldn&#x27;t the private method call use INVOKESPECIAL?<p>In general I don&#x27;t think you can do this to INVOKEVIRTUAL (or INVOKEINTERFACE) as it covers cases where your target is not statically resolved (virtual&#x2F;interface calls). This transformation should be limited to INVOKESTATIC and INVOKESPECIAL.<p>You also need lots more checks to make sure you can apply the transformations, like ensure the call site is not covered by a try block, otherwise this is not semantics preserving.<p>1: <a href="https:&#x2F;&#x2F;jauvm.blogspot.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jauvm.blogspot.com&#x2F;</a>
lukaslalinsky大约 2 个月前
I never understood the need for tail recursion optimization in imperative languages. Sure, you need it in FP if you don&#x27;t have loops and recursion is you only option, but what is the benefit of recursive algorithms, that could benefit from tail optimization (i.e recursive loops), in a language like Java?
droideqa大约 2 个月前
Cool, now ABCL can have TCO!
评论 #43525333 未加载
评论 #43524497 未加载