Jump to page: 1 2
Thread overview
A New Import Idiom`
Feb 13, 2017
Mike Parker
Feb 13, 2017
jmh530
Feb 13, 2017
Daniel N
Feb 13, 2017
jmh530
Feb 13, 2017
Daniel N
Feb 13, 2017
Johan Engelen
Feb 13, 2017
Daniel Nielsen
Feb 13, 2017
Johan Engelen
Feb 13, 2017
Daniel Nielsen
Feb 13, 2017
Bastiaan Veelo
Feb 13, 2017
Johan Engelen
Feb 13, 2017
Ali Çehreli
Feb 14, 2017
Jerry
Feb 14, 2017
Daniel N
Feb 14, 2017
kinke
Feb 14, 2017
Dmitry Olshansky
Feb 14, 2017
Jonathan M Davis
February 13, 2017
Daniel Nielsen put together a post describing the import idiom that came to light in a recent forum discussion regarding DIP 1005 [3]. The relevant links are at [1] and [2].

[1] https://dlang.org/blog/2017/02/13/a-new-import-idiom/
[2] https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/
[3] https://forum.dlang.org/thread/tzqzmqhankrkbrfsrmbo@forum.dlang.org?page=1


February 13, 2017
On Monday, 13 February 2017 at 14:28:20 UTC, Mike Parker wrote:
> Daniel Nielsen put together a post describing the import idiom that came to light in a recent forum discussion regarding DIP 1005 [3]. The relevant links are at [1] and [2].
>
> [1] https://dlang.org/blog/2017/02/13/a-new-import-idiom/
> [2] https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/
> [3] https://forum.dlang.org/thread/tzqzmqhankrkbrfsrmbo@forum.dlang.org?page=1

A great post.

With the Jack Stouffer comparison, wouldn't it be fairer to do:

import std.datetime : Systime;
import std.traits : isIntegral;
February 13, 2017
On Monday, 13 February 2017 at 15:00:24 UTC, jmh530 wrote:
> On Monday, 13 February 2017 at 14:28:20 UTC, Mike Parker wrote:
>> Daniel Nielsen put together a post describing the import idiom that came to light in a recent forum discussion regarding DIP 1005 [3]. The relevant links are at [1] and [2].
>>
>> [1] https://dlang.org/blog/2017/02/13/a-new-import-idiom/
>> [2] https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/
>> [3] https://forum.dlang.org/thread/tzqzmqhankrkbrfsrmbo@forum.dlang.org?page=1
>
> A great post.
>
> With the Jack Stouffer comparison, wouldn't it be fairer to do:
>
> import std.datetime : Systime;
> import std.traits : isIntegral;

Probably, please help measuring your idea and post it here. I intentionally chose a test made by someone else to avoid biasing from my part. It was the only test I found at time of writing.

February 13, 2017
On Monday, 13 February 2017 at 15:04:53 UTC, Daniel N wrote:
>
> Probably, please help measuring your idea and post it here. I intentionally chose a test made by someone else to avoid biasing from my part. It was the only test I found at time of writing.

The first time I did it, I got a ~60% decline in compilation speed and binary size. I called dmd without any extra settings after making the above change (and adding a main function). It seemed weird that it's even better than in his comparison. Maybe he had used some settings I hadn't.

So I tried it again comparing all three versions on my machine. This time I didn't notice a difference in compilation speed or binary size between the version with

import std.datetime;
import std.traits;

and the one with

import std.datetime : SysTime;
import std.traits : isIntegral;

So I'm not sure it matters.
February 13, 2017
On Monday, 13 February 2017 at 16:17:49 UTC, jmh530 wrote:
>
> So I tried it again comparing all three versions on my machine. This time I didn't notice a difference in compilation speed or binary size between the version with
>
> import std.datetime;
> import std.traits;
>
> and the one with
>
> import std.datetime : SysTime;
> import std.traits : isIntegral;
>
> So I'm not sure it matters.

Cool, thanks for checking!
February 13, 2017
On Monday, 13 February 2017 at 14:28:20 UTC, Mike Parker wrote:
> Daniel Nielsen put together a post
>
> [1] https://dlang.org/blog/2017/02/13/a-new-import-idiom/

An entertaining read!
February 13, 2017
On Monday, 13 February 2017 at 15:04:53 UTC, Daniel N wrote:
> On Monday, 13 February 2017 at 15:00:24 UTC, jmh530 wrote:
>>
>> With the Jack Stouffer comparison, wouldn't it be fairer to do:
>>
>> import std.datetime : Systime;
>> import std.traits : isIntegral;
>
> Probably, please help measuring your idea and post it here. I intentionally chose a test made by someone else to avoid biasing from my part. It was the only test I found at time of writing.

In case you don't actually use the function, things are faster and you get a smaller object file because the template is never instantiated. So if the function is never used in the module (it is just there for other modules to use), then things are indeed faster and smaller. But you also don't get compile checking on it; putting only
```
void func(T)(from!"std.datetime".SysTime a, T value)
    if (from!"std.traits".isIntegral!T)
{
    import std.stdio : writeln;
    writeln(a, value);
}
```
in a file compiles fast without complaints about an undefined `from`.

But for a full program, compile time is the same (it's really short so you can't really conclude much from it), and binary size as well (LDC 1.1.0, -O3). For my test cases below, the binaries are identical, except for around 30 bytes...

My test cases:
```
template from(string moduleName)
{
  mixin("import from = " ~ moduleName ~ ";");
}

void func(T)(from!"std.datetime".SysTime a, T value)
    if (from!"std.traits".isIntegral!T)
{
    import std.stdio : writeln;
    writeln(a, value);
}

void main()
{
    func!int(from!"std.datetime".SysTime(), 1);
}
```

```
import std.datetime;
import std.traits;

void func(T)(SysTime a, T value) if (isIntegral!T)
{
    import std.stdio : writeln;
    writeln(a, value);
}

void main()
{
    import std.datetime : SysTime;
    func!int(SysTime(), 1);
}
```

-Johan
February 13, 2017
On Monday, 13 February 2017 at 14:28:20 UTC, Mike Parker wrote:
> Daniel Nielsen put together a post ...
>
> [1] https://dlang.org/blog/2017/02/13/a-new-import-idiom/

I also liked the post, nicely written.

I didn't check, but if this idiom is not already tested in dmd-testsuite, please add it!

-Johan
February 13, 2017
On Monday, 13 February 2017 at 19:41:11 UTC, Johan Engelen wrote:
> But for a full program, compile time is the same (it's really short so you can't really conclude much from it), and binary size as well (LDC 1.1.0, -O3). For my test cases below, the binaries are identical, except for around 30 bytes...
>

You're disqualified for using LDC, it's too good. ;) Just kidding, well I did see it coming so I safeguarded with linker section. I still suspect there will be a difference in real-world applications even with LDC.

Glad you liked it.

Daniel
February 13, 2017
On Monday, 13 February 2017 at 20:09:45 UTC, Daniel Nielsen wrote:
> 
> I still suspect there will be a difference in real-world applications even with LDC.

Why would you expect there to be a difference?
Module ctors/dtors are still pulled in, regardless of the import being selective or not.
What am I missing?

Thanks,
  Johan

« First   ‹ Prev
1 2