optimize-hot-path
Use when the user says this is slow, asks to speed up a function, optimize a hot path, profile and optimize, rewrite the slow part, make a script faster, or port it to Rust. Profiles the real workload, records real calls, and delivers one faster function backed by exact replay ch
Install
npx skills add https://github.com/Heyvhuang/copybook-skills/tree/main/plugins/skills/skills/optimize-hot-path
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install heyvhuang-copybook-skills@llmmart
git clone https://github.com/Heyvhuang/copybook-skills.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole heyvhuang/copybook-skills collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
Optimize a hot path
Guessing at bottlenecks wastes effort on cold code and often breaks edge cases. This workflow profiles the real workload, records real calls as a behavior contract, and tests each optimization against a written prediction and repeated timings.
1. Profile the real workload
- Run the program on its real workload, through the project's own documented entry point (its batch or run command) and real input, not generated data; profile that run, not the unit test suite.
- Pick the single hottest function that dominates total execution time.
- Skip any function taking under 2% of the run, because speeding it up will barely change end-to-end time.
- Check whether the target mixes pure computation with I/O, randomness, or shared state; if so, note how to isolate the pure logic first.
2. Record real calls and outputs
- Wrap or instrument the target function during a workload run to capture real inputs, return values, and expected errors.
- Keep enough calls to cover real input diversity, including non-ASCII text, empty values, and boundary cases.
- Replace secrets, credentials, or personal data with realistic synthetic values before saving recordings.
- Keep the recorded outputs untouched as the ground truth for exactness checks.
3. State the hypothesis and prediction
- Write down why the function is slow, the specific change you plan to test, and the speedup you predict before editing code.
- Note semantic details the change must preserve, such as Unicode behavior, numeric precision, ordering, or error paths.
- Test one focused change per iteration so the benchmark isolates its effect.
4. Change the implementation
- Optimize in the current language first by cutting allocations, hoisting repeated work out of loops, improving algorithms, or using faster built-ins.
- Keep the public signature and exact behavior across every branch, including fallback paths when adding a fast path for common inputs.
- Port the function to a native module or compiled language such as Rust only when same-language changes fall short and call overhead will not cancel the gain.
5. Replay, benchmark, and keep or discard
- Replay every recorded call through the updated function and require exact output equality.
- If any call diverges, inspect the failing input, fix the code, and replay the entire set before timing.
- Remove any recording overhead, benchmark the original and updated code on the same workload for at least 5 repeated runs each, and compare their median runtimes.
- Keep the change only when all recorded outputs match and the median speedup justifies any added complexity; otherwise discard it and revise your hypothesis.
- Report the function's profiled share of runtime, the hypothesis and predicted speedup next to the measured median speedup, the replay pass count, and the keep or discard verdict.
Based on EL4CTEO/fast-anything (Apache-2.0 or MIT) and chszlab/AgenticAlgorithmEngineering (MIT), rewritten by Copybook.
Files (copybook-skills)
-
CHANGES.md 1.7 KB
Loading… -
example.md 1.7 KB
Loading… -
LICENSE 1.1 KB · in bundle
-
LICENSE-MIT 1 KB · in bundle
-
LICENSE.txt 11.1 KB
Loading… -
SKILL.md 3.4 KB
--- name: optimize-hot-path description: "Use when the user says this is slow, asks to speed up a function, optimize a hot path, profile and optimize, rewrite the slow part, make a script faster, or port it to Rust. Profiles the real workload, records real calls, and delivers one faster function backed by exact replay checks and median timings across repeated runs. Not for page-level React performance (optimize-react)." license: MIT (see LICENSE and LICENSE-MIT and LICENSE.txt) --- # Optimize a hot path Guessing at bottlenecks wastes effort on cold code and often breaks edge cases. This workflow profiles the real workload, records real calls as a behavior contract, and tests each optimization against a written prediction and repeated timings. ## 1. Profile the real workload - Run the program on its real workload, through the project's own documented entry point (its batch or run command) and real input, not generated data; profile that run, not the unit test suite. - Pick the single hottest function that dominates total execution time. - Skip any function taking under 2% of the run, because speeding it up will barely change end-to-end time. - Check whether the target mixes pure computation with I/O, randomness, or shared state; if so, note how to isolate the pure logic first. ## 2. Record real calls and outputs - Wrap or instrument the target function during a workload run to capture real inputs, return values, and expected errors. - Keep enough calls to cover real input diversity, including non-ASCII text, empty values, and boundary cases. - Replace secrets, credentials, or personal data with realistic synthetic values before saving recordings. - Keep the recorded outputs untouched as the ground truth for exactness checks. ## 3. State the hypothesis and prediction - Write down why the function is slow, the specific change you plan to test, and the speedup you predict before editing code. - Note semantic details the change must preserve, such as Unicode behavior, numeric precision, ordering, or error paths. - Test one focused change per iteration so the benchmark isolates its effect. ## 4. Change the implementation - Optimize in the current language first by cutting allocations, hoisting repeated work out of loops, improving algorithms, or using faster built-ins. - Keep the public signature and exact behavior across every branch, including fallback paths when adding a fast path for common inputs. - Port the function to a native module or compiled language such as Rust only when same-language changes fall short and call overhead will not cancel the gain. ## 5. Replay, benchmark, and keep or discard - Replay every recorded call through the updated function and require exact output equality. - If any call diverges, inspect the failing input, fix the code, and replay the entire set before timing. - Remove any recording overhead, benchmark the original and updated code on the same workload for at least 5 repeated runs each, and compare their median runtimes. - Keep the change only when all recorded outputs match and the median speedup justifies any added complexity; otherwise discard it and revise your hypothesis. - Report the function's profiled share of runtime, the hypothesis and predicted speedup next to the measured median speedup, the replay pass count, and the keep or discard verdict. Based on EL4CTEO/fast-anything (Apache-2.0 or MIT) and chszlab/AgenticAlgorithmEngineering (MIT), rewritten by Copybook.
Reviews (0)
No reviews yet.
Comments
Sign in to join the conversation.
No comments yet. Start the conversation.