[Issue 11497] New: lambda in "static if"/"assert" prevent inlining of function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Nov 11 13:07:22 PST 2013


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 at puremagic.com
        ReportedBy: monarchdodra at gmail.com


--- Comment #0 from monarchdodra at 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: -------


More information about the Digitalmars-d-bugs mailing list