Why are unity builds faster given modern compilers?

Consider (using Visual C++ as an example):

cl /EHsc a.cpp b.cpp c.cpp

versus

cat a.cpp b.cpp c.cpp > unity.cpp
cl /EHsc unity.cpp

Why is the first variant slower given a modern compiler? I’ve seen people say that unity builds avoid headers being parsed multiple times. But then why doesn’t a modern compiler cache its header parse results and reuse them between compilation units? Well, they already do. That’s what precompiled headers are. But then why not do it automatically for everything all the time? You might say that the order of includes, undefs, and other weird shit can matter, but that’s the pathological/exceptional case. You could still detect a well behaved header file and cache it, could you not?

1

Most C and C++ header files (notably system headers, or those of large C++ libraries like Qt) have #include guards or the non-standard #pragma once. Practically speaking, standard C++ headers like <set> or <map> are often expanded to at least ten thousand lines each.

So when some translation unit (like your unity.cpp) is including some given one header file foo.h several times, it gets included only once (the first occurrence of its #include).

Hence, the preprocessed form of unity.cpp is shorter (in preprocessed tokens) than the sum of the preprocessed form of each of a.cpp, b.cpp, c.cpp.

And you could check that by getting their preprocessed form. With GCC you would use (perhaps adding other preprocessor options like -I… to add an include directory, -D… to define a preprocessor symbol, -H to get the included files’ path, …)

g++ -C -E a.cpp > a.ii
g++ -C -E b.cpp > b.ii
g++ -C -E c.cpp > c.ii
g++ -C -E unity.cpp > unity.ii

and you could compare the cumulated word count obtained by wc a.ii b.ii c.ii with the one of wc unity.ii

(I leave you to adapt these Linux commands to your cl compiler & Windows)

However, having several translation units enable parallel builds (in several processes running simultaneously), e.g. with make -j.

(Hence, in practical terms, you may want some trade-off; I’ve got the habit of having *.cc files of a few thousand lines)

You might say that the order of includes, undefs, and other weird shit can matter, but that’s the pathological/exceptional case.

I am not sure it is pathological. On Linux, I would presume it could be quite common. See feature_test_macros(7)

You could still detect a well behaved header file and cache it

In practice, that does not work well. Within GCC, some clever people worked on preparsed headers without success. Future C++ standards might define modules (See also Clang documentation on them)

NB: I am answering with a Linux & GCC -centric view, but you could adapt my answer to your Visual C++ & Windows (which I don’t know) because it is much more a language (and practical) issue than an operating system & compiler one.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật