0 cost template instantiation

Basile B. b2.temp at gmx.com
Thu Sep 30 02:08:10 UTC 2021


On Thursday, 30 September 2021 at 02:02:19 UTC, Hipreme wrote:
> On Thursday, 30 September 2021 at 01:56:42 UTC, Basile B. wrote:
>> 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.
>>
>> cant you just use a regular functions ? loading happens at 
>> runtime right ?
>
>
> The entire reason to having that function is having that syntax 
> which would pretty much do the monkey's job for me:
>
> Instead of writing
>
> myFunction = cast(typeof(myFunction))_loadSymbol(_dll, 
> "myFunction");
>
> I could write
>
> loadSymbol!myFunction;
>
> But if no other way is found of doing that, I will do the 
> massive rewriting.
>
> Anyway, I don't think the problem is not in the way I'm doing, 
> but the output, as that template could easily be inlined

well ok. Maybe try to see if overloads

   void loadSymbol(alias s)()
   void loadSymbol(alias s, string symName)()

helps. That save the static if. In addition there's the null 
sentinel that can save a bit of memory, as you've been suggested.


More information about the Digitalmars-d-learn mailing list