Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
reduce and debug assert bound checks (#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 7, 2022
1 parent 98e4913 commit e4947cc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
28 changes: 17 additions & 11 deletions src/array/growable/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,22 @@ impl<'a, T: DictionaryKey> GrowableDictionary<'a, T> {
let validity = std::mem::take(&mut self.key_validity);
let key_values = std::mem::take(&mut self.key_values);

#[cfg(debug_assertions)]
{
crate::array::specification::check_indexes(&key_values, self.values.len()).unwrap();
}
let keys =
PrimitiveArray::<T>::try_new(T::PRIMITIVE.into(), key_values.into(), validity.into())
.unwrap();

DictionaryArray::<T>::try_new(self.data_type.clone(), keys, self.values.clone()).unwrap()
PrimitiveArray::<T>::new(T::PRIMITIVE.into(), key_values.into(), validity.into());

// Safety - the invariant of this struct ensures that this is up-held
unsafe {
DictionaryArray::<T>::try_new_unchecked(
self.data_type.clone(),
keys,
self.values.clone(),
)
.unwrap()
}
}
}

Expand Down Expand Up @@ -141,12 +152,7 @@ impl<'a, T: DictionaryKey> Growable<'a> for GrowableDictionary<'a, T> {

impl<'a, T: DictionaryKey> From<GrowableDictionary<'a, T>> for DictionaryArray<T> {
#[inline]
fn from(val: GrowableDictionary<'a, T>) -> Self {
let data_type = T::PRIMITIVE.into();
let keys =
PrimitiveArray::<T>::try_new(data_type, val.key_values.into(), val.key_validity.into())
.unwrap();

DictionaryArray::<T>::try_new(val.data_type.clone(), keys, val.values).unwrap()
fn from(mut val: GrowableDictionary<'a, T>) -> Self {
val.to()
}
}
32 changes: 16 additions & 16 deletions src/array/growable/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,20 @@ impl<'a, O: Offset> GrowableList<'a, O> {
let offsets = std::mem::take(&mut self.offsets);
let values = self.values.as_box();

ListArray::<O>::new(
self.arrays[0].data_type().clone(),
offsets.into(),
values,
validity.into(),
)
#[cfg(debug_assertions)]
{
crate::array::specification::try_check_offsets(&offsets, values.len()).unwrap();
}

// Safety - the invariant of this struct ensures that this is up-held
unsafe {
ListArray::<O>::new_unchecked(
self.arrays[0].data_type().clone(),
offsets.into(),
values,
validity.into(),
)
}
}
}

Expand All @@ -135,15 +143,7 @@ impl<'a, O: Offset> Growable<'a> for GrowableList<'a, O> {
}

impl<'a, O: Offset> From<GrowableList<'a, O>> for ListArray<O> {
fn from(val: GrowableList<'a, O>) -> Self {
let mut values = val.values;
let values = values.as_box();

ListArray::<O>::new(
val.arrays[0].data_type().clone(),
val.offsets.into(),
values,
val.validity.into(),
)
fn from(mut val: GrowableList<'a, O>) -> Self {
val.to()
}
}
19 changes: 9 additions & 10 deletions src/array/growable/utf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ impl<'a, O: Offset> GrowableUtf8<'a, O> {
let offsets = std::mem::take(&mut self.offsets);
let values = std::mem::take(&mut self.values);

#[cfg(debug_assertions)]
{
crate::array::specification::try_check_offsets_and_utf8(&offsets, &values).unwrap();
}

unsafe {
Utf8Array::<O>::from_data_unchecked(
Utf8Array::<O>::try_new_unchecked(
self.arrays[0].data_type().clone(),
offsets.into(),
values.into(),
validity.into(),
)
.unwrap()
}
}
}
Expand Down Expand Up @@ -99,14 +105,7 @@ impl<'a, O: Offset> Growable<'a> for GrowableUtf8<'a, O> {
}

impl<'a, O: Offset> From<GrowableUtf8<'a, O>> for Utf8Array<O> {
fn from(val: GrowableUtf8<'a, O>) -> Self {
unsafe {
Utf8Array::<O>::from_data_unchecked(
val.arrays[0].data_type().clone(),
val.offsets.into(),
val.values.into(),
val.validity.into(),
)
}
fn from(mut val: GrowableUtf8<'a, O>) -> Self {
val.to()
}
}

0 comments on commit e4947cc

Please sign in to comment.