optimize

A command for making small, behavior-preserving improvements to C++23 code. C++23 is a recent version of the C++ programming language.

In plain words
What is it for?
Use it to optimize a specified C++ file or function, with limits on change size and checks for compiler warnings and existing tests.
Why use it?
It targets waste such as unnecessary allocations, copies, runtime work, or ownership overhead while keeping the public interface and behavior unchanged.

Command for Claude Code

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/awesome-embedded-learning-studio/cfdesktop/optimize
Clone the repo
git clone --depth 1 https://github.com/Awesome-Embedded-Learning-Studio/CFDesktop

Made for: Claude Code.

Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 865 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00865
Opus 5 $0.00000 $0.00432
Sonnet 5 $0.00000 $0.00173
Haiku 4.5 $0.00000 $0.00086

Measured 2d ago against content hash 442332c5abc0, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

optimize scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.claude/commands/optimize.md · 94 lines

What it actually says

/optimize — 代码优化

基于 C++23 和现代 C++ 工程实践,对代码进行逼近零开销的小改动优化。

触发方式

  • /optimize <文件/函数> — 优化指定代码
  • Review 输出对接 — 用户基于审查报告请求优化

硬约束 (MUST)

  • 每项优化改动 1-5 行
  • 禁止改变公共 API 签名(破坏 ABI)
  • 禁止改变行为(只做优化)
  • 禁止大规模重构
  • 必须通过 -Wall -Wextra -Wpedantic 编译
  • 必须通过现有测试
  • 每文件每轮最多 10 项优化
  • 不确定时加 @note FIXME 而非猜测

优化流程

Step 1: 读取目标代码

  • 读取指定文件完整内容
  • 读取相关头文件和测试文件
  • 理解类层次和使用上下文

Step 2: 优化检查清单

2.1 constexpr 提升
  • 编译期可计算的函数 → constexpr
  • 魔数 → constexpr 常量
  • 类型特征和辅助工具 → constexpr 优先于模板元编程
  • if constexpr 替代运行时分支(类型分发场景)
2.2 移动语义
  • 按值返回 vs 显式 std::move(NRVO 场景不要 move,会阻碍优化)
  • Sink 参数用按值传递 + std::move 进成员
  • 构造函数中用 std::move 初始化成员
  • 不要对返回局部变量 std::move(阻碍 RVO/NRVO)
2.3 零开销抽象
  • 编译期可确定的多态 → 模板参数替代虚分派
  • 小函数隐式内联(头文件定义)
  • CRTP 替代虚分派(静态多态)
  • 固定大小容器 → std::array 替代 std::vector
2.4 [[nodiscard]] 标注
  • 所有 const getter
  • 返回所有权的工厂函数
  • 返回 expected<T> 或错误码的函数
  • 忽略返回值会导致 bug 的函数
2.5 智能指针优化
  • 独占所有权 → std::unique_ptr(零开销)
  • 共享所有权 → std::shared_ptr(仅在必要时)
  • std::make_unique / std::make_shared 优于裸 new
  • 观察者模式 → 裸指针/引用,不用智能指针
2.6 Qt 特定优化
  • QString → 使用 QStringView / qsizetype
  • Signal/Slot → 新式连接 (&Class::method)
  • 避免 QString::fromStdString 不必要转换
  • 像素操作 → QImage 优于 QPixmap
  • Q_FOREACH → 范围 for 循环

Step 3: 输出格式 (中文)

# 优化建议: <file_name>

## 优化 1: <标题>
- **类别**: constexpr/移动语义/零开销/[[nodiscard]]/智能指针/Qt
- **当前代码**:
  ```cpp
  // file:line
  • 优化后代码:
    // 修改后
    
  • 原理: ...
  • 影响范围: 低/中
  • 验证方法: 编译 + 运行测试

优化 2: ...

总结

  • 优化项数: N
  • 预估性能提升: ...
  • 风险等级: 整体低/中
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 94 lines · 0 tokens per session scan A 442332c5abc0

Subscribe to this mod's changes

optimize is a command published in the GitHub repository Awesome-Embedded-Learning-Studio/CFDesktop (10 stars, last pushed 1mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 865 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.