Bug 14975 – DMD refuses to inline even trivial struct constructors

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-08-29T00:11:00Z
Last change time
2015-09-05T07:49:06Z
Keywords
performance
Assigned to
nobody
Creator
thomas.bockman
See also
https://issues.dlang.org/show_bug.cgi?id=10286

Comments

Comment #0 by thomas.bockman — 2015-08-29T00:11:38Z
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 by thomas.bockman — 2015-08-29T00:21:15Z
I have confirmed this issue affects 32-bit x86 Linux, as well.
Comment #2 by bugzilla — 2015-08-29T01:41:55Z
A more complete example would be appreciated. Sometimes, it is something else causing the problem.
Comment #3 by thomas.bockman — 2015-08-29T03:48:35Z
(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 #4 by bugzilla — 2015-09-03T10:51:50Z
Comment #5 by github-bugzilla — 2015-09-04T15:27:08Z
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 by thomas.bockman — 2015-09-05T07:49:06Z
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.