Repository: pola-rs/polars
File: crates/polars-core/src/series/from.rs (Rust)

Code before:
        };

        let values = Series::_try_from_arrow_unchecked_with_md(
            name,
            vec![values.clone()],
            values.dtype(),
            None,
        )?;

        values.take(&IdxCa::from_chunks_and_dtype(
            PlSmallStr::EMPTY,
            vec![keys.to_boxed()],
            IDX_DTYPE,
        ))
    }
}

#[cfg(feature = "dtype-struct")]
fn convert_month_day_nano_to_struct(chunk: Box<dyn Array>) -> PolarsResult<Box<dyn Array>> {
    let arr: &PrimitiveArray<months_days_ns> = chunk.as_any().downcast_ref().unwrap();

    let mut months_out: Vec<i32> = vec![0; arr.len()];
    let mut days_out: Vec<i32> = vec![0; arr.len()];
    let mut nanoseconds_out: Vec<i64> = vec![0; arr.len()];

    for (i, x) in arr.iter().enumerate() {
        let Some(x) = x else { continue };

        let months: i32 = x.months();
        let days: i32 = x.days();
        let nanoseconds: i64 = x.ns();

        unsafe {
            *months_out.get_unchecked_mut(i) = months;
            *days_out.get_unchecked_mut(i) = days;
            *nanoseconds_out.get_unchecked_mut(i) = nanoseconds;
        }
    }

    let out = StructArray::new(
        DataType::_month_days_ns_struct_type()
            .to_physical()
            .to_arrow(CompatLevel::newest()),
        arr.len(),
        vec![
            PrimitiveArray::<i32>::from_vec(months_out).boxed(),
            PrimitiveArray::<i32>::from_vec(days_out).boxed(),
            PrimitiveArray::<i64>::from_vec(nanoseconds_out).boxed(),
        ],
        arr.validity().cloned(),
    );

Review comment (suggestion):
nit. Why are we zero initializing these vecs? I think we can do a `.collect::<(Vec<_>, Vec<_>, Vec<_>)>()` and they won't have to be zero initialized.

Code after:
        };

        let values = Series::_try_from_arrow_unchecked_with_md(
            name,
            vec![values.clone()],
            values.dtype(),
            None,
        )?;

        values.take(&IdxCa::from_chunks_and_dtype(
            PlSmallStr::EMPTY,
            vec![keys.to_boxed()],
            IDX_DTYPE,
        ))
    }
}

#[cfg(feature = "dtype-struct")]
fn convert_month_day_nano_to_struct(chunk: Box<dyn Array>) -> PolarsResult<Box<dyn Array>> {
    let arr: &PrimitiveArray<months_days_ns> = chunk.as_any().downcast_ref().unwrap();

    let values: &[months_days_ns] = arr.values();
    let output_length = values.len();

    let mut months_out: Vec<i32> = Vec::with_capacity(output_length);
    let mut days_out: Vec<i32> = Vec::with_capacity(output_length);
    let mut nanoseconds_out: Vec<i64> = Vec::with_capacity(output_length);

    for (i, x) in values.iter().enumerate() {
        let months: i32 = x.months();
        let days: i32 = x.days();
        let nanoseconds: i64 = x.ns();

        unsafe {
            months_out.as_mut_ptr().add(i).write(months);
            days_out.as_mut_ptr().add(i).write(days);
            nanoseconds_out.as_mut_ptr().add(i).write(nanoseconds);
        }
    }

    unsafe {
        months_out.set_len(output_length);
        days_out.set_len(output_length);
        nanoseconds_out.set_len(output_length);
    }

    let out = StructArray::new(
        DataType::_month_days_ns_struct_type()
            .to_physical()
            .to_arrow(CompatLevel::newest()),
        arr.len(),