Reputation: 207
I see that tail call optimization can be enabled in ruby and that it works with method calls following these instructions, but I need it to work for Procs too for use with blocks/lambdas, since only these can pass closures around as first class functions (that I'm aware of). But I can't find any information about tail call optimization and Procs. From my experiments it doesn't seem to use the setting that regular functions calls do. Is it possible or is there an alternative that could be useful?
I'm implementing a lazy language and I need to support deep recursion in that language since that is how the target lang works. I've tried switching the code to Continuation Passing Style and returning thunks that get evaluated trampoline style (this avoids the stack overflow that deep recursion would call), but it is about 3 times slower than my previous implementation just using the stack. Essentially I need the tail call opt because all my CPS stuff is a tail call so I could eliminate the trampoline which is expensive (maybe doubles the number of Procs created to use it). I realize Ruby probably isn't the best language for implementing this, but it isn't a super serious project, still it would be nice for it not to be very slow.
Upvotes: 0
Views: 72