Created
July 9, 2010 23:49
-
-
Save anonymous/470257 to your computer and use it in GitHub Desktop.
notes on Ruby Tail Call Optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First, I suppose you have ruby 1.9.1 or 1.9.2 | |
# Open irb. Execute this: | |
# RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false } | |
# Note, RubyVM::InstructionSequence.compile_option() doesn't have item :trace_instruction, still, you need to specify it. WTF? | |
# Also, it doesn't work if you just put it to the script. Don't know why yet. Tell me (@rofh) if you know. | |
# Then open the file: | |
# require "./tail-recursive" | |
# Then execute function: | |
# fact(30000) | |
# Unfortunately, this doesn't work: | |
#def fact(n) | |
# fact_tail = lambda { |x,acc| x == 0 ? acc : fact_tail.call(x - 1, x*acc) } | |
# fact_tail.call(n,1) | |
#end | |
# It is unfortunate, because ruby doesn't have support for nested functions ( as in Python ) | |
# So we have to go with this: | |
def fact(n) | |
fact_tail(n,1) | |
end | |
def fact_tail(x,acc) | |
x == 0 ? acc : fact_tail(x - 1, x*acc) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple wrapper to run your script with TCO support. | |
# Works on 1.9.1, doesn't work on 1.9.2 because of RubyVM::InstructionSequence.new format change, and there are no docs covering it. iseq.c isn't very informative for me as well. | |
#!/usr/bin/env ruby | |
filename = ARGV.first | |
iseq = RubyVM::InstructionSequence.new(open(filename).read,filename,1,tailcall_optimization: true, trace_instruction: false) | |
puts iseq.eval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment