返回提示词目录第 1503 页

优化 C++ 编译与链接速度

帮助测量 C++ 构建瓶颈并优化头文件、缓存、预编译头和链接器,输出前后性能对比。

完整提示词

<context>
In C++ the dominant cost is usually parsing the same headers thousands of times. Clang's `-ftime-trace` produces per-file traces that ClangBuildAnalyzer aggregates into the most expensive headers, template instantiations and functions across the build; MSVC has `/Bt+` and `/d1reportTime` or Build Insights; the build system's own timing (Ninja's `.ninja_log`, viewable with ninjatracing) shows slow translation units and parallelism. The fixes: include what you use and forward-declare instead of including in headers; move heavy includes (Windows.h, Boost, Eigen, `<regex>`, big generated protobuf headers) out of widely included headers; pimpl or type erasure at module boundaries so private members do not leak includes; precompiled headers for stable, widely used headers (CMake `target_precompile_headers`); unity or jumbo builds for clean builds (can cause name clashes between anonymous-namespace symbols); `extern template` for templates instantiated everywhere; ccache or sccache for repeat builds and CI; and a faster linker (lld, mold on Linux) plus split debug info for incremental link time, which often dominates after a one-file change. Make sure the build actually runs in parallel: Ninja over Make, and no serialising custom commands.
</context>

<task>
Cut build times for: {{build}}

1. Read the build files, target layout, compiler flags, and the most included project headers.
2. Measure on a fixed machine: clean build, incremental build after editing a widely used header and after editing a leaf `.cpp`, and link time. Record parallelism.
3. Collect evidence with `-ftime-trace` and ClangBuildAnalyzer (or the compiler's equivalent) and the build log: top headers by total parse time, top template instantiations, slowest translation units, link time.
4. Apply the cheapest high-value fixes first: compiler cache, faster linker and split debug info, Ninja and correct parallelism, precompiled headers for stable heavy headers. Re-measure after each.
5. Then fix include hygiene in the worst headers: forward declarations, moving includes to `.cpp` files, pimpl where a header drags in heavy dependencies, `extern template` for hot templates. Re-measure.
6. Offer unity builds for CI or clean builds if still needed, noting name-clash risks. Run the full test suite and confirm the binaries behave the same.
</task>

<constraints>
- Keep changes behaviour-preserving; include removals must still compile on every supported platform and configuration.
- Do not lower optimisation or warning levels to gain build speed.
- Compiler and linker changes must work on every platform the project supports; document setup for teammates and CI.
- Report times from repeated runs on the same machine and cache state.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Baseline
Times per scenario and parallelism.
## Where time goes
Top headers, instantiations, slow translation units and link time.
## Changes
One line per file: `path`, what changed, time saved.
## Results
Times after, platforms and tests checked.
</output_format>

可替换内容

  • {{build}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、C++开发、编译优化、构建性能、工程工具链

前往 AI 图片创作