Issue 14975 - DMD refuses to inline even trivial struct constructors
Summary: DMD refuses to inline even trivial struct constructors
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 enhancement
Assignee: No Owner
URL:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2015-08-29 00:11 UTC by thomas.bockman
Modified: 2015-09-05 07:49 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description thomas.bockman 2015-08-29 00:11:38 UTC
While doing some refactoring and updating [CheckedInt](https://github.com/tsbockman/CheckedInt) for DMD 2.068, I have discovered one major source of slowness: DMD cannot inline even trivial struct constructors:

// Error: constructor foo.this cannot inline function
struct foo {
    int bar;

    pragma(inline, true) this(int bar) {
        this.bar = bar;
    }
}

Refactoring my code to reduce the use of struct constructors yielded a 2x speed boost. The workaround is stupidly simple, though ugly:

struct foo {
    int bar;

    pragma(inline, true) static auto inline_cons(int bar) {
        foo ret = void;
        ret.bar = bar;
        return ret;
    }
}
Comment 1 thomas.bockman 2015-08-29 00:21:15 UTC
I have confirmed this issue affects 32-bit x86 Linux, as well.
Comment 2 Walter Bright 2015-08-29 01:41:55 UTC
A more complete example would be appreciated. Sometimes, it is something else causing the problem.
Comment 3 thomas.bockman 2015-08-29 03:48:35 UTC
(In reply to Walter Bright from comment #2)
> A more complete example would be appreciated. Sometimes, it is something
> else causing the problem.

More complete in what sense?

From my experimentation, this code will fail to compile with DMD in release mode regardless of the context, but I can slap a main() function on it and make it a complete program, if you want:


module testd2;

import std.stdio;

struct foo {
    int bar;

    // Error: constructor testd2.foo.this cannot inline function
    pragma(inline, true) this(int bar) {
        this.bar = bar;
    }
}

void main(string[] args) {
    foo baz = 1;
    writeln(baz.bar);
}
Comment 5 github-bugzilla 2015-09-04 15:27:08 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/545572134f613433f0efe3aaa247b3f401a02aa9
fix Issue 14975 - DMD refuses to inline even trivial struct constructors

https://github.com/D-Programming-Language/dmd/commit/68650c1df4608a10be47e5cfe0610babe7bd2f47
Merge pull request #5033 from WalterBright/fix14975

fix Issue 14975 - DMD refuses to inline even trivial struct constructors
Comment 6 thomas.bockman 2015-09-05 07:49:06 UTC
I have tested, and PR #5033 does indeed fix this issue for me both in my simple test case, and also in the CheckedInt code where I originally discovered it. Thanks.