Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's NOT in an interface file?

I was under the impression that "A D interface file contains only what an import of the module needs, rather than the whole implementation of that module." To me, that translates to signatures - just return types, names and arguments, so that the compiler knows it's valid and the linker can do the dirty work later.

Running a file through dmd, though, strips almost nothing:

import std.stdio;

void SayHello(const string Name)
{
    writeln("Hello, ", Name, "!");
}

dmd Interface.d -o- -H

// D import file generated from 'Interface.d'
import std.stdio;
void SayHello(const string Name)
{
writeln("Hello, ",Name,"!");
}

Hardly a paragon of optimization.

What, exactly, is stripped in interface files?

(header-files added because it's the closest thing I could find.)

like image 396
Maxpm Avatar asked Oct 11 '11 01:10

Maxpm


2 Answers

Any function which is going to be inlined must have its full source in the .di file. Any function which is going to be used in CTFE must not only have its full source in the .di file, but the full source of every function that it uses - directly or indirectly - must be available to the compiler. Also, because of how templates work, their full source must be in the .di file as well (which is the same as how templates must be in header files in C++). So, there are a number of cases where you need stuff to be in a .di file.

Under exactly what circumstances the compiler chooses to strip stuff or not, I don't know (aside from the fact that templates automatically end up in .di files in their entirety because they have to). It could change depending on the compiler's current implementation and what optimizations it does. But at minimum, it's going to have to leave in small function bodies if it's going to do any inlining. Large function bodies and the bodies of small virtual functions (which can't be inlined anyway) will likely be stripped out however. But your example gives a small, non-virtual function, so dmd likely left it in so that it could inline any calls to it. If you want to see dmd strip a lot of stuff when generating a .di file, then you probably need to have large functions and/or use classes.

like image 88
Jonathan M Davis Avatar answered Sep 28 '22 09:09

Jonathan M Davis


Hardly a paragon of optimization.

No, that is an optimization. The compiler will leave the implementation in the interface file if the implementation is small enough that it can later be inlined.

like image 24
Andrej Mitrović Avatar answered Sep 28 '22 09:09

Andrej Mitrović