Get the latest tech news

Tail Call Recursion in Java with ASM (2023)


One kind of optimization offered by some compilers is tail call optimization. This optimization does not bring much, since the programmer can always tailor his code without recursion, especially in an imperative language.

In this article I will present a neat way to implement tail call optimization in Java using byte code manipulation with ASM. ASM is a wonderfull and neat library which allows one to analyze, transform and generate byte code at compile time or at runtime. The next stage is to remove the recursive call and the return instruction after it, altogether with preparing the local variables and stack for next use.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of ASM

ASM

Photo of tail call recursion

tail call recursion

Related news:

News photo

Converting C to ASM to specs and then to a working Z/80 Speccy tape

News photo

Mixin is a trait/mixin and bytecode weaving framework for Java using ASM