Operations 23 min read

Optimizing C++ Build Times in Large-Scale Projects: Analysis and Practices

By systematically analyzing compilation stages and applying a mix of generic tactics—parallel and distributed builds, precompiled headers, ccache, C++20 modules, include pruning—and code‑level changes such as forward declarations, external templates, and replacing heavy Boost headers, the Meituan Search & NLP team cut a 20‑minute C++ build to about 100 seconds, a 70 % reduction, and instituted a compile‑fingerprint system to guard against regressions.

Meituan Technology Team
Meituan Technology Team
Meituan Technology Team
Optimizing C++ Build Times in Large-Scale Projects: Analysis and Practices

Background – Large C++ projects often suffer from long compilation times, which hurts development efficiency during debugging, testing, and continuous integration. The Meituan Search & NLP team faced a 20‑minute compile time for their Deep Query Understanding (DQU) service on a 32‑core machine.

Compilation Principles – The article reviews the four classic stages of C++ compilation: preprocessing (gcc -E), compilation to assembly (gcc -S), assembly to object files (gcc -c), and linking (static .a, dynamic .so, executable). It explains how each stage contributes to overall build time.

C++ Compilation Characteristics

1. Each source file is compiled independently, limiting cross‑file optimizations. 2. Every translation unit parses all included headers, causing repeated work for large headers (e.g., Boost, Thrift). 3. Template instantiation occurs per translation unit; external templates can reduce redundant instantiations. 4. Virtual functions add pointer and v‑table overhead.

Service Problem Analysis – The DQU service includes >20 Thrift files, many Boost headers, and a massive Event header that pulls in >2000 other headers. This leads to huge pre‑processed files (≈15 MB) and long link times.

Optimization Strategies

General Build Acceleration

Parallel compilation using make -j $(nproc).

Distributed compilation (Distcc/Dmucs) for very large codebases.

Precompiled headers (PCH) where applicable.

CCache to reuse object files across builds.

C++20 modules to replace header inclusion.

Automatic include analysis (IWYU) to prune unnecessary headers.

Code‑Level Optimizations

Forward declarations and pointer/reference usage to avoid pulling full definitions.

External templates to prevent duplicate instantiations:

template<typename T> void max(T);
extern template void max<int>(int); // explicit instantiation

Replace heavy Boost headers with STL equivalents when possible.

Convert heavily used templates into polymorphic base classes with virtual methods.

Pre‑compile stable components (Thrift generated code, common libraries) into shared libraries.

Adjust CMake link dependencies to increase parallelism.

Results – After applying the above measures on a 32‑core, 64 GB machine, compilation time was reduced by ~70 %. Local builds now finish within ~100 seconds, saving significant developer time.

Maintaining Gains – The team introduced a “compile fingerprint” (CF) that records .ii file sizes per version. By comparing CFs between releases, they can detect regressions early. The analysis tools are integrated into the CI pipeline to enforce the optimizations continuously.

Conclusion – Systematic analysis, tooling, and both generic and project‑specific optimizations can dramatically cut C++ build times in large services, improving overall development velocity.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceCompilationCTooling
Meituan Technology Team
Written by

Meituan Technology Team

Over 10,000 engineers powering China’s leading lifestyle services e‑commerce platform. Supporting hundreds of millions of consumers, millions of merchants across 2,000+ industries. This is the public channel for the tech teams behind Meituan, Dianping, Meituan Waimai, Meituan Select, and related services.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.