Prompt
Minimize memory allocations and optimize allocation patterns to improve performance. Key practices:
- Pre-allocate collections with known sizes
- Avoid unnecessary intermediate allocations
- Reuse allocated memory when possible
- Use appropriate capacity reservations
Example - Before:
// Allocates new Vec for each row
result.try_push(Some(
x.chunks_exact(element_size)
.map(|val| T::cast_le(val))
.collect::<Vec<_>>(),
));
Example - After:
// Pre-allocate with known capacity
let mut builder = make_builder(arr.dtype());
builder.reserve(output_length);
// Avoid intermediate allocations
result.try_push(
x.chunks_exact(element_size)
.map(|val| T::cast_le(val))
);
Benefits:
- Reduced memory churn
- Better performance through fewer allocations
- More predictable memory usage patterns
- Lower risk of allocation-related performance spikes