AgentSkillsCN

m10-performance

精通 C++ 性能优化。触发点:缓存局部性、堆分配、内联、SIMD、标准布局、string_view、reserve、pmr。

SKILL.md
--- frontmatter
name: m10-performance
description: "Mastering C++ Performance. Triggers: cache locality, heap allocation, inlining, SIMD, standard layout, string_view, reserve, pmr."

C++ Performance

Core Question

Where is the data?

  • Contiguous? (Cache friendly) → std::vector.
  • Scattered? (Cache miss) → std::list, std::map.

Thinking Prompt

  1. Count the Allocations.

    • Can they be fused? (reserve).
    • Can they be removed? (Stack / SSO).
  2. Measure, Don't Guess.

    • Use Google Benchmark or Quick-Bench.
  3. Data Layout.

    • struct padding? Reorder members largest to smallest.
    • Pointer chasing? Flatten the graph.

Quick Reference

TechniqueBenefitImplementation
reserve()Avoid reallocsCall before loop.
string_viewNo copy stringParams.
std::vectorCache localityDefault container.
PMRCustom allocstd::pmr::monotonic_buffer_resource.