| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
March 31, 2015 Mutual optimization of tail recursion does not work in D | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
March 31, 2015 Re: Mutual optimization of tail recursion does not work in D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | 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. | |||
March 31, 2015 Re: Mutual optimization of tail recursion does not work in D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to w0rp Attachments: | 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.
| |||
March 31, 2015 Re: Mutual optimization of tail recursion does not work in D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Tuesday, 31 March 2015 at 12:01:52 UTC, ketmar wrote:
> gdc does, as this is gcc backend optimisation.
Thanks.
| |||
March 31, 2015 Re: Mutual optimization of tail recursion does not work in D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to w0rp | 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? | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply