Bug 3713 – Tail call optimization not enabled with the ?: operator

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-01-16T20:40:56Z
Last change time
2020-09-13T15:26:55Z
Keywords
backend, performance, pull, wrong-code
Assigned to
No Owner
Creator
Andrei Alexandrescu

Attachments

IDFilenameSummaryContent-TypeSize
12570001-Apply-tail-call-optimization-to-conditional-return-e.patchPartial solutiontext/plain5886
12580001-Apply-tail-call-optimization-to-conditional-return-e.patchPartial solutiontext/plain6248

Comments

Comment #0 by andrei — 2010-01-16T20:40:56Z
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 #1 by yebblies — 2012-02-20T02:43:14Z
Comment #2 by safety0ff.bugz — 2013-10-04T20:01:14Z
Created attachment 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.
Comment #3 by safety0ff.bugz — 2013-10-04T20:11:47Z
Created attachment 1258 Partial solution
Comment #4 by dlang-bot — 2020-09-13T08:57:00Z
@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 by dlang-bot — 2020-09-13T15:26:55Z
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