Skip to content

Commit

Permalink
Replace checked casts with as for performance (#1918)
Browse files Browse the repository at this point in the history
Remove num dependency from arrow-buffer

Deprecate unnecessary methods
  • Loading branch information
tustvold committed Sep 27, 2022
1 parent a7cf274 commit 45aa763
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 274 deletions.
13 changes: 3 additions & 10 deletions arrow-array/src/array/dictionary_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,20 +308,13 @@ impl<K: ArrowPrimitiveType> DictionaryArray<K> {

/// Return an iterator over the keys (indexes into the dictionary)
pub fn keys_iter(&self) -> impl Iterator<Item = Option<usize>> + '_ {
self.keys
.iter()
.map(|key| key.map(|k| k.to_usize().expect("Dictionary index not usize")))
self.keys.iter().map(|key| key.map(|k| k.as_usize()))
}

/// Return the value of `keys` (the dictionary key) at index `i`,
/// cast to `usize`, `None` if the value at `i` is `NULL`.
pub fn key(&self, i: usize) -> Option<usize> {
self.keys.is_valid(i).then(|| {
self.keys
.value(i)
.to_usize()
.expect("Dictionary index not usize")
})
self.keys.is_valid(i).then(|| self.keys.value(i).as_usize())
}

/// Downcast this dictionary to a [`TypedDictionaryArray`]
Expand Down Expand Up @@ -586,7 +579,7 @@ where

unsafe fn value_unchecked(&self, index: usize) -> Self::Item {
let val = self.dictionary.keys.value_unchecked(index);
let value_idx = val.to_usize().unwrap();
let value_idx = val.as_usize();

// As dictionary keys are only verified for non-null indexes
// we must check the value is within bounds
Expand Down
14 changes: 6 additions & 8 deletions arrow-array/src/array/list_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,16 @@ impl<OffsetSize: OffsetSizeTrait> GenericListArray<OffsetSize> {
/// # Safety
/// Caller must ensure that the index is within the array bounds
pub unsafe fn value_unchecked(&self, i: usize) -> ArrayRef {
let end = *self.value_offsets().get_unchecked(i + 1);
let start = *self.value_offsets().get_unchecked(i);
self.values
.slice(start.to_usize().unwrap(), (end - start).to_usize().unwrap())
let end = self.value_offsets().get_unchecked(i + 1).as_usize();
let start = self.value_offsets().get_unchecked(i).as_usize();
self.values.slice(start, end - start)
}

/// Returns ith value of this list array.
pub fn value(&self, i: usize) -> ArrayRef {
let end = self.value_offsets()[i + 1];
let start = self.value_offsets()[i];
self.values
.slice(start.to_usize().unwrap(), (end - start).to_usize().unwrap())
let end = self.value_offsets()[i + 1].as_usize();
let start = self.value_offsets()[i].as_usize();
self.values.slice(start, end - start)
}

/// Returns the offset values in the offsets buffer
Expand Down
6 changes: 3 additions & 3 deletions arrow-array/src/builder/string_dictionary_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ fn get_bytes<'a, K: ArrowNativeType>(values: &'a StringBuilder, key: &K) -> &'a
let offsets = values.offsets_slice();
let values = values.values_slice();

let idx = key.to_usize().unwrap();
let end_offset = offsets[idx + 1].to_usize().unwrap();
let start_offset = offsets[idx].to_usize().unwrap();
let idx = key.as_usize();
let end_offset = offsets[idx + 1].as_usize();
let start_offset = offsets[idx].as_usize();

&values[start_offset..end_offset]
}
Expand Down
1 change: 0 additions & 1 deletion arrow-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ path = "src/lib.rs"
bench = false

[dependencies]
num = { version = "0.4", default-features = false, features = ["std"] }
half = { version = "2.0", default-features = false }

[dev-dependencies]
Expand Down
Loading

0 comments on commit 45aa763

Please sign in to comment.