www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - A New Import Idiom`

reply Mike Parker <aldacron gmail.com> writes:
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
Feb 13 2017
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
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;
Feb 13 2017
parent reply Daniel N <no public.email> writes:
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.
Feb 13 2017
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
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.
Feb 13 2017
parent Daniel N <no public.email> writes:
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!
Feb 13 2017
prev sibling parent reply Johan Engelen <j j.nl> writes:
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
Feb 13 2017
parent reply Daniel Nielsen <daniel.nielsen.se gmail.com> writes:
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
Feb 13 2017
parent reply Johan Engelen <j j.nl> writes:
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
Feb 13 2017
parent Daniel Nielsen <daniel.nielsen.se gmail.com> writes:
On Monday, 13 February 2017 at 21:04:05 UTC, Johan Engelen wrote:
 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
Sorry, I misread your post. In Jack Stouffer test, main() was empty!
Feb 13 2017
prev sibling next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
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!
Feb 13 2017
prev sibling next sibling parent Johan Engelen <j j.nl> writes:
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
Feb 13 2017
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 02/13/2017 06:28 AM, Mike Parker wrote:

 https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/
I claimed there is a performance improvement in compilation. Can someone answer kibwen's question on this subthread please: https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/ddp6a4p/ Ali
Feb 13 2017
parent reply Jerry <hurricane hereiam.com> writes:
On Monday, 13 February 2017 at 22:40:55 UTC, Ali Çehreli wrote:
 On 02/13/2017 06:28 AM, Mike Parker wrote:

 https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/
I claimed there is a performance improvement in compilation. Can someone answer kibwen's question on this subthread please: https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/ddp6a4p/ Ali
I ran it a couple of times and just doing the following has a faster compile time: import std.datetime : SysTime; import std.traits : isIntegral; No difference in binary size as the article stated either, using dmd. Anyways yes this is kind of cool and fascinating how it works, but that aside I hope I never see this used in phobos. Does anyone else feel this way?
Feb 13 2017
next sibling parent Daniel N <no public.email> writes:
On Tuesday, 14 February 2017 at 02:32:33 UTC, Jerry wrote:
 On Monday, 13 February 2017 at 22:40:55 UTC, Ali Çehreli wrote:
 On 02/13/2017 06:28 AM, Mike Parker wrote:

 https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/
I claimed there is a performance improvement in compilation. Can someone answer kibwen's question on this subthread please: https://www.reddit.com/r/programming/comments/5tt33y/a_new_import_idiom_for_d/ddp6a4p/ Ali
I ran it a couple of times and just doing the following has a faster compile time: import std.datetime : SysTime; import std.traits : isIntegral; No difference in binary size as the article stated either, using dmd. Anyways yes this is kind of cool and fascinating how it works, but that aside I hope I never see this used in phobos. Does anyone else feel this way?
I suspect you test something slightly different, I tested both on linux and windows, with dmd, ldc and gdc(not on windows). time dmd slow.d time dmd fast.d ====== Slow ====== import std.datetime : SysTime; import std.traits : isIntegral; void func(T)(SysTime a, T value) if (isIntegral!T) { import std.stdio : writeln; writeln(a, value); } void main() {} ====== Fast ====== 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() {}
Feb 13 2017
prev sibling next sibling parent kinke <noone nowhere.com> writes:
On Tuesday, 14 February 2017 at 02:32:33 UTC, Jerry wrote:
 Anyways yes this is kind of cool and fascinating how it works, 
 but that aside I hope I never see this used in phobos. Does 
 anyone else feel this way?
Yes. +1
Feb 14 2017
prev sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 2/14/17 3:32 AM, Jerry wrote:
 Anyways yes this is kind of cool and fascinating how it works, but that
 aside I hope I never see this used in phobos. Does anyone else feel this
 way?
+1 Let's not make Phobos as scary as C++ STL. --- Dmitry Olshansky
Feb 14 2017
next sibling parent Jonathan M Davis via Digitalmars-d-announce writes:
On Wednesday, February 15, 2017 00:01:42 Dmitry Olshansky via Digitalmars-d-
announce wrote:
 On 2/14/17 3:32 AM, Jerry wrote:
 Anyways yes this is kind of cool and fascinating how it works, but that
 aside I hope I never see this used in phobos. Does anyone else feel this
 way?
+1 Let's not make Phobos as scary as C++ STL.
Honestly, as hideous as functions signatures can get - particularly with heavily templated stuff, I think that the result tends to be far more understandable than the likes of the STL of Boost. That being said, I think that D function signaures are certainly pushing it as it is, and I don't think that the benefits of adding imports to them is worth the clutter that they add. - Jonathan M Davis
Feb 14 2017
prev sibling parent Arun Chandrasekaran <aruncxy gmail.com> writes:
On Tuesday, 14 February 2017 at 23:01:42 UTC, Dmitry Olshansky 
wrote:
 On 2/14/17 3:32 AM, Jerry wrote:
 Anyways yes this is kind of cool and fascinating how it works, 
 but that
 aside I hope I never see this used in phobos. Does anyone else 
 feel this
 way?
+1 Let's not make Phobos as scary as C++ STL. --- Dmitry Olshansky
Honestly, after staring at C++ STL, I never imaged that the standard library of a language can be *readable* and *understandable* until I read phobos. Kudos to Walter, Andrei and contributors. I still believe that templated D code is much more readable and intuitive than templated C++ code. Arun
Feb 14 2017
prev sibling parent Dominikus Dittes Scherkl <Dominikus.Scherkl continental-corporation.com> writes:
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
Thanks for writing this up, Daniel. Has become quite a nice article.
Feb 14 2017