On inlining in D libraries

Dmitry Olshansky dmitry.olsh at gmail.com
Mon Sep 9 06:01:40 PDT 2013


While investigating std.regex performance in Phobos I've found that a 
lot of stuff never gets inlined (contrary to my expectations).

Namely the 3 critical ones were declared like this:
struct Bytecode{
     uint raw;
     //bit twiddling helpers
     @property uint data() const { return raw & 0x003f_ffff; }

     //ditto
     @property uint sequence() const { return 2 + (raw >> 22 & 0x3); }

     //ditto
     @property IR code() const { return cast(IR)(raw>>24); }
     ...
}

And my quick hack to get them inlined - 0-arg templates:
https://github.com/D-Programming-Language/phobos/pull/1553

The "stuff" in question turns out to be anything that is not a template 
and (consequently) is compiled into library. At first I thought it's a 
horrible bug somewhere in DMD's inliner,  but this behavior is the same 
regardless of compiler. (It could be a bug of the front-end in general)

Few days after filing the bug report with minimal test case:
http://d.puremagic.com/issues/show_bug.cgi?id=10985

I'm  not so sure if that's not an issue of separate compilation to begin 
with. I *thought* that the intended behavior is
a) Have source - compile from source
b) Don't have source (*.di files) - link in objects

But I don't have much to go on this. Somebody from compiler team could 
probably shed some light on this. If I'm wrong then 0-arg templates is 
actually the only way out to get 'explicitly inline' of C++.

In C++ that would look like this:
//header
struct A{
	int foo();
}
//source
int A::foo(){ ... }

C++ explicitly inlined:
//header
struct A{
	int foo(){ ... }
}

In D we don't have this distinction.
It has to be decided then if we adopt 0-arg as intended solution, or 
tweak front-end to always peruse accessible source when inlining.

Anyhow as it stands you have one of the following:
a) Do nothing. Then using e.g. isAlpha from std.ascii (or pick your 
favorite one-liner) is useless as it would never outperform a 
hand-rolled version (that could be 1:1 the same) because the latter will 
be inlined.
b) Pass all of the interesting files from Phobos on the command line to 
get them fully scanned for inlining (and get compiled anew each time I 
guess).
c) For code under your control - add an empty pair of brackets to 
anything that has to be inlined.

None of the above options is nice.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list