---
name: rust-module-optimizer
description: "A faster, leaner version of a Rust module with the public API untouched: allocations removed, clones turned into borrows, hot paths tightened, each change commented, and the tests carried over to prove nothing broke. Use when a Rust module is too slow or uses too much memory and needs a performance rewrite that keeps its public API and tests intact. Do not use for adding features, changing the API, or reviewing architecture without rewriting. Related topics: coding, rust."
---

# Rust Module Optimizer

A faster, leaner version of a Rust module with the public API untouched: allocations removed, clones turned into borrows, hot paths tightened, each change commented, and the tests carried over to prove nothing broke.

## When to use

Use this skill when a Rust module is too slow or uses too much memory and needs a performance rewrite that keeps its public API and tests intact. Do not use it for adding features, changing the API, or reviewing architecture without rewriting.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. Identify the hot paths and the allocation or locking patterns on them before editing a line.
3. Apply the cheap wins first: borrows instead of clones, &str instead of String, pre-sized Vecs, iterator chains instead of intermediate collects.
4. For memory targets, reorder struct fields and narrow integer types where the value range allows, keeping serialisation stable.
5. Preserve every public signature, doc comment, and observable behaviour, and skip any change that would alter results at the edges.
6. Carry the existing tests over unchanged in intent, comment each performance-critical edit, and write the five-bullet changelog.

## Inputs

- **Module name** (free text, e.g. "my_module")
- **Current source** (longer free text)
- **Target** (choose one: Lower latency, Lower memory usage, Both)
- **Rust edition** (choose one: 2015, 2018, 2021, 2024)
- **Allow unsafe blocks** (choose one: Yes, No)
- **Dependencies to keep** (choose any: serde, tokio, async-std, clap, anyhow)

## Output

The complete optimised module in a rust code block followed by a changelog of up to five bullets, with no other text.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Rust Module Optimisation

Optimise this Rust module for performance.

- Module name: {{Module name}}
- Current source: {{Current source}}
- Target: {{Target}}
- Rust edition: {{Rust edition}}
- Allow unsafe blocks: {{Allow unsafe blocks}}
- Dependencies to keep: {{Dependencies to keep}}

Read the code for its hot paths before changing anything, and say in the changelog which ones you targeted. Work from the cheapest wins outward: clones that can be borrows, Strings that can be &str, Vec growth that can be pre-sized with with_capacity, collect calls that an iterator chain can avoid, locks held longer than needed, and boxed trait objects that generics can replace on a hot path. For memory, shrink structs by reordering fields and using smaller integer types where the range allows. Keep every public signature, doc comment, and behaviour identical; when a change alters observable behaviour even at the edges, leave it out and mention it in the changelog. Use unsafe only when allowed and only with a SAFETY comment stating the invariant. Add no crate that is not in the keep list. Carry existing tests over to the new code with their intent unchanged, and add a comment on each performance-critical change.

Return the complete optimised module in a rust code block, then a changelog of up to five bullets describing the key changes and the hot path each one serves. Nothing else.

## Pitfalls

- Replacing a HashMap with a Vec for cache locality when the keys are looked up at random, which makes lookups linear.
- Narrowing an integer field that serde serialises, which changes the wire format for existing data.
- Adding a dependency such as smallvec or rayon that is not on the keep list, which fails the build in a locked workspace.
