1 / 16
→ advance   ← back

One copy is enough

Deduplicating source locations in Clang’s modular builds

the source-location address space
shared.h
mod1.h
shared.h ↩ reuses the first
mod2.h
02³¹
Ayokunle Amodu
C++ Alliance Fellowship, final presentation

Every token has an address

Clang encodes a source location as one 32-bit integer, an offset into a single address space. Every file the compiler enters is given a contiguous range in it, sized by the file.

the source-location address space
<built-ins>
shared.h, 4,890 B
mod1.h
tu.cc
free
01,0005,8902³¹

Diagnostics, fix-its, debug info: everything that names a line resolves through this space. It stays fast because a location is just a number.

And it is finite. About 2 GB of addresses for every character of source the compilation ever sees.

Two modules, one header

The whole talk starts from this shape: two modules, built separately, each one textually including the same header.

module A
#include "shared.h"
int a_fn();
module B
#include "shared.h"
int b_fn();
shared.h, 4,890 B

Each module is a separate compiler invocation writing its own self-contained PCM. So each one enters shared.h itself, and serializes its own record of everything it saw.

Modules multiply the same file

One space serves the whole compilation. The current TU grows from the bottom; everything loaded from PCMs is carved from the top, down.

local: this TUloaded: every module’s serialized entries
tu.cc + its headers
free
shared.h
mod1.h
shared.h
mod2.h
shared.h
mod3.h
02³¹

Alone, a TU barely dents the space: include guards enter a shared header once, and few TUs hold two billion characters.

But each PCM is self-contained, so each serialized its own entries for every header it included textually. Load N modules, and the same header is live N times at once.

fatal error: ran out of source locations

Reports of exactly this kept arriving from large module builds. The space runs out and the build stops.

First idea: fix it when loading

The duplicates meet each other at load time. So catch them there: deduplicate on the reader side.

Easy to say. The hard part is what the files on disk already assume.

Reuse at load means remapping everything

already loaded, module A
shared.h
mod1.h
on disk, module B’s PCM, written as if its copy owns a range
mod2.h
shared.h
diags.h
free
0 2,100 6,990

B’s copy is collapsed onto A’s, so every location that pointed into it must be rewritten…

…and everything after it shifts back. A forward map translates disk → memory, an inverse map must undo it when a module is re-serialized.

The inverse of a piecewise map is not one subtraction. Getting that wrong corrupted locations on ROOT, found the hard way.

The cost: nothing stays lazy

To compact the space before allocating it, the loader must read every location entry of every loaded module, up front.

Clang’s designan entry is materialized only when something touches it
Reader-side dedupall of them, for all modules, at startup

The eager scan showed up exactly where you’d expect: slower interpreter startup and higher peak memory on ROOT, regressing the fundamentals the patch has no business touching.

Saved the space, lost the fundamentals.

Reader side: it worked, and it cost too much

address spacereclaimed, the goal was met
PCM sizeunchanged, a load-time change writes nothing new to disk
startup & peak memoryslower start, about +15 MB peak memory
scan+1.58%
complexity carriedforward map, inverse map, eager scan of every loaded module

The space came back, but the fundamentals every compiler change must protect did not hold.

Keep the goal, change the place.

Prevent, don’t repair

A.pcm
mod1.h
shared.h entries
B.pcm
mod2.h
shared.h entries

Two files on disk, carrying identical entries for the same header.

Clang already does something close to this: when writing a PCM, it drops module-map files that don’t affect the build. That machinery came out of builds that were hitting exactly this problem.

A module map is the file that declares which headers form a module. When one is dropped, its locations are simply never written.

Nothing written  ⇒  nothing to remap later.

So generalize it: extend the same machinery from module maps to the headers themselves. Move deduplication to the writer, and make sure the second copy never exists.

Writer side: skip the copy, keep a pointer

A.pcm, already written, imported by B
shared.h
mod1.h
B.pcm, being written now
mod2.h
no entries written
diags.h

What a redirected location looks like

A’s loaded copy of shared.h, range starts at 1,000
shared.h  [1,000 – 5,890)
mod1.h
B’s local copy would start at 7,000; a location inside it sits at 7,420
adjustment = 7,000 − 1,000 = 6,000
7,420 − 6,000 = 1,420   written as  { module: A, offset: 1,420 }

Serialized locations were already {module-file, offset} pairs (0 = “this module”). The patch just lets that pair point at the module that owns the entries. The reader resolves it directly. No translation table, no fixup pass.

Finding A’s copy without scanning

INPUT_FILE {
  path: "shared.h", size: 4890, …  // already there
  sloc_index: 6,  sloc_offset: 1000  // new
}

Two integers per input file, a few bytes each, and the per-module scan they replaced was deleted outright.

A decision: eager map or lazy table

The lookup map is built once over the loaded modules, work proportional to the imports. The alternative raised in review: serialize a query table into every PCM, like HeaderFileInfo, work proportional to the module being written.

  • The table costs ~2 KB per PCM, written into every module, including leaves nobody ever queries. Those are exactly the bytes SDK vendors guard.
  • After keying by size and filtering by name, the eager build is cheap at every scale we could measure.
  • Verdict: not worth it yet. Revisit it if a workload ever shows the walk dominating.

The win: address space back

Measured on ROOT: 88 dictionaries, runtime C++ modules enabled.

baseline
75.33 MB
patched
61.02 MB

Loaded source-location space down 19%, the thing builds were running out of.

largest single saversc++config.h  4.75 MBfeatures.h  2.95 MB
sys/cdefs.h  2.72 MBassert.h  0.93 MB

Exactly the shared, non-modular system headers the exhaustion reports point at.

The fundamentals held

These two have to hold, no matter what a change buys elsewhere.

compile time
stable
module writing within measurement noise
peak memory (RSS)
−3.9%
slightly better at startup, fewer entries load at all

The reader-side version failed exactly this test. The writer-side version passes it, which is the whole reason it exists.

The ecosystem metrics held

SDK vendors watch these closely, and everyone benefits when they stay flat.

PCM size
−4.4%
on ROOT (embed-all-files amplifies it; ~0.4% without; ~flat on Apple SDK runs)
dependency scanning
+~1.5%
the one real cost, cut from +4.7% by re-keying the lookup and recording index + offset

The scan cost is small, understood to the instruction, and paid only where the feature actually fires.

Apple’s SDKs are modularized bottom-up, so they have little duplication left to reclaim. The savings show up where duplication actually exists, and nothing regresses where it doesn’t.

One copy is enough

ProblemEvery module carries its own copy of shared headers; the 32-bit source-location space runs out, and builds crash.
ApproachNever write the second copy. When writing a module, redirect a shared file’s locations to the module that already provides it.
Result−19% loaded location space on ROOT; compile time and memory stable; one small, measured scanning cost.
StatusPR #209795, approved, awaiting one final benchmark before landing.

Thank you. Questions welcome.