www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Weird template instantiation speed?

reply IchorDev <zxinsworld gmail.com> writes:
While working on some new bindings, I've discovered that if 
`opAssign` in a struct template "`BindingTempl(T)`" has the 
return type "`BindingTempl!T` then it adds about 4 seconds to the 
compile time per instantiation of `BindingTempl`. The added 
compile time is much lower if a function other than `opAssign` 
returns `BindingTempl!T`. Is opAssign a particularly bad operator 
to overload in templates or something?
Jul 09 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/9/23 7:54 AM, IchorDev wrote:
 While working on some new bindings, I've discovered that if `opAssign` 
 in a struct template "`BindingTempl(T)`" has the return type 
 "`BindingTempl!T` then it adds about 4 seconds to the compile time per 
 instantiation of `BindingTempl`. The added compile time is much lower if 
 a function other than `opAssign` returns `BindingTempl!T`. Is opAssign a 
 particularly bad operator to overload in templates or something?
This is probably a bug somewhere, 4 seconds is too much. A reduced test case would be helpful. But I wanted to note, inside a struct template, the template name (by itself) is equivalent to the current instantiation. So just returning `BindingTempl` would be equivalent, and might not trigger this problem. See if that helps. -Steve
Jul 09 2023
parent IchorDev <zxinsworld gmail.com> writes:
On Sunday, 9 July 2023 at 14:49:39 UTC, Steven Schveighoffer 
wrote:
 This is probably a bug somewhere, 4 seconds is too much. A 
 reduced test case would be helpful.

 But I wanted to note, inside a struct template, the template 
 name (by itself) is equivalent to the current instantiation. So 
 just returning `BindingTempl` would be equivalent, and might 
 not trigger this problem.

 See if that helps.

 -Steve
Thank you for letting me know about being able to use `BindingTempl`, I had no idea! Unfortunately it doesn't mitigate the compile times, though. I forgot to mention, it only happens when specifying `-O` with DMD. LDC and GDC compile the same thing almost instantly. Boiling it down to a simple test is tough. If you remove a lot of components the struct template depends on then the compile time is too fast for anything to be noticeable. I think the issue is some kind of snowball-effect. If anyone wants to try to reproduce this issue: 1. Download [this exact commit](https://github.com/ichordev/bindbc-imgui/tree/65d02d68e4188250c9481 7a04a5820de3479a44) of the WIP BindBC-ImGui repo. (the newer commits won't compile) 2. Edit dub.selections.json to change `"bindbc-common"`'s version to `"0.0.6"`. 3. Run: ``` dmd -extern-std=c++11 -lib -O -version=BindImGui_Static -Isource/ -I~/.dub/packages/bindbc-common/0.0.6/bindbc-common/source/ source/bindbc/imgui/config.d source/bindbc/imgui/package.d source/imgui/impl.d source/imgui/package.d -v ``` 4. Now, remove `-O` from the dmd command. Blink and you'll miss it compiling!
Jul 09 2023