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



(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




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




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




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





 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



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




 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




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


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



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




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


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




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.



https://github.com/D-Programming-Language/phobos/commit/9684fc2f8ebb07bb3f87e1f48fccd1869074a1f5


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