D import idiom compilation time

Jonathan Marler johnnymarler at gmail.com
Fri Jan 4 02:52:27 UTC 2019


On Friday, 4 January 2019 at 02:43:01 UTC, Jonathan Marler wrote:
> On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
>> There is a veredict about the compilation speedup of the "New 
>> Import Idiom"?
>>
>> Currently i´m using it this way:
>>
>> struct _std
>> {
>>   template opDispatch(string moduleName)
>>   {
>>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>>   }
>> }
>> ...
>> //if used single time on scope.
>> _std.algorithm.max(a,b);
>>
>> My question is that, in the long run, this will be worth the 
>> compilation time gains, or is just negligible and I should 
>> just import the normal way.
>> (and keep the code more sane)
>>
>> Thanks!
>
>
> Yes.
>
>
> --- testPerf.d
>
> struct fromStd
> {
>     template opDispatch(string moduleName)
>     {
>         mixin("import opDispatch = std." ~ moduleName ~ ";");
>     }
> }
>
> version (UseFrom)
> {
>     auto foo(T)(T arg) if (fromStd.typecons.isBitFlagEnum!T)
>     {
>         return arg;
>     }
> }
> else
> {
>     import std.typecons : isBitFlagEnum;
>     auto foo(T)(T arg) if (isBitFlagEnum!T)
>     {
>         return arg;
>     }
> }
>
>
>
> $ time dmd -c testperf.d
>
> real	0m0.050s
>
> $ time dmd -c -version=UseFrom testperf.d
>
> real	0m0.016s

I should clarify.  It depends what type of code you are writing.  
If you are writing library code in which only small parts of 
modules may be used then importing this way will allow apps to 
use your library while only importing the modules they need.

If you are writing program code where you don't need to expose 
subsets of code to other modules, you just want to use library 
code then in most cases there's no benefit to lazily importing 
other modules since you're always going to need them.



More information about the Digitalmars-d mailing list