Thread overview
Compile / runtime differences for using import statements
Apr 20, 2023
Selim Ozel
Apr 21, 2023
Adam D Ruppe
Apr 22, 2023
Ali Çehreli
Apr 22, 2023
Adam D Ruppe
Apr 22, 2023
Ali Çehreli
Apr 29, 2023
Walter Bright
Apr 29, 2023
Max Samukha
Apr 28, 2023
Salih Dincer
April 20, 2023

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");
}
April 21, 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.

April 22, 2023
On 4/20/23 17:15, Adam D Ruppe wrote:
>> 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;

I remember comparisons where selective imports reduced compilation times. Is that not true?

Ali

April 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.
April 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

April 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:

//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


#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

April 28, 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!

April 29, 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.