r/cpp • u/ChuanqiXu9 • Sep 01 '26
Two Tricks to Use the std Module Implicitly, with 20% compilation time improvements on Seastar without touching the source
https://chuanqixu9.github.io/c++/2026/09/01/Implicit-Std-Modules.en.html
52
Upvotes
43
u/STL MSVC STL Dev Sep 01 '26
Your percentage improvements are being computed in a confusing manner. See: https://randomascii.wordpress.com/2018/02/04/what-we-talk-about-when-we-talk-about-performance/
You've described the improvement from preload (wall median of 81.09 s) to module maps (71.55 s) as "Reduced wall time by 9.54 seconds, or 11.76%." It's true that Delta/OldTime, i.e. 9.54/81.09, is 11.76%. However, this is confusing. If the time had improved dramatically from 81.09 seconds to 8.109 seconds, this method would describe it as "90%". And it's technically true that you'd have eliminated 90% of the time previously taken. However, this is better described as "ten times faster". (If a jet can get from NY to LA in 1 hour instead of 10 hours, everyone would say that it's screaming along ten times faster, not that 90% of the time formerly taken was saved.)
The proper computation to describe speedups is OldTime/NewTime, best presented as a multiplicative factor, but (for small numbers) you can take OldTime/NewTime - 1 and describe that as a percentage speedup.
Here, improving 81.09 s to 71.55 s is a 81.09/71.55 = 1.133x speedup (preferred), or "13.3% faster" (okay for small speedups; avoid when the speedup gets big like 1.5x or especially near 2x).
7
u/JVApen Clever is an insult, not a compliment. - T. Winters Sep 01 '26
How is this different from having
for every standard library header?
3
u/smdowney WG21, Text/Unicode SG, optional<T&> Sep 01 '26
I very much like the idea of a "Prelude" for C++, and just wished the built module interface was a little more stable vs flags and compiler version.
But given how often I end up rebuilding Catch2 or GTest, even with ccache, with projects, the overhead of pre-compiling std once per "realclean" or Config change is probably more than acceptable.
I think a measure I'd like to know is how many parses of, say, <iostream>, <vector>, or <format>, does it take to pay that off.
Also, the preload and include translation both let me fix other people's code. That Catch2 in-project build gets faster, too.
Does the header rewrite help avoid the ICEs from mixing import and include?
3
u/fortsnek274 Sep 01 '26
Yeah, it would be nice if you could get MSVC to redirect all std headers to your own header that does the import and includes any necessary macros (and for Intellisense to do the same once it works with modules properly).
Should help for external lib wrappers.
So clang can handle include after import?