code-optimizer
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCode Optimization
代码优化
Analyze code for performance issues following this priority order:
按照以下优先级顺序分析代码中的性能问题:
Analysis Priorities
分析优先级
- Performance bottlenecks - O(n²) operations, inefficient loops, unnecessary iterations
- Memory leaks - unreleased resources, circular references, growing collections
- Algorithm improvements - better algorithms or data structures for the use case
- Caching opportunities - repeated computations, redundant I/O, memoization candidates
- Concurrency issues - race conditions, deadlocks, thread safety problems
- 性能瓶颈 - O(n²) 操作、低效循环、不必要的迭代
- 内存泄漏 - 未释放的资源、循环引用、持续增长的集合
- 算法改进 - 更适合当前场景的算法或数据结构
- 缓存机会 - 重复计算、冗余I/O、可记忆化的候选点
- 并发问题 - 竞态条件、死锁、线程安全问题
Workflow
工作流程
0. Create Feature Branch
0. 创建功能分支
Before making any changes:
- Check the current branch - if already on a feature branch for this task, skip
- Check the repo for branch naming conventions (e.g., ,
feat/, etc.)feature/ - Create and switch to a new branch following the repo's convention, or fallback to:
feat/optimize-<target>- Example:
feat/optimize-api-handlers
- Example:
在进行任何修改之前:
- 检查当前分支 - 如果已处于该任务对应的功能分支,可跳过此步骤
- 查看仓库的分支命名规范(例如 、
feat/等)feature/ - 按照仓库规范创建并切换到新分支,默认使用:
feat/optimize-<target>- 示例:
feat/optimize-api-handlers
- 示例:
1. Analysis
1. 分析阶段
- Read the target code file(s) or directory
- Identify language, framework, and runtime context (Node.js, CPython, browser, etc.)
- Analyze for each priority category in order
- For each issue found, estimate the performance impact (e.g., "reduces API response from ~500ms to ~50ms")
- Report findings sorted by severity (Critical first)
- 阅读目标代码文件或目录
- 识别语言、框架和运行时环境(Node.js、CPython、浏览器等)
- 按优先级顺序逐一分析每个类别
- 针对每个发现的问题,评估其性能影响(例如:“将API响应时间从约500ms缩短至约50ms”)
- 按严重程度排序报告发现的问题(严重问题优先)
2. Apply Fixes
2. 应用修复
- Present the optimization report to the user
- On approval, apply fixes starting with Critical/High severity
- Run existing tests after each change to verify no regressions
- If no tests exist, warn the user before applying changes
- 向用户提交优化报告
- 获得批准后,从严重/高优先级问题开始应用修复
- 每次修改后运行现有测试,验证无回归问题
- 如果没有测试用例,在应用修改前向用户发出警告
Response Format
响应格式
For each issue found:
undefined针对每个发现的问题:
undefined[Severity] Issue Title
[严重等级] 问题标题
Location: file:line_number
Category: Performance | Memory | Algorithm | Caching | Concurrency
Problem: Brief explanation of the issue
Impact: Why this matters (performance cost, resource usage, etc.)
Fix:
[Code example showing the optimized version]
undefined位置: 文件:行号
类别: 性能 | 内存 | 算法 | 缓存 | 并发
问题: 问题简要说明
影响: 该问题的影响(性能损耗、资源占用等)
修复:
[展示优化后版本的代码示例]
undefinedSeverity Levels
严重等级
- Critical: Causes crashes, severe memory leaks, or O(n³)+ complexity
- High: Significant performance impact (O(n²), blocking operations, resource exhaustion)
- Medium: Noticeable impact under load (redundant operations, suboptimal algorithms)
- Low: Minor improvements (micro-optimizations, style improvements with perf benefit)
- 严重:导致崩溃、严重内存泄漏或O(n³)+复杂度
- 高:显著性能影响(O(n²)、阻塞操作、资源耗尽)
- 中:高负载下有明显影响(冗余操作、次优算法)
- 低:小幅改进(微优化、带性能收益的风格改进)
Language-Specific Checks
语言专属检查
JavaScript/TypeScript
JavaScript/TypeScript
- Array methods inside loops (map/filter/find in forEach)
- Missing async/await causing blocking
- Event listener leaks
- Unbounded arrays/objects
- 循环内的数组方法(forEach中嵌套map/filter/find)
- 缺少async/await导致阻塞
- 事件监听器泄漏
- 无边界的数组/对象
Python
Python
- List comprehensions vs generator expressions for large data
- Global interpreter lock considerations
- Context manager usage for resources
- N+1 query patterns
- 大数据量下的列表推导式与生成器表达式对比
- 全局解释器锁(GIL)考量
- 资源的上下文管理器使用
- N+1查询模式
General
通用检查
- Premature optimization warnings (only flag if genuinely impactful)
- Database query patterns (N+1, missing indexes)
- I/O in hot paths
- 过早优化警告(仅标记真正有影响的问题)
- 数据库查询模式(N+1、缺失索引)
- 热点路径中的I/O操作
Error Handling
错误处理
No obvious performance issues found
未发现明显性能问题
Solution: Report that the code is already well-optimized. Suggest profiling with runtime tools (e.g., , Chrome DevTools, ) to find runtime-specific bottlenecks.
perfpy-spy解决方案: 报告代码已优化良好。建议使用运行时工具(如 、Chrome DevTools、)进行性能分析,以发现运行时特定的瓶颈。
perfpy-spyTarget file is too large (>2000 lines)
目标文件过大(超过2000行)
Solution: Ask the user to specify which functions or sections to focus on. Analyze the most performance-critical paths first.
解决方案: 请求用户指定需要重点关注的函数或代码段。优先分析对性能最关键的路径。
Optimization breaks existing tests
优化导致现有测试失败
Solution: Revert the change immediately. Re-examine the optimization and adjust the approach to preserve existing behavior.
解决方案: 立即回滚修改。重新审视优化方案,调整方法以保留原有功能。