Skip to content

Commit 932055e

Browse files
committed
from_vec
Signed-off-by: Andrew Duffy <andrew@a10y.dev>
1 parent d9e61d0 commit 932055e

1 file changed

Lines changed: 21 additions & 56 deletions

File tree

encodings/bytebool/src/array.rs

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl VTable for ByteBool {
131131
}
132132
let buffer = buffers[0].clone();
133133

134-
let data = ByteBoolData::new(buffer, validity.clone());
134+
let data = ByteBoolData::new(buffer);
135135
let slots = ByteBoolData::make_slots(&validity, len);
136136
Ok(ArrayParts::new(self.clone(), dtype.clone(), len, data).with_slots(slots))
137137
}
@@ -199,10 +199,17 @@ pub struct ByteBool;
199199

200200
impl ByteBool {
201201
pub fn new(buffer: BufferHandle, validity: Validity) -> ByteBoolArray {
202+
if let Some(len) = validity.maybe_len() {
203+
assert_eq!(
204+
buffer.len(),
205+
len,
206+
"ByteBool validity and bytes must have same length"
207+
);
208+
}
202209
let dtype = DType::Bool(validity.nullability());
203210

204211
let slots = ByteBoolData::make_slots(&validity, buffer.len());
205-
let data = ByteBoolData::new(buffer, validity);
212+
let data = ByteBoolData::new(buffer);
206213
let len = data.len();
207214
unsafe {
208215
Array::from_parts_unchecked(
@@ -214,29 +221,22 @@ impl ByteBool {
214221
/// Construct a [`ByteBoolArray`] from a `Vec<bool>` and validity.
215222
pub fn from_vec<V: Into<Validity>>(data: Vec<bool>, validity: V) -> ByteBoolArray {
216223
let validity = validity.into();
217-
let data = ByteBoolData::from_vec(data, validity.clone());
218-
let dtype = DType::Bool(validity.nullability());
219-
let len = data.len();
220-
let slots = ByteBoolData::make_slots(&validity, len);
221-
unsafe {
222-
Array::from_parts_unchecked(
223-
ArrayParts::new(ByteBool, dtype, len, data).with_slots(slots),
224-
)
225-
}
224+
// NOTE: this will not cause allocation on release builds
225+
let bytes: Vec<u8> = data.into_iter().map(|b| b as u8).collect();
226+
let handle = BufferHandle::new_host(ByteBuffer::from(bytes));
227+
ByteBool::new(handle, validity)
226228
}
227229

228230
/// Construct a [`ByteBoolArray`] from optional bools.
229231
pub fn from_option_vec(data: Vec<Option<bool>>) -> ByteBoolArray {
230232
let validity = Validity::from_iter(data.iter().map(|v| v.is_some()));
231-
let data = ByteBoolData::from(data);
232-
let dtype = DType::Bool(validity.nullability());
233-
let len = data.len();
234-
let slots = ByteBoolData::make_slots(&validity, len);
235-
unsafe {
236-
Array::from_parts_unchecked(
237-
ArrayParts::new(ByteBool, dtype, len, data).with_slots(slots),
238-
)
239-
}
233+
// NOTE: this will not cause allocation on release builds
234+
let bytes: Vec<u8> = data
235+
.into_iter()
236+
.map(|b| b.unwrap_or_default() as u8)
237+
.collect();
238+
let handle = BufferHandle::new_host(ByteBuffer::from(bytes));
239+
ByteBool::new(handle, validity)
240240
}
241241
}
242242

@@ -267,17 +267,7 @@ impl ByteBoolData {
267267
vec![validity_to_child(validity, len)]
268268
}
269269

270-
pub fn new(buffer: BufferHandle, validity: Validity) -> Self {
271-
let length = buffer.len();
272-
if let Some(vlen) = validity.maybe_len()
273-
&& length != vlen
274-
{
275-
vortex_panic!(
276-
"Buffer length ({}) does not match validity length ({})",
277-
length,
278-
vlen
279-
);
280-
}
270+
pub fn new(buffer: BufferHandle) -> Self {
281271
Self { buffer }
282272
}
283273

@@ -291,14 +281,6 @@ impl ByteBoolData {
291281
self.buffer.len() == 0
292282
}
293283

294-
// TODO(ngates): deprecate construction from vec
295-
pub fn from_vec<V: Into<Validity>>(data: Vec<bool>, validity: V) -> Self {
296-
let validity = validity.into();
297-
// SAFETY: we are transmuting a Vec<bool> into a Vec<u8>
298-
let data: Vec<u8> = unsafe { std::mem::transmute(data) };
299-
Self::new(BufferHandle::new_host(ByteBuffer::from(data)), validity)
300-
}
301-
302284
pub fn buffer(&self) -> &BufferHandle {
303285
&self.buffer
304286
}
@@ -330,23 +312,6 @@ impl OperationsVTable<ByteBool> for ByteBool {
330312
}
331313
}
332314

333-
impl From<Vec<bool>> for ByteBoolData {
334-
fn from(value: Vec<bool>) -> Self {
335-
Self::from_vec(value, Validity::AllValid)
336-
}
337-
}
338-
339-
impl From<Vec<Option<bool>>> for ByteBoolData {
340-
fn from(value: Vec<Option<bool>>) -> Self {
341-
let validity = Validity::from_iter(value.iter().map(|v| v.is_some()));
342-
343-
// This doesn't reallocate, and the compiler even vectorizes it
344-
let data = value.into_iter().map(Option::unwrap_or_default).collect();
345-
346-
Self::from_vec(data, validity)
347-
}
348-
}
349-
350315
#[cfg(test)]
351316
mod tests {
352317
use vortex_array::ArrayContext;

0 commit comments

Comments
 (0)