Identify and eliminate unnecessary memory allocations in performance-critical code paths. This includes pre-allocating collections when the size is known, using static strings for common values, and avoiding intermediate data structures.
Identify and eliminate unnecessary memory allocations in performance-critical code paths. This includes pre-allocating collections when the size is known, using static strings for common values, and avoiding intermediate data structures.
Key strategies:
Cow::Borrowed
or FastStaticString
instead of allocating new stringsExample from HTTP method handling:
// Instead of always allocating:
fn method_slow(method: &http::Method) -> String {
method.to_string() // Always allocates
}
// Use static strings for common cases:
fn method_fast(method: &http::Method) -> Cow<'static, str> {
match *method {
Method::GET => Cow::Borrowed("GET"),
Method::POST => Cow::Borrowed("POST"),
// ... other common methods
_ => Cow::Owned(method.to_string()),
}
}
This optimization is particularly important in hot code paths where even small allocations can compound into significant performance overhead. Always measure the impact, as the complexity trade-off should be justified by measurable performance gains.
Enter the URL of a public GitHub repository