digitalmars.D - Compile / runtime differences for using import statements
- Selim Ozel (19/19) Apr 20 2023 I've been wondering if there is any difference between importing
- Adam D Ruppe (11/14) Apr 20 2023 There's no difference at link or runtime.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (4/12) Apr 22 2023 I remember comparisons where selective imports reduced compilation
- Adam D Ruppe (4/6) Apr 22 2023 It shouldn't be. Do you remember where that was?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (3/4) Apr 22 2023 Ah! I think that was it.
- Walter Bright (2/7) Apr 28 2023 That's because internally the compiler rewrites it as an alias!
- Max Samukha (2/3) Apr 28 2023 Which is unfortunate and causes a ton of subtle problems.
- Salih Dincer (34/40) Apr 28 2023 Hello everyone, I had the opportunity to try the difference
I've been wondering if there is any difference between importing a function/component from a module as stand-alone or importing the whole thing makes any difference. Just looking at the assembly output in godbolt suggest they create same instructions but I wonder if it makes any difference at compilation/linking etc. https://godbolt.org/ ``` // Import writeln individually import std.stdio: writeln; void main() { writeln("hi"); } // Import all std.stdio import std.stdio; void main() { writeln("hi"); } ```
Apr 20 2023
On Thursday, 20 April 2023 at 23:04:14 UTC, Selim Ozel wrote:I've been wondering if there is any difference between importing a function/component from a module as stand-alone or importing the whole thing makes any difference.There's no difference at link or runtime. The only difference is how the name works. The selective imports work as if you wrote: static import std.stdio; alias writeln = std.stdio.writeln; So the module is always imported as a whole but then the names are hidden except for the one you specified. But the one you specified gets some weird treatment because the compiler really does treat it as if you wrote a local alias, so it can get priority over names from other imports when disambiguating.
Apr 20 2023
On 4/20/23 17:15, Adam D Ruppe wrote:I remember comparisons where selective imports reduced compilation times. Is that not true? Aliimporting a function/component from a module as stand-alone or importing the whole thing makes any difference.There's no difference at link or runtime. The only difference is how the name works. The selective imports work as if you wrote: static import std.stdio; alias writeln = std.stdio.writeln;
Apr 22 2023
On Saturday, 22 April 2023 at 16:15:08 UTC, Ali Çehreli wrote:I remember comparisons where selective imports reduced compilation times. Is that not true?It shouldn't be. Do you remember where that was? Local imports can make a difference so maybe it was conflated with that.
Apr 22 2023
On 4/22/23 10:44, Adam D Ruppe wrote:Local imports can make a difference so maybe it was conflated with that.Ah! I think that was it. Ali
Apr 22 2023
On 4/20/2023 5:15 PM, Adam D Ruppe wrote:The selective imports work as if you wrote: static import std.stdio; alias writeln = std.stdio.writeln;That's because internally the compiler rewrites it as an alias!
Apr 28 2023
On Saturday, 29 April 2023 at 04:04:49 UTC, Walter Bright wrote:That's because internally the compiler rewrites it as an alias!Which is unfortunate and causes a ton of subtle problems.
Apr 28 2023
On Thursday, 20 April 2023 at 23:04:14 UTC, Selim Ozel wrote:I've been wondering if there is any difference between importing a function/component from a module as stand-alone or importing the whole thing makes any difference. Just looking at the assembly output in godbolt suggest they create same instructions but I wonder if it makes any difference at compilation/linking etc.Hello everyone, I had the opportunity to try the difference between C and D within the subject. Here are code and the results: ```d //import core.stdc.stdio : printArray = puts; /* version1 static import core.stdc.stdio; alias printArray = core.stdc.stdio.puts; //* version2 */ void main() { char[4] arr = ['4', '2', '\n', '\0']; arr.ptr.printArray; } ``` Both versions gave the same result... **Command:** ldc2 staticImport.d -release -m64 -O **Size:** 8.232 bytes In DMD, the size have grown as the runtime is involved! **Command:** dmd staticImport.d -release -m64 -O **Size:** 897.968 bytes --- ```c #include <stdio.h> void main() { char arr[4] = {'4', '2', '\n', '\0'}; printf("%s", &arr); } ``` When we converted this simple code to C and used the DMD compiler again, it fell in size for some reason. **Command:** dmd staticImport.c -release -m64 -O **Size:** 776.720 bytes By the way, when we removed the static statement, I did not observe a compilation error or a different result. In summary, I like to use alias, especially in one line with import. SDB 79
Apr 28 2023