Exploring c # tail recursive Compiler Optimization

Source: Internet
Author: User

Exploring c # tail recursive Compiler Optimization
Recursion uses a function to call itself directly or indirectly. This function can be called a recursive function. The main function of recursion is to convert the problem into a small-scale subproblem and gradually approach the final result with the solution of the subproblem. The most important thing about recursion is the boundary condition, which is the termination condition of the entire recursion. Static int RecFact (int x) {if (x = 0) return 1; return x * RecFact (x-1);} RecFact (10 ); the above is the implementation of a classic factorial function. Here we will take two steps: Convert and convert the ten factorial into 10*9 !, 10 (9*8 !).... The scale of each conversion is smaller. Approximation, 0 when converting to the minimum scale !, Solution 1. Start reverse merge and gradually approach to 10 to get the solution. Here, x = 0 is our boundary condition (that is, the termination condition), and some depend on external variables as the boundary. If a recursive function has no boundaries, it will not be able to stop (infinite loop to memory overflow). Of course, this is meaningless. RecFact call Stack: Common Use Cases: factorial, Fibonacci series, and tower traversal of Hard Disk Files with InnerExceptions exception. innerException = null) tail recursion optimization when the boundary is unclear, recursion is prone to overflow. During the factorial process, the stack needs to save the return address of each call (RecFact) and the status of all local variables at that time. During this period, the stack space cannot be released (that is, overflow is prone ). In order to optimize the occupation of the stack, a method for optimizing tail recursion is proposed. Static void Recurse (int x) {Console. writeLine (x); if (x = 10) return; Recurse (x + 1);} Recurse (0); the difference between Recursion and factorial is that there is no return value, that is to say, the stack does not need to save the last returned address/status value, which is the idea of tail recursion optimization. Tail recursion can solve the overflow problem caused by recursion too deep, because the stack can be released/reused during recursion. Compiler Optimization tail recursion optimization looks pretty good, but it is a bit messy in net. Net is optimized when JIT is compiled into an assembly in C. Net on IL, there is a special command tail to implement tail recursion optimization (F ). We run Recurse (0) (x = 1000000) to conclude that C #/64-bit/Release has JIT compiler for tail recursion optimization (non-C # Compiler Optimization ). JIT in C #/32-bit or C #/Debug mode is not optimized. F # optimize tail recursion in two cases: 1. Optimize a simple tail recursion to A while LOOP, as follows: let rec Recurse (x) = if (x = 1000) then true else Recurse (x + 1) (convenient demo C # rendering) optimized to: public static bool Recurse (int x) {while (x! = 0x3e8) {x ++;} return true;} 2. Complex Tail recursion. F # The Compiler generates the IL command Tail for optimization, as shown below: let Recurse2 a cont = cont (a + 1) is optimized to: how to define complex tail recursion? It is usually the next transmission mode (CPS ). F # In the debug mode, you need to configure it during compilation: in the C # Language (procedural/object-oriented programming ideas), loop is preferred, rather than recursive/tail recursion. But in functional programming, recursive/tail recursion is the mainstream usage, just like using loops in C.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.