AgentSkillsCN

hotpath-rust

在编写性能关键的内层循环(下采样、卷积运算、行列扫描)时,可优先使用此技能。确保改动安全、高效,并经过充分的基准测试验证。

SKILL.md
--- frontmatter
name: hotpath-rust
description: Use this when writing performance-critical inner loops (downsample, convolution, row/col scanning). Keeps changes safe, fast, and benchmarked.

Hotpath Rust checklist (lightweight)

Goals

  • predictable performance
  • minimal unsafe
  • no per-row/per-col allocations

Do

  • Reuse scratch buffers stored on a struct (Vec<T> reused via clear() / resize()).

  • Separate:

    • safe reference implementation
    • fast path (contiguous / common border mode) with minimal branching
  • If using unsafe, document invariants right above the block.

Don’t

  • Allocate inside the scan loop.
  • Add new heavy deps “for convenience”.

Always add

  • 1–2 unit tests that pin behavior (edge cases included).
  • A Criterion bench for the hot function (representative size).