Jump to page: 1 25  
Page
Thread overview
D import idiom compilation time
Jan 03, 2019
SrMordred
Jan 04, 2019
Jonathan Marler
Jan 04, 2019
Jonathan Marler
Jan 04, 2019
Rubn
Jan 04, 2019
Simen Kjærås
Jan 05, 2019
0xEAB
Jan 05, 2019
Ethan
Jan 05, 2019
Ethan
Jan 04, 2019
bauss
Jan 05, 2019
H. S. Teoh
Jan 05, 2019
Neia Neutuladh
Jan 05, 2019
H. S. Teoh
Jan 05, 2019
Neia Neutuladh
Jan 05, 2019
Dgame
Jan 05, 2019
Neia Neutuladh
Jan 05, 2019
Dgame
Jan 05, 2019
H. S. Teoh
Jan 06, 2019
Daniel N
Jan 06, 2019
FR86
Jan 06, 2019
Daniel N
Jan 06, 2019
FR86
Jan 07, 2019
Seb
Jan 07, 2019
FR86
Jan 08, 2019
Adam D. Ruppe
Jan 08, 2019
H. S. Teoh
Jan 08, 2019
H. S. Teoh
Jan 08, 2019
JN
Jan 08, 2019
Johan Engelen
Jan 08, 2019
Jonathan M Davis
Jan 09, 2019
Jonathan Marler
Jan 09, 2019
Dgame
Jan 09, 2019
Neia Neutuladh
Jan 10, 2019
Jonathan Marler
Jan 08, 2019
Nick Treleaven
January 03, 2019
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!
January 04, 2019
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




January 04, 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.

January 04, 2019
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!

I like this idiom way more than the other nasty from!"..." syntax. Though you need to define a struct for any library you'd want to use it with, not that big of a deal. Thanks for sharing.
January 04, 2019
On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
> struct _std
> {
>   template opDispatch(string moduleName)
>   {
>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>   }
> }


There is nothing that's actually stopping you from just calling it std.

It will work just as fine.

That way you can end up with

std.stdio.writeln("...");

Instead of:
_std.stdio.writeln("...");
January 04, 2019
On Friday, 4 January 2019 at 13:07:14 UTC, Rubn 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!
>
> I like this idiom way more than the other nasty from!"..." syntax. Though you need to define a struct for any library you'd want to use it with, not that big of a deal. Thanks for sharing.

The struct could of course be templated:

struct from(string namespace) {
    template opDispatch(string subnamespace) {
        mixin("import opDispatch = "~namespace~"."~subnamespace~";");
    }
}

unittest {
    alias std = from!"std";
    std.stdio.writeln("ohai");
}

And slightly more fancy, for those pesky nested packages:


struct from(string namespace) {
    static if (__traits(compiles, { mixin("import "~namespace~";"); })) {
        mixin("import __from = "~namespace~";");
    }
    template opDispatch(string subnamespace) {
        static if (__traits(compiles, { mixin("import "~namespace~"."~subnamespace~";"); })) {
            alias opDispatch = .from!(namespace~"."~subnamespace);
        } else {
            mixin("alias opDispatch = __from."~subnamespace~";");
        }
    }
}

unittest {
    alias std = from!"std";

    std.stdio.writeln("ohai");

    // This would be impossible with the first version:
    static assert(std.range.primitives.isInputRange!(int[]));

    // As would single-level imports:
    alias myLibrary = from!"myLibrary";
    myLibrary.someFunction();
}

--
  Simen
January 04, 2019
On 1/4/19 8:11 AM, bauss wrote:
> On Thursday, 3 January 2019 at 23:54:42 UTC, SrMordred wrote:
>> struct _std
>> {
>>   template opDispatch(string moduleName)
>>   {
>>     mixin("import opDispatch = std." ~ moduleName ~ ";");
>>   }
>> }
> 
> 
> There is nothing that's actually stopping you from just calling it std.
> 
> It will work just as fine.
> 
> That way you can end up with
> 
> std.stdio.writeln("...");
> 
> Instead of:
> _std.stdio.writeln("...");

This is quite amazing.
January 04, 2019
On 1/4/19 1:04 PM, Simen Kjærås wrote:
[snip]

So I guess we could define a short module called "std.autostd" such that all uses of std.xxx would be resolved. Wow,
January 05, 2019
On Saturday, 5 January 2019 at 01:58:15 UTC, Andrei Alexandrescu wrote:
> On 1/4/19 1:04 PM, Simen Kjærås wrote:
> [snip]
>
> So I guess we could define a short module called "std.autostd" such that all uses of std.xxx would be resolved. Wow,

+1, awesome idea.

 - Elias
January 05, 2019
On Saturday, 5 January 2019 at 01:58:15 UTC, Andrei Alexandrescu wrote:
> On 1/4/19 1:04 PM, Simen Kjærås wrote:
> [snip]
>
> So I guess we could define a short module called "std.autostd" such that all uses of std.xxx would be resolved. Wow,

Looking at the above examples, and my own programming patterns lately. Every time I need something, I add it to my import statement, ie import std.algorithm : min, max, map, remove, sort;

Having each invocation import only that symbol defined at the end would be ideal. Something like:

struct from( string thismodule )
{
  template opDispatch( string symbol )
  {
      static if( __traits( compiles, { mixin( "import " ~ thismodule ~ ";" ); } )
                && __traits( compiles, { mixin( "import " ~ thismodule ~ " : " ~ symbol ~ ";" ); } ) )
    {
      mixin( "import " ~ thismodule ~ " : " ~ symbol ~ ";");
      mixin( "alias opDispatch = " ~ symbol ~ ";" );
    }
    else
    {
      alias opDispatch = from!( thismodule ~ "." ~ symbol );
    }
  }
}

int main( string[] args )
{
    alias std = from!"std";

    std.stdio.writeln( "It works!" );
    return 0;
}

« First   ‹ Prev
1 2 3 4 5