r/cpp • • Jul 29 '26

Build timings from auto-modularised VS solution with external and internal libs

To investigate how well modules work with MSVC without rewriting projects and putting up with malfunctioning intellisense, I made a little python script to modularise a whole VS solution (yes, I made it, using real bio-neurons).

The script lets me try out different modularisation strategies on a VS Solution on a per-project basis:

  • Header-based, don't modularise. If dependencies are modules, they will be imported.
  • PartitionsMI: Partition-per-header using standard module cpp units.
  • PartitionsPI: Partition-per-header using MSVC partition cpp units.
  • Submodules: Module-per-header
  • SubmodulesII: Move implementation to interface (not always possible due to cyclic imports)

Modularised code is surrounded with extern "C++" { export {.

The code is a super secret incomplete game engine/game. But here's some info:

  • External libs, including: Boost (unordered flat map), std, d3d, imgui, physx, spdlog, <windows>.

    • Wrapper module for each.
  • Solution projects: CommonStuff, Scripting, GameData, Graphics, Anim, SceneGraph, SceneManager, Entity, App

    • Some projects don't have much code. Others have quite a bit. Enough to give the build system something to parallelise.

All projects built using debug config. Benchmarks average 3 or more timings, outliers ignored. CPU is 12600, 12 threads. Multipliers are speedup.

 CommonStuffScriptingGameDataGraphicsAnimSceneGraphSceneManagerEntity
Header-based, no pch4.59s5.68s7.46s2.92s40.74s24.94s2.86s3.19s
Header-based, pch3.61s5.30s6.55s19.38s12.53s
Extlib modules + CommonStuff submodules3.60s4.78s1.62s18.55s9.41s1.57s2.09s
PartitionsMI4.25s8.09s2.54s
PartitionsPI4.02s6.04s7.09s2.53s33.83s15.97s2.66s4.10s
Submodules3.59s4.75s6.05s2.26s26.80s14.31s2.12s3.15s
SubmodulesII3.44s7.93s
 CommonStuffScriptingGameDataGraphicsAnimSceneGraphSceneManagerEntity
Header-based, no pch1.00x1.00x1.00x1.00x1.00x1.00x1.00x1.00x
Header-based, pch1.27x1.07x1.14x2.10x1.99x
Extlib modules + CommonStuff submodules1.58x1.56x1.80x2.20x2.65x1.82x1.53x
PartitionsMI1.08x0.92x1.15x
PartitionsPI1.14x0.94x1.05x1.15x1.20x1.56x1.08x0.78x
Submodules1.28x1.20x1.23x1.29x1.52x1.74x1.35x1.01x
SubmodulesII1.33x0.94x
 AppSingle fileFull Rebuild AppSingle fileFull Rebuild
Header-based, no pch65.19s3.97s145.49s1.00x1.00x1.00x
Header-based, app pch24.74s2.15s108.92s2.64x1.85x1.34x
Header-based, mostly pch78.87s1.84x
Extlib modules26.82s2.35s58.89s2.43x1.69x2.47x
Extlib modules + CommonStuff partitions25.89s2.47s2.52x1.61x
Extlib modules + CommonStuff submodules24.53s2.23s57.50s2.66x1.78x2.53x
Extlib modules + most libs as submodules31.14s2.97s2.09x1.34x
Full submodules, except app34.61s2.95s88.90s1.88x1.35x1.64x
Full submodules64.02s6.22s142.98s1.02x0.64x1.02x
  • "Most libs" = CommonStuff + Scripting + GameData + Graphics + Anim + SceneGraph.
  • "Mostly pch" = PCH for CommonStuff, Scripting, GameData, Anim, SceneGraph, App.
  • "Single file" is recompiling one moderately complex file in App. ~930 lines, lots of includes.
  • "Full Rebuild" excludes extlib module wrappers.

Detailed timings of CommonStuff:

Header-based:

	1>      210 ms  LIB                                        1 calls
	1>     4459 ms  CL                                         2 calls
	04.874 seconds
	
PartitionsMI:

	1>      125 ms  LIB                                        1 calls
	1>      199 ms  MSBuild                                   16 calls
	1>      269 ms  CppClean                                   1 calls
	1>      798 ms  SetModuleDependencies                     10 calls
	1>     1232 ms  CL                                         2 calls
	1>     1439 ms  MultiToolTask                              1 calls
	04.295 seconds

Submodules:

	1>      141 ms  MSBuild                                   16 calls
	1>      775 ms  SetModuleDependencies                     10 calls
	1>     1015 ms  CL                                         2 calls
	1>     1132 ms  MultiToolTask                              1 calls
	03.427 seconds

The CL entry is for cpp files and it's 4.39x as fast as header-based. Great!

But it's the ixx dependency scanning and compilation (MultiToolTask) that brings things down. Things that aren't there with header-based.

MultiToolTask is parallelised though. Reducing thread count slows it down. And you can open up the msbuild binlog in the msbuild log viewer to see the parallelisation of ixx compilation. See which modules are on the end of the DAG slowing things down.

So, I don't know how MultiToolTask could be sped up, but, msbuild seems to be waiting for all ixx files before moving onto cpp files, so that's some parallelism left on the table.

Detailed timings of App:

Header-based, app pch:

	1>      196 ms  MSBuild                                   13 calls
	1>     1000 ms  Link                                       1 calls
	1>    23270 ms  CL                                        16 calls
	24.708 seconds

Extlib modules + CommonStuff submodules:

	1>      199 ms  SetModuleDependencies                     18 calls
	1>      606 ms  MSBuild                                   27 calls
	1>     1197 ms  Link                                       1 calls
	1>    21877 ms  CL                                        15 calls
	24.078 seconds

Full submodules:

	1>      459 ms  MSBuild                                   27 calls
	1>     1324 ms  Link                                       1 calls
	1>     4646 ms  SetModuleDependencies                     18 calls
	1>    12593 ms  MultiToolTask                              1 calls
	1>    49327 ms  CL                                        15 calls
	01:09.245 minutes

Slow. But looking at the log viewer, ixx compilation seems very well parallelised.

And 4.6s just to scan for dependencies. VS isn't launching a whole CL process for each file is it?

Also, why does CL take so long? All modules have been compiled by that point, so why would it be much slower?

Recompiling a single file from App:

Header-based, no pch:

	1>       46 ms  SetModuleDependencies                     10 calls
	1>      120 ms  MSBuild                                    5 calls
	1>     3757 ms  CL                                         1 calls
	04.095 seconds

Header-based, app pch:

	1>       56 ms  SetModuleDependencies                     10 calls
	1>      121 ms  MSBuild                                    5 calls
	1>     1793 ms  CL                                         2 calls
	02.059 seconds

Extlib + CommonStuff modules (as submodules):

	1>      121 ms  SetModuleDependencies                     18 calls
	1>      216 ms  MSBuild                                   17 calls
	1>     1793 ms  CL                                         1 calls
	02.162 seconds

Full submodules, except app:

	1>      518 ms  MSBuild                                   17 calls
	1>      631 ms  SetModuleDependencies                     18 calls
	1>     2211 ms  CL                                         1 calls
	03.095 seconds

Full submodules:

	1>      559 ms  MSBuild                                   17 calls
	1>      696 ms  SetModuleDependencies                     18 calls
	1>     1891 ms  MultiToolTask                              1 calls
	1>     3221 ms  CL                                         1 calls
	06.331 seconds

That MultiToolTask is quite slow, checking that all the modules in the project are up to date it seems. Does it have to be this slow?

"CL.exe will run on 0 out of 224 file(s) in 224 batches. Startup phase took 2040.9372ms."

Anyway.

Conclusion:

  • I now have the data to know how I should go about modularising a codebase for build perf.
  • Surprisingly, there is a middle-ground between 0% modules and 100% modules where you get optimal build performance. It seems extlibs and your common lib should be modularised and nothing else. Or maybe my internal lib headers are too small to benefit.
  • Because of that, I might be able to have a header-based fallback for intellisense without it spreading to the rest of the code.
  • Multiple modules > Partitions. Partitions have worse performance. I assume that's because they need to have an extra module at the end of the DAG.

    • Their only advantage is shared module attachment across multiple ixx files, if you don't use extern "C++". As I said before, I wish you could have "public" partitions or multiple modules with the same name attachment.
  • MSVC partition cpp files are slightly faster vs module cpp files. But why. Cpp files wait for all ixx files to compile anyway. Noise in the data?
  • MSBuild parallelises as it should, it seems, except that cpp files wait for all ixx files. Could improve performance by merging the tasks?
  • cpp compilation can be much faster, but overall build perf brought down by ixx files and scanning.
  • Moving implementation to ixx files doesn't always help. Need more data.

    • And it would be ideal if build systems avoided the build cascade when only changing implementation details.
  • The more modules you add to the solution, the more bloated the build system feels. Lots of scanning, lots of ixx checking. Can performance be improved?
  • PCH can be beaten in some cases.

Problems:

  • Intellisense. Red squiggles everywhere. Not only import std issues, but Intellisense is highly sensitive to errors in imported modules. It cannot limp along like it can with headers.

    • A simple example would be missing a semicolon after a struct def in a header and in a module. Intellisense can still use the struct from the header, but not the module.
    • Or in working code, something from import std that Intellisense can't handle.
    • This means that parsing issues with Intellisense may be viral when using modules. Intellisense must either be perfect or tolerant.
  • From a previous modularisation attempt, I removed the combination of explicit template instantiations of a class with constrained friend functions, due to ICE, which has now become a bogus compiler error. Bug not fixed.
  • Lots of linker warnings from dllexport-ed manual RTTI data. Bug not fixed.

At least the compiler functions well enough to produce a working program with no further changes. That's pretty good.

19 Upvotes

20 comments sorted by

View all comments

Show parent comment

1

u/slithering3897 Aug 04 '26

The constexpr bug is actually at https://github.com/llvm/llvm-project/pull/74775, but seems to have stalled.

Otherwise, clang isn't really my primary compiler for now. Until VS has clang modules support. Then getting bugs fixed would be more important. But might not happen any time soon: https://github.com/dotnet/msbuild/issues/13530

and any target can import it, and the BMI is automatically computed, presumably including MSVC iterator debug level flags.

Auto import std is nice, but where do the flags come from? Sounds like some sort of unspecified behaviour where cmake could pick any flags if more than one target does import std.