www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3713] New: Tail call optimization not enabled with the ?: operator

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3713

           Summary: Tail call optimization not enabled with the ?:
                    operator
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrei metalanguage.com


--- Comment #0 from Andrei Alexandrescu <andrei metalanguage.com> 2010-01-16
20:40:56 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 16 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3713


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|performance                 |pull, wrong-code
         AssignedTo|nobody puremagic.com        |yebblies gmail.com


--- Comment #1 from yebblies <yebblies gmail.com> 2012-02-20 21:43:14 EST ---
https://github.com/D-Programming-Language/dmd/pull/745

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 20 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3713



--- Comment #2 from safety0ff.bugz <safety0ff.bugz gmail.com> 2013-10-04
20:01:14 PDT ---
Created an attachment (id=1257)
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 04 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3713


safety0ff.bugz <safety0ff.bugz gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Attachment #1257|0                           |1
        is obsolete|                            |


--- Comment #3 from safety0ff.bugz <safety0ff.bugz gmail.com> 2013-10-04
20:11:47 PDT ---
Created an attachment (id=1258)
Partial solution

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 04 2013