Feature #1256

Add constant TAILRECURSION to let a program recognize if tail recursion optimization is implemented

Added by Wolfgang Nádasi-Donner about 6 years ago. Updated almost 4 years ago.

[ruby-core:22748]
Status:Rejected
Priority:Normal
Assignee:-

Description

=begin
Ruby 1.9 has the possibility to support tail recursion optimization for methods and proc objects. Due to technical problems it cannot be easy implemented for every VM or platform, which will support Ruby 1.9 functionality.

Tail recursion optimization is an implementation detail and not a language feature in general. I propose to supply a constant TAILRECURSION, which will be set to "true" if this feature is implemented, otherwise "false".
=end

History

#1 Updated by Shyouhei Urabe about 6 years ago

=begin
I'm curious, how you can be happy if you detect your VM supports tail-call optimization or not (from your script)?

I think you have to write without tail-calls anyway for compatibilities with those no-tail-call enironment.
=end

#2 Updated by Wolfgang Nádasi-Donner about 6 years ago

=begin
Shyouhei Urabe schrieb:

I'm curious, how you can be happy if you detect your VM supports tail-call optimization or not (from your script)?

I think you have to write without tail-calls anyway for compatibilities with those no-tail-call enironment.
I don't always write portable programs, but I want a controlled end of
such a programm, e.g. with a message "Programm cannot run under this
environment".

It's the same as I do in the moment. When started using Ruby 1.8 they
tell the user, that it will not work using Ruby 1.8.

This s better than a system error message.

Otherwise it is also possible to present a different implementation,
which may be much slower, but works and inform the user via a warning
about this fact.

Wolfgang Nádasi-Donner

=end

#3 Updated by Nobuyoshi Nakada about 6 years ago

=begin
Hi,

At Mon, 9 Mar 2009 02:41:55 +0900,
Wolfgang Nádasi-Donner wrote in :

Tail recursion optimization is an implementation detail and
not a language feature in general. I propose to supply a
constant TAILRECURSION, which will be set to "true" if this
feature is implemented, otherwise "false".

RubyVM::InstructionSequence.compile_option[:tailcall_optimization]

--
Nobu Nakada

=end

#4 Updated by Wolfgang Nádasi-Donner about 6 years ago

=begin
Nobuyoshi Nakada schrieb:

Hi,

At Mon, 9 Mar 2009 02:41:55 +0900,
Wolfgang Nádasi-Donner wrote in :

Tail recursion optimization is an implementation detail and
not a language feature in general. I propose to supply a
constant TAILRECURSION, which will be set to "true" if this
feature is implemented, otherwise "false".

RubyVM::InstructionSequence.compile_option[:tailcall_optimization]

Thank you for this information. I saw that it still exists in Ruby 1.9.1-p0.

Yesterday I tried to compile Ruby 1.9.1-p0 with changes named by
Nobuyoshi Nakada. In vm_opts.h I changed

#define OPT_TAILCALL_OPTIMIZATION 0

to

#define OPT_TAILCALL_OPTIMIZATION 1

Afterwards I rebuilt Ruby, but the stack overflow for both examples I
posted before are still there.

Should I change additional things in the source?

Wolfgang Nádasi-Donner

=end

#5 Updated by Wolfgang Nádasi-Donner about 6 years ago

=begin
As far as I understand this Ticket can be closed. If RubyVM::InstructionSequence.compile_option exists (defined? RubyVM::InstructionSequence.compile_option) and the value is "true" a program can expect a working tail recursion optimization, otherwise not.

Is this correct? - I yes, I think thee is no additional need for an extra constant.
=end

#6 Updated by Nobuyoshi Nakada about 6 years ago

=begin
Hi,

At Tue, 10 Mar 2009 04:26:51 +0900,
Wolfgang Nádasi-Donner wrote in :

Afterwards I rebuilt Ruby, but the stack overflow for both examples I
posted before are still there.

Should I change additional things in the source?

trace_instruction option prevents the optimization.

$ cat ~/tmp/tailcall.rb
#! /usr/bin/ruby
src, file, line = < e
puts "#{e.class} #{e.backtrace.size}"
end

$ ./ruby ~/tmp/tailcall.rb
tailcall_optimization: false, trace_instruction: true
SystemStackError 8187

$ ./ruby ~/tmp/tailcall.rb true
tailcall_optimization: true, trace_instruction: true
SystemStackError 8187

$ ./ruby ~/tmp/tailcall.rb true false
tailcall_optimization: true, trace_instruction: false
ok

--
Nobu Nakada

=end

#7 Updated by Nobuyoshi Nakada about 6 years ago

  • Status changed from Open to Rejected

=begin

=end

Also available in: Atom PDF