From 562cc9371e4e0586f2e9f4ed0603d4bcfe513325 Mon Sep 17 00:00:00 2001 From: Joe Isaacs Date: Mon, 20 Jul 2026 13:56:17 +0100 Subject: [PATCH] Avoid redundant allocations in struct layout reader Pre-size the dtype and name vectors to their final length and push the validity entry first, instead of collecting at field-count capacity and then inserting at index 0. This removes both the reallocation and the O(n) element shift caused by `insert(0, ..)`. Signed-off-by: Joe Isaacs Co-Authored-By: Claude Opus 4.8 (1M context) --- vortex-layout/src/layouts/struct_/reader.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/vortex-layout/src/layouts/struct_/reader.rs b/vortex-layout/src/layouts/struct_/reader.rs index ab03a4624fa..18a14a2c97b 100644 --- a/vortex-layout/src/layouts/struct_/reader.rs +++ b/vortex-layout/src/layouts/struct_/reader.rs @@ -82,17 +82,17 @@ impl StructReader { .collect() }); - let mut dtypes: Vec = struct_dt.fields().collect(); - let mut names: Vec> = struct_dt - .names() - .iter() - .map(|x| Arc::clone(x.inner())) - .collect(); - - if layout.dtype.is_nullable() { - dtypes.insert(0, DType::Bool(Nullability::NonNullable)); - names.insert(0, Arc::from("validity")); + let nullable = layout.dtype.is_nullable(); + let extra = nullable as usize; + + let mut dtypes: Vec = Vec::with_capacity(struct_dt.nfields() + extra); + let mut names: Vec> = Vec::with_capacity(struct_dt.nfields() + extra); + if nullable { + dtypes.push(DType::Bool(Nullability::NonNullable)); + names.push(Arc::from("validity")); } + dtypes.extend(struct_dt.fields()); + names.extend(struct_dt.names().iter().map(|x| Arc::clone(x.inner()))); let lazy_children = LazyReaderChildren::new( Arc::clone(&layout.children),