Prompt
Identify code paths that execute frequently (loops, hot functions, repeated calls) and optimize them by avoiding redundant computations, expensive operations, and unnecessary allocations.
Key optimization strategies:
- Reorder conditions for short-circuiting: Place cheaper or more likely-to-fail conditions first
- Cache expensive computations: Store results of function calls that are used multiple times
- Choose efficient APIs: Prefer operations like
fs::rename()overfs::copy() + fs::remove_file() - Avoid repeated allocations: Use
Vec::push()andjoin()instead of repeatedformat!()calls - Reuse objects: Don’t recreate expensive objects like
Regexinstances in loops
Example of avoiding redundant calls:
// Before: calling node.is_named() twice
fn caller() {
if node.is_named() {
render_node_range(cursor, node.is_named()); // redundant call
}
}
// After: cache the result
fn caller() {
let is_named = node.is_named();
if is_named {
render_node_range(cursor, is_named);
}
}
Focus especially on optimizing code that processes large datasets, handles user input, or runs in tight loops where small inefficiencies compound into significant performance impacts.