Minimize memory allocations and optimize allocation patterns to improve performance. Key practices:

  1. Pre-allocate collections with known sizes
  2. Avoid unnecessary intermediate allocations
  3. Reuse allocated memory when possible
  4. 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: