www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mutual optimization of tail recursion does not work in D

reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
Hi,

This code does not work:

import std.stdio;

bool odd(int n);
bool even(int n);

bool even(int n) {
	if (n == 0)
		return true;
	else
		return odd(n - 1);
}

bool odd(int n) {
	if (n == 0)
		return false;
	else
		return even(n - 1);
}

void main() {

	bool r = odd(655370);
	
	writeln(r);
}

http://ideone.com/GSNMxl
	
This code works completely:

#include <cstdio>

bool odd(int n);
bool even(int n);

bool even(int n) {
	if (n == 0)
		return true;
	else
		return odd(n - 1);
}

bool odd(int n) {
	if (n == 0)
		return false;
	else
		return even(n - 1);
}

int main() {
	
	bool r = odd(655370);
	
	printf("%d\n", r); // prints 0
	
	return 0;
}

http://ideone.com/TT48zT

Why D does not work?
Mar 31 2015
parent reply "w0rp" <devw0rp gmail.com> writes:
Mutual tail call optimisation doesn't work in C++ either.

Because it's not a language feature in C++ or D. It is not 
required by the standards of either language. It's an 
optimisation which compilers apply. I am guessing you are using 
DMD, which might not offer the best optimisations for runtime 
code, but has other benefits.

You might want to try GDC or LDC. I am not certain if they 
implement tail call optimisations, but they might do, and it 
seems like a good optimisation to have. I'm sure this has been 
discussed before.
Mar 31 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Tue, 31 Mar 2015 11:57:49 +0000, w0rp wrote:

 You might want to try GDC or LDC. I am not certain if they implement
 tail call optimisations, but they might do, and it seems like a good
 optimisation to have. I'm sure this has been discussed before.
gdc does, as this is gcc backend optimisation.=
Mar 31 2015
parent "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Tuesday, 31 March 2015 at 12:01:52 UTC, ketmar wrote:
 gdc does, as this is gcc backend optimisation.
Thanks.
Mar 31 2015
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Tue, Mar 31, 2015 at 11:57:49AM +0000, w0rp via Digitalmars-d-learn wrote:
 Mutual tail call optimisation doesn't work in C++ either.
 
 Because it's not a language feature in C++ or D. It is not required by
 the standards of either language. It's an optimisation which compilers
 apply. I am guessing you are using DMD, which might not offer the best
 optimisations for runtime code, but has other benefits.
 
 You might want to try GDC or LDC. I am not certain if they implement
 tail call optimisations, but they might do, and it seems like a good
 optimisation to have. I'm sure this has been discussed before.
In general, if you care about performance, I highly recommend using gdc or ldc instead of dmd. Dmd's backend is not as good at optimization as gdc or ldc. For compute-intensive programs, I find that gdc -O3 consistently produces code that performs about 20% (sometimes up to 50%) better than dmd-produced code, even with all dmd optimization flags turned on. T -- MAS = Mana Ada Sistem?
Mar 31 2015