phobos dependencies

Andrej Mitrovic andrej.mitrovich at gmail.com
Sat Dec 21 13:43:58 PST 2013


On 12/21/13, Dicebot <public at dicebot.lv> wrote:
> On Saturday, 21 December 2013 at 21:16:23 UTC, Andrei
> Alexandrescu wrote:
>> That's why I'm saying: make all imports lazy!!!!
>
> How? It has been already mentioned in this thread that this does
> not seem possible, at least withing existing language.

The first step is to avoid reading the imports at all *unless* there's
symbols missing. And then try to match selective imports first if
there are any missing symbols. To demonstrate, here's the first
test-case:

-----
module test;

import std.stdio : writeln;
import std.algorithm;

void main()
{
}
-----

The compiler *does not* need to import any other modules (other than
object.d, of course), because it doesn't find any missing symbols
referenced from "test.d".

Test-case 2:
-----
import std.stdio : writeln;
import std.algorithm;

void main()
{
    writeln("");
}
-----

The compiler matches the missing symbol with the selective import
"writeln", so it knows it only has to load std.stdio, *but not*
std.algorithm.

Test-case 3:
-----
import std.stdio : writeln;
import std.algorithm : map;
import std.range;  // might be here (compiler doesn't know)
import std.container;  // or here

void main()
{
    "foo".front;
}
-----

The compiler tries to find a selective import "front", but it doesn't
find it. What's important here is that it still does not have to load
std.stdio or std.algorithm, as we're explicitly loading only a set of
symbols which were never referenced from the test.d module.

So the next step here is for the compiler to try and load each module
in sequence (probably via the declaration order, first std.container),
and if there's a match the compiler would stop loading other modules
(no need to load std.container if std.range has "front").

-----

I could think of more optimizations, for example if we had a way of
exporting a list of module-level symbols into some kind of
intermediary format (say JSON), the compiler could look up this list
rather than to have to eagerly load every module in search of a
symbol. For example:

Test-case 4:
-----
import std.stdio;
import std.algorithm;
import std.range;
import std.container;

void main()
{
    "foo".front;
}
-----

If we had a "symbols.json", it might list things like: "front": {
std.range, std.stdio }, so the compiler would know only to look for
this symbol in these modules (and /if/ they are actually imported in
the test.d module).


More information about the Digitalmars-d mailing list