Skip to content

Commit b7b01d3

Browse files
Avoid redundant allocations in struct layout reader (#8851)
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 `insert(0, ..)`. The previous code `collect`ed each `Vec` at exact field-count capacity, then `insert(0, ..)` forced a reallocation **and** an O(n) shift of every element. Both are now gone. ## Checks - `cargo build -p vortex-layout` ✅ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Joe Isaacs <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent c5e075a commit b7b01d3

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

‎vortex-layout/src/layouts/struct_/reader.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ impl StructReader {
8282
.collect()
8383
});
8484

85-
let mut dtypes: Vec<DType> = struct_dt.fields().collect();
86-
let mut names: Vec<Arc<str>> = struct_dt
87-
.names()
88-
.iter()
89-
.map(|x| Arc::clone(x.inner()))
90-
.collect();
91-
92-
if layout.dtype.is_nullable() {
93-
dtypes.insert(0, DType::Bool(Nullability::NonNullable));
94-
names.insert(0, Arc::from("validity"));
85+
let nullable = layout.dtype.is_nullable();
86+
let extra = nullable as usize;
87+
88+
let mut dtypes: Vec<DType> = Vec::with_capacity(struct_dt.nfields() + extra);
89+
let mut names: Vec<Arc<str>> = Vec::with_capacity(struct_dt.nfields() + extra);
90+
if nullable {
91+
dtypes.push(DType::Bool(Nullability::NonNullable));
92+
names.push(Arc::from("validity"));
9593
}
94+
dtypes.extend(struct_dt.fields());
95+
names.extend(struct_dt.names().iter().map(|x| Arc::clone(x.inner())));
9696

9797
let lazy_children = LazyReaderChildren::new(
9898
Arc::clone(&layout.children),

0 commit comments

Comments
 (0)