[Issue 6528] Private module functions optimizations

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Mar 9 22:51:35 PST 2017


https://issues.dlang.org/show_bug.cgi?id=6528

Johannes Pfau <johannespfau at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |johannespfau at gmail.com

--- Comment #4 from Johannes Pfau <johannespfau at gmail.com> ---
Reposting from the newsgroup:

--------------------------------------------------------
// a.d
private void fooPrivate() {}

/*template*/ void fooPublic(string func = "fooPrivate")()
{
    mixin(func ~ "();");
}
--------------------------------------------------------

When compiling a.d we haven't analyzed the fooPublic template and the
example shows why we can't know at all which private functions could
be called from a template. As the template is instantiated into another
object file (e.g. module b.d) we can't know in a.d that fooPrivate is
actually required.

So does that mean removing private functions in D is completely
impossible as we can't know if a function is unused? People sometimes
refer to the linker as a solution but if a.d is in a shared library
this won't work either.

This seems to be a problem especially for CTFE only functions, as it
means for example that any such function in phobos (e.g. used for
string creation for mixins) bloats the phobos library.

It's interesting to think about template instances here as
well: If a template instance is completely inlined in a module, do we
have to keep the function in the object file? AFAICS no, as the
template should be re-instantiated if used in a different module, but I
don't know the template <=> object file rules in detail. Right now this
means we could get lots of template instances in the phobos shared
library for template instances only used in CTFE:

--------------------------------------------------------
import std.conv;
private string fooPrivate(int a)
{
    return `int b = ` ~ to!string(a) ~";";
}
mixin(fooPrivate(42));
--------------------------------------------------------
https://godbolt.org/g/VW8yLr


Any idea to measure the impact of this on the binary shared libphobos
file? We probably can get some estimate by counting all template
instances that are only referenced by private functions which are
themselves never referenced...

I think the same problem was mentioned in the DLL-support context as this
implies we also have to export private functions from modules for templates to
work. Was there some kind of solution / discussion? I think I remember
something about marking `private` functions as `export private` instead?

--


More information about the Digitalmars-d-bugs mailing list