www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11497] New: lambda in "static if"/"assert" prevent inlining of function

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

           Summary: lambda in "static if"/"assert" prevent inlining of
                    function
           Product: D
           Version: unspecified
          Platform: All
               URL: https://d.puremagic.com/issues/show_bug.cgi?id=10848
        OS/Version: All
            Status: NEW
          Keywords: performance
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: monarchdodra gmail.com


--- Comment #0 from monarchdodra gmail.com 2013-11-11 13:07:20 PST ---
(Related: https://d.puremagic.com/issues/show_bug.cgi?id=10848)

If you have a function, that needs to do a test/assert, and does it via a
lambda block (for example, to declare a variable), then that function will not
be inline-able.

This seems really crazy to me, since the lambda only exists during compilation
anyways. This affects DMD, but not GDC.

Here is a test program. It's a reduced case of what currently happens when we
call "std.array.array".

Variant 1 contains:
    static assert(is(typeof(*chunk = arg)));
Whereas 2 contains:
    static assert(is(typeof({*chunk = arg;})));

//----
import std.stdio;
import std.datetime;

void array1(int[] arr)
{
    foreach (int i, e ; arr)
    {
        emplace1(&e, i);
        ++i;
    }
}
void emplace1(T, Arg)(T* chunk, Arg arg)
{
    static assert(is(typeof(*chunk = arg)));
    *chunk = arg;
}

void array2(T)(T[] arr)
{
    foreach (int i, e ; arr)
    {
        emplace2(&e, i);
        ++i;
    }
}
void emplace2(T, Arg)(T* chunk, Arg arg)
{
    static assert(is(typeof({*chunk = arg;})));
    *chunk = arg;
}

void main()
{
    auto arr = new int[] (10_000);
    StopWatch st1;
    st1.start;
    foreach (__; 0 .. 20_00)
    {
        array1(arr);
    }
    st1.stop;

    StopWatch st2;
    st2.start;
    foreach (__; 0 .. 20_00)
    {
        array2(arr);
    }
    st2.stop;

    writefln("1");
    writefln("Time: %sms", st1.peek.msecs);

    writefln("2");
    writefln("Time: %sms", st2.peek.msecs);
}
//----

Resulting times:
1
Time: 11ms
2
Time: 1972ms

:/

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 11 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #1 from monarchdodra gmail.com 2013-11-11 13:14:10 PST ---
To add, this happens regardless of optimization flags:

 dmd -release -O -inline -run hello.d 
1 Time: 11ms 2 Time: 1972ms
 dmd -release -O -run hello.d 
1 Time: 92ms 2 Time: 1967ms
 dmd -release -run hello.d 
1 Time: 167ms 2 Time: 2020ms
 dmd -run hello.d 
1 Time: 148ms 2 Time: 2023ms -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #2 from monarchdodra gmail.com 2013-11-11 13:15:13 PST ---
EDIT: I'm *assuming* it's an inline problem, I haven't look at the ASM. But I
don't see how it could be anything else...

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #3 from monarchdodra gmail.com 2013-11-12 07:03:35 PST ---
Hum... I did some more investigating, and it would appear the culprit is not
"just" having a lambda, but rather, having a lambda that needs access to
context. EG:

static assert(is(typeof(*chunk = arg))); //FAST
static assert(is(typeof({*chunk = Arg.init;))); //FAST
static assert(is(typeof({*chunk = arg;}))); //SLOW

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #4 from monarchdodra gmail.com 2013-11-12 09:20:26 PST ---
(In reply to comment #3)
 Hum... I did some more investigating, and it would appear the culprit is not
 "just" having a lambda, but rather, having a lambda that needs access to
 context. EG:
 
 static assert(is(typeof(*chunk = arg))); //FAST
 static assert(is(typeof({*chunk = Arg.init;))); //FAST
 static assert(is(typeof({*chunk = arg;}))); //SLOW
Typo: static assert(is(typeof(*chunk = arg))); //FAST static assert(is(typeof({*chunk = Arg.init;}))); //FAST static assert(is(typeof({*chunk = arg;}))); //SLOW -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497


Artem Tarasov <lomereiter gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lomereiter gmail.com


--- Comment #5 from Artem Tarasov <lomereiter gmail.com> 2013-11-12 13:44:40
PST ---
Also related: https://d.puremagic.com/issues/show_bug.cgi?id=11483

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://d.puremagic.com/iss
                   |                            |ues/show_bug.cgi?id=11483


--- Comment #6 from monarchdodra gmail.com 2013-11-12 14:20:40 PST ---
(In reply to comment #5)
 Also related: https://d.puremagic.com/issues/show_bug.cgi?id=11483
Nice. So indeed, it's not an inlining problem. I *thought* the results were way too catastrophic for a simple inline problem. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #7 from github-bugzilla puremagic.com 2013-11-17 19:47:18 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8f10b877ead5e82e5c9a05fc2ed361ab6b08a398
Workaround Issue 11497 - lambda in "static if"/"assert" prevent inlining of
function

https://github.com/D-Programming-Language/phobos/commit/1ef29584e992dba98b3ddbbc9c97f564d4e6207f
Merge pull request #1688 from monarchdodra/workaround11497

Workaround Issue 11497 - lambda in "static if"/"assert" prevent inlining of
functio

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 17 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #8 from Kenji Hara <k.hara.pg gmail.com> 2013-11-21 06:38:00 PST ---
https://github.com/D-Programming-Language/dmd/pull/2845

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 21 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #9 from github-bugzilla puremagic.com 2013-11-22 17:42:41 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/de32be111bb299612285e2fef82f197e2d4a2764
fix Issue 11497 - lambda in "static if"/"assert" prevent inlining of function

https://github.com/D-Programming-Language/dmd/commit/472f8c220f6ce7ca76d7d45afa0b5624575824dd
Merge pull request #2845 from 9rnsr/fix11497

Issue 11497 - lambda in "static if"/"assert" prevent inlining of function

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 22 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 22 2013
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11497



--- Comment #10 from github-bugzilla puremagic.com 2013-11-23 14:49:16 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/ad52c5f027d832e7d6064f88c1d64352e7a984d8
Revert "Workaround Issue 11497 - lambda in "static if"/"assert" prevent
inlining of function"

This reverts commit 8f10b877ead5e82e5c9a05fc2ed361ab6b08a398.

Itroduced by pull #1688. Because 11497 is now fixed.

https://github.com/D-Programming-Language/phobos/commit/9684fc2f8ebb07bb3f87e1f48fccd1869074a1f5
Merge pull request #1714 from monarchdodra/revert11497

Revert "Workaround Issue 11497 - lambda in "static if"/"assert" prevent ...

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 23 2013