notes · · 4 min · updated 2026-10-27

Phobos Under the Microscope: What 67 Remote AST Tools Found Inside the D Standard Library

Analysis of the D standard library with prod-code: 366,399 lines of D, a dependency scan that recognized no modules, range-based algorithms, Design by Introspection, and repository-wide matching-line counts for assertions and unittest occurrences.

On this page · 4 sections
  1. Subsystem Architecture: The Range and Metaprogramming Foundation
  2. Design by Introspection: The InputRange Contract
  3. Assertion and Unittest Matching Lines (Not Construct Counts)
  4. Clone Analysis and Test Matrix Deduplication

First published on 2026-10-04. The measurements reported in this article are historical and have not been re-run for this revision.

A decade before C++20 standardized std::ranges and Rust established its iterator adapter ecosystem, Andrei Alexandrescu published “Iterators Must Go” in 2009, proposing that iterator pairs were fundamentally prone to safety bugs and mismatch errors. The solution he designed became the architectural bedrock of Phobos-the standard library of the D programming language.

Phobos showed that by treating data sequences as composable ranges (empty, front, popFront), algorithms could be chained lazily, with range requirements checked at compile time; whether a given pipeline allocates or gets inlined depends on the adapters used and on the compiler. Combined with D’s compile-time introspection capabilities, Phobos developed Design by Introspection (DbI), where generic templates inspect type capabilities at compile time and adapt their implementations dynamically.

Correction, 2026-10-27. As published on 2026-10-04, this paragraph said ranges give zero heap allocation, strict type safety and automatic compiler inlining, and the InputRange section said object-oriented interfaces require virtual tables and heap indirection and that providing the three member names is enough for isInputRange. Phobos v2.110.0 (a tagged comparison revision; the article used an unpinned master checkout) spells out bool, non-void and callability requirements in std/range/primitives.d, and its stable sort may allocate (std/algorithm/sorting.d). The blanket allocation, inlining and OOP claims are withdrawn.

To analyze the structural purity, template boundaries, and assertion density of Phobos, we deployed selected operations from prod-code’s 67-tool suite against a Phobos checkout (master branch) on a remote 32-core cluster node (node-b:9400), measuring modular dependencies, clone clusters, and executable specification assertions.

$ git ls-files '*.d' | wc -l
186
$ git ls-files -z '*.d' | xargs -0 wc -l | tail -n 1
366399 total
$ git ls-files | awk -F. '{if (NF>1) print $NF}' | sort | uniq -c | sort -nr | head -n 6
 186 d
  15 dd
  10 mak
   7 txt
   4 html
   3 yml

The captured inventory shows 366,399 lines of D across 186 files. The article reports the standard library core (std/) at 339,250 lines across 162 files and etc/ at 23,439 lines across 18 files; together these leave 3,710 lines in 6 files outside both directories, which the article does not identify.

Subsystem Architecture: The Range and Metaprogramming Foundation

Phobos builds upon layered domain modules:

  1. Compile-Time Traits & Type Constructors (std.traits, std.typecons): Provides static reflection tools to query function signatures, field alignments, and type qualifiers at compile time.
  2. Range Primitives & Composition (std.range): Defines the hierarchy of range concepts (InputRange, ForwardRange, BidirectionalRange, RandomAccessRange, OutputRange) and adapter wrappers (take, drop, chain, zip, stride).
  3. Composable Algorithms (std.algorithm): Lazy transformations (iteration.d, searching.d, sorting.d, mutation.d).
  4. Data Formats & Conversion (std.conv, std.format, std.string, std.utf): Type-safe conversions and Unicode processing.
  5. System & Concurrency Runtimes (std.parallelism, std.concurrency, std.process, std.file): Task pools, message-passing concurrency, and OS process management.

We ran prod-code dependencies across the library modules:

$ prod-code dependencies --scope modules
⚡ prod-code Architecture & Dependency Graph Report
────────────────────────────────────────────────────
Scope: modules | Nodes: 0 | Dependencies: 0

✓ Zero circular dependencies detected. Architecture graph is a clean DAG.

The captured run found zero nodes and zero dependency edges. It extracted no module graph, so it cannot establish a clean DAG, leaf modules, or the absence of circular template dependencies.

Design by Introspection: The InputRange Contract

In classical object-oriented languages, interfaces are usually dispatched at runtime through virtual function tables. In Phobos, generic constraints are evaluated statically using D’s compile-time introspection:

enum bool isInputRange(R) =
    is(typeof(R.init) == R)
    && is(typeof((R r) { return r.empty; } (R.init)) == bool)
    && (is(typeof((return ref R r) => r.front)) || is(typeof(ref (return ref R r) => r.front)))
    && !is(typeof((R r) { return r.front; } (R.init)) == void)
    && is(typeof((R r) => r.popFront));

A struct or class satisfies isInputRange!R when, as the constraint above spells out, empty yields a bool, front yields a non-void value, and popFront can be called. Algorithms can further query whether the range provides forward saving (isForwardRange), back indexing (isBidirectionalRange), or random access (isRandomAccessRange).

std.algorithm.sorting.sort requires a random-access range. It uses Introsort for unstable sorting and Timsort for stable sorting; bidirectional-only ranges do not satisfy this function’s range requirement.

Assertion and Unittest Matching Lines (Not Construct Counts)

D builds unit testing into the language: unittest { ... } blocks live directly alongside the code they test. In Phobos, these blocks serve as executable examples, and documented ones also appear as examples in the generated API documentation:

$ grep -rnE "assert\(" std/ | wc -l
33038
$ grep -rnE "unittest" std/ | wc -l
5613

These commands count matching lines, not assertion occurrences or parsed unittest blocks: 33,038 lines contain assert( and 5,613 lines contain unittest under std/. Comments and assertions outside test blocks can contribute. The results do not establish exact assertion or test-block totals, and the derived tests-per-line ratio is unsupported.

These matching-line counts do not show that every template function has a static assertion or that regressions are caught at compile time. Establishing those guarantees requires inspecting the actual assertion sites and their contexts.

Clone Analysis and Test Matrix Deduplication

We executed prod-code duplicates to detect structural cloning across Phobos:

$ prod-code duplicates --min-lines 6 --max-groups 5
⚡ prod-code Clone & Duplication Harvester Report
────────────────────────────────────────────────────
Files Scanned: 186 | Lines: 366399 | Clone Groups: 5 | Duplication: 0.8%

Discovered Clone Groups:

[Clone Group #14] 6 lines | 24 occurrences (Type-2 (Parameterized))
  • std/datetime/date.d:1786-1791
  • std/datetime/date.d:1797-1802
  • std/datetime/date.d:1982-1987
  • std/datetime/date.d:1988-1993
  • std/datetime/date.d:2000-2005
  Preview:
    │     testDT(DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)), -960,
    │            DateTime(Date(-1999, 7, 6), TimeOfDay(12, 30, 33)));

The tool reports overall duplication of 0.8% across 366,399 lines. The one group shown, of the five the report is capped at, resides in std/datetime/date.d, where calendar verification routines sweep comprehensive tables of time offsets, leap-second boundaries, and century transitions.

The report does not show the other groups or break duplication down by module, so it does not establish how much duplication std/range/, std/algorithm/ and std/traits/ contain, or that template parameterization and mixins are the reason.

The evaluation demonstrates how Phobos established the blueprint for modern zero-overhead range pipelines, uniting compile-time introspection with assertion-heavy unittest blocks.

Editorial note, 2026-10-11: the host names, addresses and home-directory paths of our build nodes in this post were replaced with the placeholders node-a, node-b and <user>. No other part of the quoted output was changed.

Cite this article
Citation
Alexander Panasenko (2026-10-04). Phobos Under the Microscope: What 67 Remote AST Tools Found Inside the D Standard Library. https://prod.codes/blog/phobos-under-the-microscope-67-ast-tools/