<!--
title: Optimize frequent operations
domain: runtimes
topic: Performance Optimization
language: Rust
source: tree-sitter/tree-sitter
updated: 2025-07-13
url: https://awesomereviewers.com/reviewers/tree-sitter-optimize-frequent-operations/
-->

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()` over `fs::copy() + fs::remove_file()`
- **Avoid repeated allocations**: Use `Vec::push()` and `join()` instead of repeated `format!()` calls
- **Reuse objects**: Don't recreate expensive objects like `Regex` instances in loops

Example of avoiding redundant calls:
```rust
// 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.
