D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3713 - Tail call optimization not enabled with the ?: operator
Summary: Tail call optimization not enabled with the ?: operator
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: backend, performance, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2010-01-16 20:40 UTC by Andrei Alexandrescu
Modified: 2020-09-13 15:26 UTC (History)
3 users (show)

See Also:


Attachments
Partial solution (5.75 KB, patch)
2013-10-04 20:01 UTC, safety0ff.bugz
Details | Diff
Partial solution (6.10 KB, patch)
2013-10-04 20:11 UTC, safety0ff.bugz
Details | Diff

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2010-01-16 20:40:56 UTC
Consider:

bool b1search(int[] a, int x) {
    if (!a.length) return 0;
    auto mid = a.length / 2;
    auto e = a[mid];
    return x == e ? true
        : x < e ? b1search(a[0 .. mid], x)
        : b1search(a[mid + 1 .. $], x);
}

bool b2search(int[] a, int x) {
    if (!a.length) return 0;
    auto mid = a.length / 2;
    auto e = a[mid];
    if (x == e) return true;
    return x < e ? b2search(a[0 .. mid], x)
        : b2search(a[mid + 1 .. $], x);
}

bool b3search(int[] a, int x) {
    if (!a.length) return 0;
    auto mid = a.length / 2;
    auto e = a[mid];
    if (x == e) return true;
    if (x < e) return b3search(a[0 .. mid], x);
    return b3search(a[mid + 1 .. $], x);
}

bool b4search(int[] a, int x) {
    if (!a.length) return 0;
    auto mid = a.length / 2;
    auto e = a[mid];
    if (x == e) return true;
    return b4search(x < e ? a[0 .. mid] : a[mid + 1 .. $], x);
}

void main()
{
}

After compiling with:

dmd -O -release test

and looking at the generated object file, only b3search and b4search are tail-call optimized, but not b1search and b2search. Tail-call optimization doesn't seem to work when a ternary expression is used with return.
Comment 2 safety0ff.bugz 2013-10-04 20:01:14 UTC
Created attachment 1257 [details]
Partial solution

I took a stab at this one.
I've gotten b2search to compile to the same assembly code as b3search.
I did not handle nested ternary operators following a return statement.
Comment 3 safety0ff.bugz 2013-10-04 20:11:47 UTC
Created attachment 1258 [details]
Partial solution
Comment 4 Dlang Bot 2020-09-13 08:57:00 UTC
@WalterBright created dlang/dmd pull request #11725 "fix Issue 3713 - Tail call optimization not enabled with the ?: operator" fixing this issue:

- fix Issue 3713 - Tail call optimization not enabled with the ?: operator

https://github.com/dlang/dmd/pull/11725
Comment 5 Dlang Bot 2020-09-13 15:26:55 UTC
dlang/dmd pull request #11725 "fix Issue 3713 - Tail call optimization not enabled with the ?: operator" was merged into master:

- fc88c8befa53870256fbe8f2abbba3c397469323 by Walter Bright:
  fix Issue 3713 - Tail call optimization not enabled with the ?: operator

https://github.com/dlang/dmd/pull/11725