Phobos import graph

Philippe Sigaud philippe.sigaud at gmail.com
Tue Jun 8 14:24:50 PDT 2010


Hi,

reading the std.all threads, I got curious as to how the different
Phobos modules import each other. A dependency graph, if you will,
though I'm sure to use that in a correct way.

Funnily, I wrote yesterday an importGraph function that, given a
module name and an optional directory where DMD is installed,
crawls the module, find import statements and recursively calls
itself with these new modules, building an import graph at the same
time. It's mostly made to give a dependency graph for a project,
but could be used on Phobos.

So, I created a stdall module importing all Phobos and got my
function to graph it (btw, it's cool to see it can read and kind-
of-parse all of Phobos and core in a few tens of ms).

For those interested, the result is there:

http://www.dsource.org/projects/dranges/browser/downloads/Phobos_gr
aph.pdf
(thanks graphviz)

A few notes:

- A ---> B means 'A imports B'.

- my parsing is primitive: I deal correctly with public/static
imports, multiline imports, renaming imports (import io =
std.stdio;) and partial imports (import std.stdio: writefln,
writef;), but don't test for comments. So modules with docs
containing import statements, mostly of themselves, will get a loop
A ---> A. You can see that in std.demangle or format, for example.

- The result is a Graph class, I have a toGraphviz function that
writes the graph as a .dot file. That's useful to debug graph
algorithms :-)

- if anyone can teach me how to use std.process to then call "dot -
Tpdf graph.dot -o graph.pdf" to generate the pdf graph from a .dot
file, I'd be most grateful. I can't get std.process to do anything,
not even creating a directory. I'm under Vista.

- There is probably a way to get the most imported modules at the
bottom of the graph, and the main 'importers' at the top. I don't
know graphviz enough. Maybe if I sort the module by number of
imports?

- I know how std.x.y works, but not core.*. When I get a core.x.y
module, I don't know where to find it. They don't seem to be all in
druntime\import\core nor in druntime\src\core. So i just create a
core.x.y node and don't open the file to parse it. So druntime is
underepresented here. It's strictly Phobos.

- the most imported modules seem to be conv (23), traits (22),
contracts (19), string(18), range(17), stdio (16), ... That does
not mean they should be imported by some
std.commonlyNeeededModules, but it's a data point.


Philippe


More information about the Digitalmars-d mailing list