Loading...
Loading...
Compare original and translation side by side
The One Rule: Profile first. Prove behavior unchanged. One change at a time.
核心准则: 先做性能剖析(Profile),证明代码行为无变更,每次仅提交一项改动。
1. BASELINE → hyperfine --warmup 3 --runs 10 'command'
2. PROFILE → cargo flamegraph / py-spy / clinic flame
3. PROVE → Golden outputs + isomorphism proof per change
4. IMPLEMENT → Score ≥ 2.0 only, one lever per commit
5. VERIFY → sha256sum -c golden_checksums.txt
6. REPEAT → Re-profile (bottlenecks shift)1. BASELINE → hyperfine --warmup 3 --runs 10 'command'
2. PROFILE → cargo flamegraph / py-spy / clinic flame
3. PROVE → Golden outputs + isomorphism proof per change
4. IMPLEMENT → Score ≥ 2.0 only, one lever per commit
5. VERIFY → sha256sum -c golden_checksums.txt
6. REPEAT → Re-profile (bottlenecks shift)| Hotspot | Impact (1-5) | Confidence (1-5) | Effort (1-5) | Score |
|---|---|---|---|---|
| func:line | × | × | ÷ | Impact×Conf/Effort |
| 热点代码位置 | 影响度(1-5) | 置信度(1-5) | 投入成本(1-5) | 得分 |
|---|---|---|---|---|
| 函数名:行号 | × | × | ÷ | 影响度×置信度/投入成本 |
undefinedundefined
---
---| Pattern | When | Isomorphism |
|---|---|---|
| N+1 → Batch | Sequential fetches | Same results, fewer round-trips |
| Linear → HashMap | Keyed lookups | O(n)→O(1), order may change |
| Lazy eval | Maybe-unused values | Same final values |
| Memoization | Repeated pure calls | Cached = recomputed |
| Buffer reuse | Alloc per iteration | Zero-copy in loop |
| 优化模式 | 适用场景 | 同构性说明 |
|---|---|---|
| N+1请求 → 批量请求 | 顺序拉取场景 | 结果一致,减少往返次数 |
| 线性查找 → HashMap查找 | 键值查询场景 | O(n)→O(1),顺序可能变更 |
| 惰性求值 | 可能不会用到的数值计算场景 | 最终结果一致 |
| 结果缓存 | 重复纯函数调用场景 | 缓存结果=重新计算结果 |
| 缓冲区复用 | 每次迭代都需要分配内存的场景 | 循环内零拷贝 |
| Pattern | Change | Check |
|---|---|---|
| Binary search | O(n)→O(log n) | Sorted input |
| Two-pointer | O(n²)→O(n) | Structured input |
| Prefix sums | O(n)→O(1) query | Static data |
| Priority queue | O(n)→O(log n) | Top-k/scheduling |
| 优化模式 | 改动效果 | 校验项 |
|---|---|---|
| 二分查找 | O(n)→O(log n) | 输入已排序 |
| 双指针 | O(n²)→O(n) | 输入有结构化特征 |
| 前缀和 | O(n)查询→O(1)查询 | 数据为静态 |
| 优先队列 | O(n)→O(log n) | Top k/调度场景 |
| Structure | Use Case |
|---|---|
| HashMap | Point lookups |
| BTreeMap | Range queries |
| SmallVec | Usually-small collections |
| Arena | Many allocations, bulk free |
| Bloom filter | Membership pre-filter |
| 数据结构 | 适用场景 |
|---|---|
| HashMap | 单点查询 |
| BTreeMap | 范围查询 |
| SmallVec | 通常数据量很小的集合 |
| Arena | 大量内存分配、批量释放场景 |
| Bloom filter | 成员资格预过滤 |
| Lang | CPU Profile | Trouble Spot Grep |
|---|---|---|
| Rust | | |
| Go | | |
| TS | | |
| Python | | |
| 语言 | CPU性能剖析工具 | 常见问题排查命令 |
|---|---|---|
| Rust | | |
| Go | | |
| TS | | |
| Python | | |
| ✗ | Why |
|---|---|
| Optimize without profiling | Wastes effort on non-hotspots |
| Multiple changes per commit | Can't isolate regressions |
| Assume improvement | Must measure before/after |
| Change behavior "while we're here" | Breaks isomorphism guarantee |
| Skip golden output capture | No regression detection |
| ✗ 操作 | 原因 |
|---|---|
| 不做Profile就优化 | 浪费精力在非热点代码上 |
| 单次提交多个改动 | 无法定位性能回归原因 |
| 主观假设性能会提升 | 必须通过前后测量验证效果 |
| 优化时顺便修改代码行为 | 破坏同构性保证 |
| 不保存基准输出结果 | 无法检测功能回归 |
git revert <sha>git revert <sha>undefinedundefined
---
---| Need | Reference |
|---|---|
| Complete technique catalog | TECHNIQUES.md |
| Step-by-step methodology | METHODOLOGY.md |
| Language-specific guides | LANGUAGE-SPECIFIC.md |
| Advanced (Round 2+) | ADVANCED.md |
| 需求 | 参考文档 |
|---|---|
| 完整优化技巧目录 | TECHNIQUES.md |
| 分步操作方法论 | METHODOLOGY.md |
| 语言专属优化指南 | LANGUAGE-SPECIFIC.md |
| 进阶优化(第二轮及以后) | ADVANCED.md |