Optimize Hot Path Code for Zero Memory Allocation
Audits intensive loops and hot paths to eliminate heap allocations, slice cloning, and temporary objects while providing benchmark code.
Agent prompt for Claude and ChatGPT
You get: Allocation Hotspots, optimized Implementation and benchmark Harness
Prompt
mkdir -p .agents/skills/optimize-hot-path-code-for-zero-memory-allocation && curl -sL "https://prompts-gpt.com/prompts/optimize-hot-path-code-for-zero-memory-allocation/raw" -o .agents/skills/optimize-hot-path-code-for-zero-memory-allocation/SKILL.mdAct as a systems performance engineer. I will paste a code snippet representing a hot path, loop, or stream processor.
Your job is to identify every unnecessary heap allocation, slice reallocation, and temporary buffer clone, then provide zero-allocation or pooled alternatives.
Rules:
1. Identify exact lines where allocations occur on each iteration or function call.
2. Rewrite the code using memory-efficient techniques (such as pre-allocated buffers, object pools, or direct slice reuse).
3. Provide a runnable benchmark snippet comparing the original logic against the optimized version.
4. Avoid premature micro-optimizations that damage readability without reducing GC or heap pressure.
Output format:
- Allocation Hotspots (bullet list naming line concepts and allocation causes)
- Optimized Implementation (complete replacement code block)
- Benchmark Harness (ready-to-run benchmark snippet)
- Tradeoffs and Caveats (concurrency constraints, buffer reset requirements, or safety risks)
Please paste your code snippet, specify the programming language, and state your target throughput or batch size.Example output
Tips and common edits
Why this works
- Forces explicit line-by-line identification of heap triggers before writing code.
- Requires a matching benchmark harness to mathematically verify zero allocations.
- Mandates a caveats section to protect against dangerous pointer reuse and race conditions.
Paste directly into GitHub Copilot Chat or Claude alongside your hot loop or packet handler. Works with Go, Rust, C#, Java, or C++.
Common edits
- Swap sync.Pool usage for fixed stack arrays when packet size is strictly bounded.
- Ask for unsafe zero-copy string casts if strings must be preserved downstream.
Reuse it: Use in a workflowAdapt in the builder
Prompt details
- By
- Prompts-GPT editors
- Published
Did this prompt work?
More like this
Speed Up a Slow Function with Big-O Analysis and a Benchmark
You get the exact bottleneck line, current vs. proposed Big-O, an optimized drop-in function, and a small benchmark that proves the speedup before you adopt it.
Design a Benchmark Harness for Claude Code Agent Accuracy
Create a structured testing framework to evaluate coding agent performance on pull requests using deterministic ground-truth verification.
Compress bloated instructions while preserving accuracy
Trim wordy system prompts down to their essential constraints. Use this to reduce token consumption, lower API latency, and eliminate confusing prompt contradictions.
Refactor nested legacy loops into flat readable helper functions
Decompose deeply nested legacy loops into single-purpose functions. Use this when reviewing complex procedural code that is hard to test or debug.
Automated Dead Code Cleanup Plan for Codex Repositories
Get a systematic plan to identify and safely remove unused exports and dead code from your codebase using characterization tests to prevent regressions.
Perform Static Call Graph Trace Before Modifying Code
Generate a clear visual map of function calls and file dependencies to identify exactly where your changes will ripple before you type a single line of code.
- Refactor Monolithic God Classes into Clean Services
- Refactor Nested Procedural Code into Pure Pipelines
- Sync Test Factories and Fixtures with Database Migrations
- Port a Function or Module to Another Language With Tests Proving Same Behavior
- Cursor Refactor of a Component With Zero Behaviour Change
- Unit Test Plan and Test Code for an Existing Function