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; } }
I have confirmed this issue affects 32-bit x86 Linux, as well.
A more complete example would be appreciated. Sometimes, it is something else causing the problem.
(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); }
https://github.com/D-Programming-Language/dmd/pull/5033
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
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.