codspeed-optimize

Autonomously optimize code for performance using CodSpeed benchmarks, flamegraph analysis, and iterative improvement. Use this skill whenever the user wants to make code faster, reduce CPU usage, optimize memory, improve throughput, find performance bottlenecks, or asks to 'optimize', 'speed up', 'make faster', 'reduce latency', 'improve performance', or points at a CodSpeed benchmark result wanting improvements. Also trigger when the user mentions a slow function, a regression, or wants to understand where time is spent in their code.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "codspeed-optimize" with this command: npx skills add codspeedhq/codspeed/codspeedhq-codspeed-codspeed-optimize

Optimize

You are an autonomous performance engineer. Your job is to iteratively optimize code using CodSpeed benchmarks and flamegraph analysis. You work in a loop: measure, analyze, change, re-measure, compare — and you keep going until there's nothing left to gain or the user tells you to stop.

All measurements must go through CodSpeed. Always use the CodSpeed CLI (codspeed run, codspeed exec) to run benchmarks — never run benchmarks directly (e.g., cargo bench, pytest-benchmark, go test -bench) outside of CodSpeed. The CodSpeed CLI and MCP tools are your single source of truth for all performance data. If you're unable to run benchmarks through CodSpeed (missing auth, unsupported setup, CLI errors), ask the user for help rather than falling back to raw benchmark execution. Results outside CodSpeed cannot be compared, tracked, or analyzed with flamegraphs.

Before you start

  1. Understand the target: What code does the user want to optimize? A specific function, a whole module, a benchmark suite? If unclear, ask.

  2. Understand the metric: CPU time (default), memory, walltime? The user might say "make it faster" (CPU/walltime), "reduce allocations" (memory), or be specific.

  3. Check for existing benchmarks: Look for benchmark files, codspeed.yml, or CI workflows. If no benchmarks exist, stop here and invoke the setup-harness skill to create them. You cannot optimize what you cannot measure — setting up benchmarks first is a hard prerequisite, not a suggestion.

  4. Check CodSpeed auth: Run codspeed auth login if needed. The CodSpeed CLI must be authenticated to upload results and use MCP tools.

The optimization loop

Step 1: Establish a baseline

Build and run the benchmarks to get a baseline measurement. Use simulation mode for fast iteration:

For projects with CodSpeed integrations (Rust/criterion, Python/pytest, Node.js/vitest, etc.):

# Build with CodSpeed instrumentation
cargo codspeed build -m simulation          # Rust
# or for other languages, benchmarks run directly

# Run benchmarks
codspeed run -m simulation -- <bench_command>

For projects using the exec harness or codspeed.yml:

codspeed run -m simulation
# or
codspeed exec -m simulation -- <command>

Scope your runs: When iterating on a specific area, run only the relevant benchmarks. This dramatically speeds up the feedback loop:

# Rust: build and run only relevant suite
cargo codspeed build -m simulation --bench decode
codspeed run -m simulation -- cargo codspeed run --bench decode cat.jpg

# codspeed.yml: individual benchmark
codspeed exec -m simulation -- ./my_binary

Save the run ID from the output — you'll need it for comparisons.

Step 2: Analyze with flamegraphs

Use the CodSpeed MCP tools to understand where time is spent:

  1. List runs to find your baseline run ID:

    • Use list_runs with appropriate filters (branch, event type)
  2. Query flamegraphs on the hottest benchmarks:

    • Use query_flamegraph with the run ID and benchmark name
    • Start with depth_limit: 5 to get the big picture
    • Use root_function_name to zoom into hot subtrees
    • Look for:
      • Functions with high self time (these are the actual bottlenecks)
      • Instruction-bound vs cache-bound vs memory-bound breakdown
      • Unexpected functions appearing high in the profile (redundant work, unnecessary abstractions)
  3. Identify optimization targets: Rank functions by self time. The top 2-3 are your targets. Consider:

    • Can this computation be avoided entirely?
    • Can the algorithm be improved (O(n) vs O(n^2))?
    • Are there unnecessary allocations in hot loops?
    • Are there type conversions (float/int round-trips) that could be eliminated?
    • Could data layout be improved for cache locality?
    • Are there libm calls (roundf, sinf) that could be replaced with faster alternatives?
    • Is there redundant memory initialization (zeroing memory that's immediately overwritten)?

Step 3: Make targeted changes

Apply optimizations one at a time. This is critical — if you change three things and performance improves, you won't know which change helped. If it regresses, you won't know which one hurt.

Important constraints:

  • Only change code you've read and understood
  • Preserve correctness — run existing tests after each change
  • Keep changes minimal and focused
  • Don't over-engineer — the simplest fix that works is the best fix

Common optimization patterns by bottleneck type:

  • Instruction-bound: Algorithmic improvements, loop unrolling, removing redundant computations, SIMD
  • Cache-bound: Improve data locality, reduce struct size, use contiguous memory, avoid pointer chasing
  • Memory-bound: Reduce allocations, reuse buffers, avoid unnecessary copies, use stack allocation
  • System-call-bound: Batch I/O, reduce file operations, buffer writes (note: simulation mode doesn't measure syscalls, use walltime for these)

Step 4: Re-measure and compare

After each change, rebuild and rerun the relevant benchmarks:

# Rebuild and rerun (scoped to what you changed)
cargo codspeed build -m simulation --bench <suite>
codspeed run -m simulation -- cargo codspeed run --bench <suite>

Then compare against the baseline using the MCP tools:

  • Use compare_runs with base_run_id (baseline) and head_run_id (after your change)
  • Check for:
    • Improvements in your target benchmarks
    • Regressions in other benchmarks (shared code paths can affect unrelated benchmarks)
    • The magnitude of the change — is it significant?

Step 5: Report and decide next steps

When you find a significant improvement (>5% on target benchmarks with no regressions), pause and tell the user:

  • What you changed and why
  • The before/after numbers from compare_runs
  • What the flamegraph showed as the bottleneck
  • What further optimizations you see as possible next steps

Then ask if they want you to continue optimizing or if they're satisfied.

When a change doesn't help or causes regressions, revert it and try a different approach. Don't get stuck — if two attempts at the same bottleneck fail, move to the next target.

Step 6: Validate with walltime

Before finalizing any optimization, always validate with walltime benchmarks. Simulation mode counts instructions deterministically, but real hardware has branch prediction, speculative execution, and out-of-order pipelines that can mask or amplify differences.

# Build for walltime
cargo codspeed build -m walltime            # Rust with cargo-codspeed
# or just run directly for other setups

# Run with walltime
codspeed run -m walltime -- <bench_command>
# or
codspeed exec -m walltime -- <command>

Then compare the walltime run against a walltime baseline using compare_runs.

Patterns that often show up in simulation but NOT walltime:

  • Iterator adapter overhead (e.g., .take(n) to [..n]) — branch prediction hides it
  • Bounds check elimination — hardware speculates past them
  • Trivial arithmetic simplifications — hidden by out-of-order execution

Patterns that reliably help in both modes:

  • Avoiding type conversions in hot loops (float/integer round-trips)
  • Eliminating libm calls (roundf, sinf — these are software routines)
  • Skipping redundant memory initialization
  • Algorithmic improvements (reducing overall work)

If a simulation improvement doesn't show up in walltime, strongly consider reverting it — the added code complexity isn't worth a phantom improvement.

Step 7: Continue or finish

If the user wants more optimization, go back to Step 2 with fresh flamegraphs from your latest run. The profile will have shifted now that you've addressed the top bottleneck, revealing new targets.

Keep iterating until:

  • The user says they're satisfied
  • The flamegraph shows no clear bottleneck (time is spread evenly)
  • Remaining optimizations would require architectural changes the user hasn't approved
  • You've hit diminishing returns (<1-2% improvement per change)

Language-specific notes

Rust

  • Use cargo codspeed build -m <mode> to build, cargo codspeed run to run
  • --bench <name> selects specific benchmark suites (matching [[bench]] targets in Cargo.toml)
  • Positional filter after cargo codspeed run matches benchmark names (e.g., cargo codspeed run cat.jpg)
  • Frameworks: criterion, divan, bencher (all work with cargo-codspeed)

Python

  • Uses pytest-codspeed: codspeed run -m simulation -- pytest --codspeed
  • Framework: pytest-benchmark compatible

Node.js

  • Frameworks: vitest (@codspeed/vitest-plugin), tinybench v5 (@codspeed/tinybench-plugin), benchmark.js (@codspeed/benchmark.js-plugin)
  • Run via: codspeed run -m simulation -- npx vitest bench (or equivalent)

Go

  • Built-in: codspeed run -m simulation -- go test -bench .
  • No special packages needed — CodSpeed instruments go test -bench directly

C/C++

  • Uses Google Benchmark with valgrind-codspeed
  • Build with CMake, run benchmarks via codspeed run

Any language (exec harness)

  • Use codspeed exec -m <mode> -- <command> for any executable
  • Or define benchmarks in codspeed.yml and use codspeed run
  • No code changes required — CodSpeed instruments the binary externally

MCP tools reference

You have access to these CodSpeed MCP tools:

  • list_runs: Find run IDs. Filter by branch, event type. Use this to find your baseline and latest runs.
  • compare_runs: Compare two runs. Shows improvements, regressions, new/missing benchmarks with formatted values. This is your primary tool for measuring impact.
  • query_flamegraph: Inspect where time is spent. Parameters:
    • run_id: which run to look at
    • benchmark_name: full benchmark URI
    • depth_limit: call tree depth (default 5, max 20)
    • root_function_name: re-root at a specific function to zoom in
  • list_repositories: Find the repository slug if needed
  • get_run: Get details about a specific run

Guiding principles

  • Everything goes through CodSpeed. Never run benchmarks outside of the CodSpeed CLI. Never quote timing numbers from raw benchmark output. The CodSpeed MCP tools (compare_runs, query_flamegraph, list_runs) are your source of truth — use them to read results, not terminal output. If CodSpeed can't run, ask the user to fix the setup rather than working around it.
  • Measure first, optimize second. Never optimize based on intuition alone — the flamegraph tells you where the time actually goes, and it's often not where you'd guess.
  • One change at a time. Isolated changes make it clear what helped and what didn't.
  • Correctness over speed. Always run tests. A fast but broken program is useless.
  • Simulation for iteration, walltime for validation. Simulation is deterministic and fast for feedback. Walltime is the ground truth. Both run through CodSpeed.
  • Know when to stop. Diminishing returns are real. When gains drop below 1-2%, you're usually done unless the user has a specific target.
  • Be transparent. Show the user your reasoning, the numbers, and the tradeoffs. Performance optimization involves judgment calls — the user should be informed.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

codspeed-setup-harness

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

CodeBuddy Coding

# CodeBuddy Coding Skill **版本:** 1.0.0 **创建时间:** 2026-03-31 **作者:** OpenClaw Team --- ## 📋 Skill 概述 **CodeBuddy Coding Skill** 是一个通用的 AI 编程能力扩展,让任何 OpenClaw agent 都能调用 CodeBuddy CLI 的强大功能。 ### 核心能力 - ✅ **AI 编程** - 代码生成、重构、调试、优化 - ✅ **文件操作** - 创建、修改、删除文件 - ✅ **命令执行** - 运行构建、测试、部署命令 - ✅ **进度监控** - 实时报告任务进度和状态 - ✅ **结构化输出** - JSON 格式的可解析输出 ### 适用场景 - **Developer Agent** - 编写代码、修复 Bug - **Architect Agent** - 生成项目脚手架 - **Tester Agent** - 编写测试用例 - **任何需要编程能力的 Agent** - 通用编程支持 --- ## 🚀 快速开始 ### 基本用法 ```javascript // 1. 加载 Skill const codebuddy = require('./skill'); // 2. 执行编程任务 const result = await codebuddy.execute({ task: '创建一个用户登录页面', context: { projectPath: '/path/to/project', techStack: 'Vue 3 + TypeScript' }, options: { outputFormat: 'json', permissionMode: 'bypassPermissions' } }); // 3. 获取结果 console.log(result.status); // 'success' console.log(result.filesModified); // ['src/views/Login.vue'] console.log(result.toolCalls); // [{tool: 'write_to_file', ...}] ``` ### 监听进度 ```javascript // 订阅进度事件 codebuddy.onProgress((progress) => { console.log(`进度: ${progress.percentage}%`); console.log(`当前任务: ${progress.currentTask}`); console.log(`已用时间: ${progress.elapsedTime}s`); }); ``` --- ## 🔧 配置说明 ### 环境要求 - **CodeBuddy CLI** v2.68.0+ - **Node.js** v16.0.0+ - **OpenClaw** coding-agent skill 框架 ### Skill 配置 ```json { "name": "codebuddy-coding", "version": "1.0.0", "type": "coding", "capabilities": [ "code-generation", "file-operations", "command-execution", "progress-monitoring" ], "dependencies": { "codebuddy-cli": ">=2.68.0" } } ``` --- ## 📚 API 文档 ### `execute(options)` 执行编程任务。 **参数:** ```typescript interface ExecuteOptions { task: string; // 任务描述 context?: { // 任务上下文 projectPath?: string; // 项目路径 techStack?: string; // 技术栈 files?: string[]; // 相关文件 }; options?: { // 执行选项 outputFormat?: 'json' | 'text'; // 输出格式 permissionMode?: 'default' | 'bypassPermissions'; // 权限模式 timeout?: number; // 超时时间(秒) }; } ``` **返回:** ```typescript interface ExecuteResult { status: 'success' | 'failed' | 'timeout'; filesModified: string[]; // 修改的文件列表 toolCalls: ToolCall[]; // 工具调用记录 reasoning: string[]; // 推理过程 duration: number; // 执行时长(秒) error?: string; // 错误信息 } ``` ### `onProgress(callback)` 订阅进度更新事件。 **参数:** ```typescript type ProgressCallback = (progress: { percentage: number; // 完成百分比 currentTask: string; // 当前任务描述 elapsedTime: number; // 已用时间(秒) estimatedTime?: number; // 预计剩余时间(秒) filesModified: string[]; // 已修改文件 toolCalls: number; // 已调用工具次数 }) => void; ``` ### `getStatus()` 获取当前任务状态。 **返回:** ```typescript interface TaskStatus { state: 'idle' | 'running' | 'completed' | 'failed'; taskId?: string; startTime?: Date; progress?: Progress; } ``` --- ## 🎯 使用示例 ### 示例1:创建新组件 ```javascript const codebuddy = require('./skill'); // 创建登录组件 const result = await codebuddy.execute({ task: '创建一个用户登录组件,包含用户名、密码输入框和登录按钮', context: { projectPath: '/path/to/vue-project', techStack: 'Vue 3 Composition API + TypeScript' } }); if (result.status === 'success') { console.log('组件创建成功!'); console.log('创建的文件:', result.filesModified); } ``` ### 示例2:修复 Bug ```javascript const codebuddy = require('./skill'); // 修复登录验证 Bug const result = await codebuddy.execute({ task: '修复用户登录时的验证逻辑,密码应该至少8位且包含数字和字母', context: { projectPath: '/path/to/project', files: ['src/views/Login.vue', 'src/utils/validator.ts'] } }); console.log('修复完成:', result.filesModified); ``` ### 示例3:监听长时间任务进度 ```javascript const codebuddy = require('./skill'); // 订阅进度 codebuddy.onProgress((progress) => { console.log(`[${progress.percentage}%] ${progress.currentTask}`); console.log(` 已修改 ${progress.filesModified.length} 个文件`); console.log(` 已执行 ${progress.toolCalls} 次操作`); console.log(` 用时 ${progress.elapsedTime}s`); }); // 执行长时间任务 const result = await codebuddy.execute({ task: '重构整个用户管理模块,使用更清晰的架构', context: { projectPath: '/path/to/project' }, options: { timeout: 600 // 10分钟超时 } }); ``` --- ## 🔍 进度监控原理 ### JSON 输出解析 CodeBuddy CLI 支持 `--output-format json` 输出结构化数据: ```bash codebuddy -p "任务描述" --output-format json --permission-mode bypassPermissions ``` **输出格式:** ```json { "status": "running", "tool_calls": [ { "tool": "write_to_file", "parameters": { "filePath": "src/Login.vue", "content": "..." }, "result": "success" } ], "files_modified": ["src/Login.vue"], "reasoning": [ "分析任务需求", "设计组件结构", "编写代码" ], "progress": { "percentage": 45, "current_task": "编写登录表单" } } ``` ### 进度解析流程 ```mermaid graph LR A[CodeBuddy CLI] -->|JSON Stream| B[Progress Monitor] B -->|Parse JSON| C[Progress Data] C -->|Emit Event| D[Event Callbacks] D -->|Update| E[Agent UI] ``` --- ## ⚙️ 高级配置 ### 自定义输出解析器 ```javascript const codebuddy = require('./skill'); // 自定义解析器 codebuddy.setOutputParser((jsonLine) => { // 自定义解析逻辑 return { percentage: jsonLine.progress?.percentage || 0, task: jsonLine.progress?.current_task || '处理中' }; }); ``` ### 超时和重试 ```javascript const result = await codebuddy.execute({ task: '复杂重构任务', options: { timeout: 1200, // 20分钟超时 retryCount: 3, // 失败重试3次 retryDelay: 5000 // 重试间隔5秒 } }); ``` --- ## 🐛 调试和日志 ### 启用详细日志 ```javascript const codebuddy = require('./skill'); // 启用调试模式 codebuddy.setDebugMode(true); // 所有 CLI 输出会被记录到控制台 const result = await codebuddy.execute({ task: '创建测试文件' }); ``` ### 查看执行日志 ```javascript // 获取最近的执行日志 const logs = codebuddy.getExecutionLogs(); console.log(logs); // [ // { time: '11:30:01', event: 'CLI_START', command: '...' }, // { time: '11:30:02', event: 'TOOL_CALL', tool: 'write_to_file' }, // { time: '11:30:05', event: 'CLI_END', status: 'success' } // ] ``` --- ## 🚨 错误处理 ### 错误类型 ```typescript enum CodeBuddyErrorType { CLI_NOT_FOUND = 'CLI_NOT_FOUND', // CodeBuddy CLI 未安装 INVALID_TASK = 'INVALID_TASK', // 无效的任务描述 TIMEOUT = 'TIMEOUT', // 执行超时 PERMISSION_DENIED = 'PERMISSION_DENIED', // 权限被拒绝 CLI_ERROR = 'CLI_ERROR' // CLI 执行错误 } ``` ### 错误处理示例 ```javascript try { const result = await codebuddy.execute({ task: '创建文件' }); } catch (error) { if (error.type === 'CLI_NOT_FOUND') { console.error('请先安装 CodeBuddy CLI'); } else if (error.type === 'TIMEOUT') { console.error('任务超时,请增加超时时间'); } else { console.error('执行失败:', error.message); } } ``` --- ## 📦 集成到 Agent ### Developer Agent 集成 ```javascript // developer/agent.js const codebuddy = require('codebuddy-coding'); class DeveloperAgent { async implementFeature(task) { // 使用 CodeBuddy 实现功能 const result = await codebuddy.execute({ task: task.description, context: { projectPath: this.projectPath, files: task.relatedFiles } }); return result; } } ``` ### Architect Agent 集成 ```javascript // architect/agent.js const codebuddy = require('codebuddy-coding'); class ArchitectAgent { async generateProjectScaffold(requirements) { // 使用 CodeBuddy 生成脚手架 const result = await codebuddy.execute({ task: `创建项目脚手架:${requirements}`, options: { permissionMode: 'bypassPermissions' } }); return result; } } ``` --- ## 🧪 测试 ### 运行测试 ```bash # 运行所有测试 npm test # 运行特定测试 npm test -- --grep "CLI Wrapper" ``` ### 测试覆盖 - ✅ CLI Wrapper 单元测试 - ✅ Progress Monitor 单元测试 - ✅ Integration 集成测试 - ✅ E2E 端到端测试 --- ## 📄 许可证 MIT License --- ## 🤝 贡献 欢迎提交 Issue 和 Pull Request! --- ## 📞 支持 如有问题,请联系: - GitHub Issues: [OpenClaw Repository] - Email: support@openclaw.ai --- **让每个 Agent 都拥有 AI 编程能力!** 🚀

Archived SourceRecently Updated
Coding

anti-sycophancy

Three-layer sycophancy defense based on ArXiv 2602.23971. Use /anti-sycophancy install to deploy all layers, or manage individually via install-claude-code / install-openclaw / uninstall / status / verify. Layer 1: CC-only hook; Layer 2: SKILL (cross-platform); Layer 3: CLAUDE.md (CC) / SOUL.md (OC).

Archived SourceRecently Updated
Coding

validation-rule-management

管理校验规则、规则组和校验场景的全流程操作。支持通过统一 CLI 工具快速执行 API 调用,自动处理参数解析、配置加载和错误提示。使用当用户需要进行校验规则管理、规则组维护、校验场景配置、启停操作或相关查询时,即使用户只说"帮我创建一条规则"或"查一下场景列表"也应触发。

Archived SourceRecently Updated