Tail Call Optimisation in Java











up vote
3
down vote

favorite












As of Java 8, Java does not provide Tail-Call Optimization (TCO).
On researching about it, I came to know the reason which is:




in jdk classes [...] there are a number of security sensitive methods that rely on counting stack frames between jdk library code and calling code to figure out who's calling them.




However Scala, which is based on JVM has support for Tail-Call Optimisation. Scala does tail recursion optimisation at compile-time. Why can't Java use the same approach ?



PS: Not sure whether the latest version (Java 11 as of now) of Java now has TCO. Would be great if some who knows can share this also.



Note




  1. I know TCO is at backlog and is of lower priority but want to know why can't Java make changes in compile time similar to Scala.


  2. Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops. (Source)











share|improve this question
























  • From the video, I came to know that TCO can be done at runtime but for this, changes will be needed in JVM level. Any specific reason why Java does not use similar approach and make changes in javac
    – rishabh agarwal
    Nov 17 at 19:48










  • Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops.
    – rishabh agarwal
    Nov 17 at 19:56










  • Yes it is. Wanted to share the info here. Isn't it the right way ?
    – rishabh agarwal
    Nov 17 at 20:01












  • @Aomine does this look good ?
    – rishabh agarwal
    Nov 17 at 20:08










  • yes. much better.
    – Aomine
    Nov 17 at 20:24

















up vote
3
down vote

favorite












As of Java 8, Java does not provide Tail-Call Optimization (TCO).
On researching about it, I came to know the reason which is:




in jdk classes [...] there are a number of security sensitive methods that rely on counting stack frames between jdk library code and calling code to figure out who's calling them.




However Scala, which is based on JVM has support for Tail-Call Optimisation. Scala does tail recursion optimisation at compile-time. Why can't Java use the same approach ?



PS: Not sure whether the latest version (Java 11 as of now) of Java now has TCO. Would be great if some who knows can share this also.



Note




  1. I know TCO is at backlog and is of lower priority but want to know why can't Java make changes in compile time similar to Scala.


  2. Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops. (Source)











share|improve this question
























  • From the video, I came to know that TCO can be done at runtime but for this, changes will be needed in JVM level. Any specific reason why Java does not use similar approach and make changes in javac
    – rishabh agarwal
    Nov 17 at 19:48










  • Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops.
    – rishabh agarwal
    Nov 17 at 19:56










  • Yes it is. Wanted to share the info here. Isn't it the right way ?
    – rishabh agarwal
    Nov 17 at 20:01












  • @Aomine does this look good ?
    – rishabh agarwal
    Nov 17 at 20:08










  • yes. much better.
    – Aomine
    Nov 17 at 20:24















up vote
3
down vote

favorite









up vote
3
down vote

favorite











As of Java 8, Java does not provide Tail-Call Optimization (TCO).
On researching about it, I came to know the reason which is:




in jdk classes [...] there are a number of security sensitive methods that rely on counting stack frames between jdk library code and calling code to figure out who's calling them.




However Scala, which is based on JVM has support for Tail-Call Optimisation. Scala does tail recursion optimisation at compile-time. Why can't Java use the same approach ?



PS: Not sure whether the latest version (Java 11 as of now) of Java now has TCO. Would be great if some who knows can share this also.



Note




  1. I know TCO is at backlog and is of lower priority but want to know why can't Java make changes in compile time similar to Scala.


  2. Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops. (Source)











share|improve this question















As of Java 8, Java does not provide Tail-Call Optimization (TCO).
On researching about it, I came to know the reason which is:




in jdk classes [...] there are a number of security sensitive methods that rely on counting stack frames between jdk library code and calling code to figure out who's calling them.




However Scala, which is based on JVM has support for Tail-Call Optimisation. Scala does tail recursion optimisation at compile-time. Why can't Java use the same approach ?



PS: Not sure whether the latest version (Java 11 as of now) of Java now has TCO. Would be great if some who knows can share this also.



Note




  1. I know TCO is at backlog and is of lower priority but want to know why can't Java make changes in compile time similar to Scala.


  2. Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops. (Source)








java recursion compilation jvm tail-recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 at 20:13

























asked Nov 17 at 19:43









rishabh agarwal

692316




692316












  • From the video, I came to know that TCO can be done at runtime but for this, changes will be needed in JVM level. Any specific reason why Java does not use similar approach and make changes in javac
    – rishabh agarwal
    Nov 17 at 19:48










  • Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops.
    – rishabh agarwal
    Nov 17 at 19:56










  • Yes it is. Wanted to share the info here. Isn't it the right way ?
    – rishabh agarwal
    Nov 17 at 20:01












  • @Aomine does this look good ?
    – rishabh agarwal
    Nov 17 at 20:08










  • yes. much better.
    – Aomine
    Nov 17 at 20:24




















  • From the video, I came to know that TCO can be done at runtime but for this, changes will be needed in JVM level. Any specific reason why Java does not use similar approach and make changes in javac
    – rishabh agarwal
    Nov 17 at 19:48










  • Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops.
    – rishabh agarwal
    Nov 17 at 19:56










  • Yes it is. Wanted to share the info here. Isn't it the right way ?
    – rishabh agarwal
    Nov 17 at 20:01












  • @Aomine does this look good ?
    – rishabh agarwal
    Nov 17 at 20:08










  • yes. much better.
    – Aomine
    Nov 17 at 20:24


















From the video, I came to know that TCO can be done at runtime but for this, changes will be needed in JVM level. Any specific reason why Java does not use similar approach and make changes in javac
– rishabh agarwal
Nov 17 at 19:48




From the video, I came to know that TCO can be done at runtime but for this, changes will be needed in JVM level. Any specific reason why Java does not use similar approach and make changes in javac
– rishabh agarwal
Nov 17 at 19:48












Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops.
– rishabh agarwal
Nov 17 at 19:56




Java doesn't have tail call optimization for the same reason most imperative languages don't have it. Imperative loops are the preferred style of the language, and the programmer can replace tail recursion with imperative loops.
– rishabh agarwal
Nov 17 at 19:56












Yes it is. Wanted to share the info here. Isn't it the right way ?
– rishabh agarwal
Nov 17 at 20:01






Yes it is. Wanted to share the info here. Isn't it the right way ?
– rishabh agarwal
Nov 17 at 20:01














@Aomine does this look good ?
– rishabh agarwal
Nov 17 at 20:08




@Aomine does this look good ?
– rishabh agarwal
Nov 17 at 20:08












yes. much better.
– Aomine
Nov 17 at 20:24






yes. much better.
– Aomine
Nov 17 at 20:24














1 Answer
1






active

oldest

votes

















up vote
4
down vote














Why can't Java use the same approach ?




I can't say which approach will be used, but it's better-explained in Project Loom's proposal:




As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.




As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354898%2ftail-call-optimisation-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote














    Why can't Java use the same approach ?




    I can't say which approach will be used, but it's better-explained in Project Loom's proposal:




    As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.




    As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.






    share|improve this answer

























      up vote
      4
      down vote














      Why can't Java use the same approach ?




      I can't say which approach will be used, but it's better-explained in Project Loom's proposal:




      As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.




      As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.






      share|improve this answer























        up vote
        4
        down vote










        up vote
        4
        down vote










        Why can't Java use the same approach ?




        I can't say which approach will be used, but it's better-explained in Project Loom's proposal:




        As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.




        As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.






        share|improve this answer













        Why can't Java use the same approach ?




        I can't say which approach will be used, but it's better-explained in Project Loom's proposal:




        As adding the ability to manipulate call stacks to the JVM will undoubtedly be required, it is also the goal of this project to add an even lighter-weight construct that will allow unwinding the stack to some point and then invoke a method with given arguments (basically, a generalization of efficient tail-calls). We will call that feature unwind-and-invoke, or UAI. It is not the goal of this project to add an automatic tail-call optimization to the JVM.




        As far as I've heard, work has not yet begun on tail calls, as Fibers and Continuations seem to currently be a higher priority.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 at 19:50









        Jacob G.

        14.6k51961




        14.6k51961






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354898%2ftail-call-optimisation-in-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Fotorealismo