0 cost template instantiation
Hipreme
msnmancini at hotmail.com
Thu Sep 30 02:09:50 UTC 2021
On Thursday, 30 September 2021 at 01:09:47 UTC, Hipreme wrote:
> I have a template function that all it does is given a symbol,
> it loads a dll for its type + its name:
>
> ```
> void loadSymbol(alias s, string symName = "")()
> {
> static if(symName == "")
> s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
> else
> s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
> }
> ```
>
>
> The main problem is that this function is costing 2KB per
> instantiation, which is something pretty high. Specially if I
> inline, there is almost no cost when compared to its inline
> version. Trying to use pragma(inline, true) didn't do anything
> too.
>
> If I understood correctly, mixin template would be some way to
> actually inline anything. The problem is that I can't have any
> kind of expression inside it, so, that's the only way I could
> think in how to do it.
>
> Optimization seems to don't take care of that behaviour too.
>
> I would like to know which approach could I take for making
> something like C's #define for that.
>
> As this function could be really rewritten as a macro if I were
> coding in C, the cost is too high for a function that would be
> only a syntactic sugar
I could reduce by almost 100kb of code by instead of using the
former option, I use:
```
void loadSymbols(Ts...)()
{
static foreach(s; Ts)
s = cast(typeof(s))_loadSymbol(_dll, s.stringof);
}
```
Then instead of making a call for each one, I just pass a list of
templates, this would only create one instantiation per need.
I do still don't think that this should be the answer, as the
alias problem is not yet solved. It takes a lot of size for
functions that could be only syntactic sugar, where C would be a
lot better
More information about the Digitalmars-d-learn
mailing list