Force inline

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Feb 20 05:16:15 PST 2017


On Monday, February 20, 2017 12:47:43 berni via Digitalmars-d-learn wrote:
> with
>
> > dmd -inline test.d
>
> I get
>
> > test.d(8): Error: function test.foo cannot inline function
>
> When I remove -inline, it compiles, but seems not to inline. I
> cannot tell from this small example, but with the large program,
> there is no speed gain.
>
> It also compiles with -inline when I remove the "if (bar==2)...".
> I guess, it's now really inlining, but the function is
> ridiculously short...

For better or worse, the whole point of pragma(inline, true) is to produce
an error when the compiler fails to inline the function. It doesn't force
inlining in any way. So, the fact that it produces an error means that the
compiler can't inline that function. And it's not going to inline if you're
not using -inline.

The reality of the matter is that the inliner in the D frontend needs some
serious work. So, it's not going to do a very good job. It's better than
nothing, but in comparison to what you'd see with your typical C++ compiler,
it just isn't as good. Also, there are a number of compiler bugs that get
triggered when both -O and -inline are enabled. So, you're likely better off
just using -O for now.

Regardless, if performance is your #1 concern, then I would suggest that you
compile with ldc and not dmd. dmd is great for fast compilation and
therefore it's great for development. However, while it produces decent
binaries, and it may very well do certain optimizations better than the gcc
or llvm backends do, on the whole, dmd's optimizer really can't compare with
those of gcc or llvm. ldc almost always produces a faster binary than dmd
does (though it does take longer to compile).

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list