Closed Bug 257128 Opened 20 years ago Closed 20 years ago

Interpreter and tail call elimination

Categories

(Rhino Graveyard :: Core, enhancement)

head
x86
Linux
enhancement
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: igor, Assigned: igor)

Details

Attachments

(2 files, 1 obsolete file)

Given that interpreter do not use Java stack frames for JS (see bug 256339) it is only natural to support at least minimal form of tail call elimination so tail call recursion in JS would require only constant amount of memory.
Attached patch Implementation (obsolete) — Splinter Review
The code generation of the patch is straightforward. It adds context flags to visitExpression which defaults to 0 indicating generic context if node does not know what to do about it. Then during generation of Token.RETURN the patch calls visitExpression with ECF_TAIL flag to indicate the context. Then Token.COMMA, Token.HOOK, Token.AND and Token.OR preserve the flag for tail call operands and when Token.CALL gets the flag it uses Icode_TAIL_CALL instead of Token.CALL to indicate tail call. The runtime part was more tricky since initially I tries to implement state and its stack arrays reuse. But this is too complex due to possible exceptions during state reinitialization (see comments in the patch) )so at the end the difference between tail and ordinary call in the patch is that the tail call release its parent and would transfer control on return to its grandparent instead.
Attached file JS test cae
Without tail call recursion eleimination the test case would create 20_000_000 JS stack frames and even stackless interpreter would give up with OutOfMemory. With the patch applied the test case finishes after about 45s on my Athlon 1.2MHz Fedora box.
Attached patch Implementation2Splinter Review
The previous patch contains a bug: Interpreter.interpret would use initial state to extract interpretation result but after tail call that state was never updated with the result. The solution is to return the final state for the result extraction.
Attachment #157149 - Attachment is obsolete: true
I committed the patch
Status: NEW → RESOLVED
Closed: 20 years ago
Resolution: --- → FIXED
Target Milestone: --- → 1.5R6
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: