Skip to content

Commit 1bcacc5

Browse files
committed
spare mut for copy
Signed-off-by: Alexander Droste <[email protected]>
1 parent 63258a2 commit 1bcacc5

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

vortex-compute/src/expand/buffer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::mem::MaybeUninit;
45
use vortex_buffer::{Buffer, BufferMut};
56
use vortex_mask::{Mask, MaskValues};
67

@@ -137,11 +138,7 @@ fn expand_copy<T: Copy>(src: &[T], mask_values: &MaskValues) -> Buffer<T> {
137138
let mask_len = mask_values.len();
138139

139140
let mut target_buf = BufferMut::<T>::with_capacity(mask_len);
140-
141-
// SAFETY: Sufficient capacity has been reserved.
142-
unsafe { target_buf.set_len(mask_len) };
143-
144-
let buf_slice = target_buf.as_mut_slice();
141+
let target_slice = target_buf.spare_capacity_mut();
145142

146143
// Pick the first value as a default value.
147144
let pseudo_default_value = src[0];
@@ -154,14 +151,17 @@ fn expand_copy<T: Copy>(src: &[T], mask_values: &MaskValues) -> Buffer<T> {
154151
// NOTE(0ax1): .value is slow => optimize
155152
if mask_values.value(mask_idx) {
156153
element_idx -= 1;
157-
buf_slice[mask_idx] = src[element_idx];
154+
target_slice[mask_idx].write(src[element_idx]);
158155
} else {
159156
// Initialize with a pseudo-default value. In case we expand into a
160157
// new buffer all false positions need to be initialized.
161-
buf_slice[mask_idx] = pseudo_default_value;
158+
target_slice[mask_idx].write(pseudo_default_value);
162159
}
163160
}
164161

162+
// SAFETY: Buffer has sufficient capacity and all elements have been initialized.
163+
unsafe { target_buf.set_len(mask_len) };
164+
165165
target_buf.freeze()
166166
}
167167

0 commit comments

Comments
 (0)