Skip to content

Commit

Permalink
chore: Remove unsafe *_release functions (#19554)
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp authored Nov 1, 2024
1 parent 802e692 commit 0e52706
Show file tree
Hide file tree
Showing 68 changed files with 297 additions and 545 deletions.
3 changes: 1 addition & 2 deletions crates/polars-arrow/src/array/binview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ mod private {
pub use iterator::BinaryViewValueIter;
pub use mutable::MutableBinaryViewArray;
use polars_utils::aliases::{InitHashMaps, PlHashMap};
use polars_utils::slice::GetSaferUnchecked;
use private::Sealed;

use crate::array::binview::view::{validate_binary_view, validate_utf8_only};
Expand Down Expand Up @@ -334,7 +333,7 @@ impl<T: ViewType + ?Sized> BinaryViewArrayGeneric<T> {
/// Assumes that the `i < self.len`.
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> &T {
let v = self.views.get_unchecked_release(i);
let v = self.views.get_unchecked(i);
T::from_bytes_unchecked(v.get_slice_unchecked(&self.buffers))
}

Expand Down
9 changes: 4 additions & 5 deletions crates/polars-arrow/src/array/binview/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::sync::Arc;
use hashbrown::hash_map::Entry;
use polars_error::PolarsResult;
use polars_utils::aliases::{InitHashMaps, PlHashMap};
use polars_utils::slice::GetSaferUnchecked;

use crate::array::binview::iterator::MutableBinaryViewValueIter;
use crate::array::binview::view::validate_utf8_only;
Expand Down Expand Up @@ -187,9 +186,9 @@ impl<T: ViewType + ?Sized> MutableBinaryViewArray<T> {
self.views.push_unchecked(v)
} else {
self.total_buffer_len += len as usize;
let data = buffers.get_unchecked_release(v.buffer_idx as usize);
let data = buffers.get_unchecked(v.buffer_idx as usize);
let offset = v.offset as usize;
let bytes = data.get_unchecked_release(offset..offset + len as usize);
let bytes = data.get_unchecked(offset..offset + len as usize);
let t = T::from_bytes_unchecked(bytes);
self.push_value_ignore_validity(t)
}
Expand All @@ -206,7 +205,7 @@ impl<T: ViewType + ?Sized> MutableBinaryViewArray<T> {
if len <= 12 {
self.views.push_unchecked(v);
} else {
let buffer = buffers.get_unchecked_release(v.buffer_idx as usize);
let buffer = buffers.get_unchecked(v.buffer_idx as usize);
let idx = match self.stolen_buffers.entry(buffer.deref().as_ptr() as usize) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
Expand Down Expand Up @@ -571,7 +570,7 @@ impl<T: ViewType + ?Sized> MutableBinaryViewArray<T> {
let data = if buffer_idx == self.completed_buffers.len() {
self.in_progress_buffer.as_slice()
} else {
self.completed_buffers.get_unchecked_release(buffer_idx)
self.completed_buffers.get_unchecked(buffer_idx)
};

let offset = offset as usize;
Expand Down
24 changes: 7 additions & 17 deletions crates/polars-arrow/src/array/binview/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use bytemuck::{Pod, Zeroable};
use polars_error::*;
use polars_utils::min_max::MinMax;
use polars_utils::nulls::IsNull;
use polars_utils::slice::GetSaferUnchecked;
use polars_utils::total_ord::{TotalEq, TotalOrd};

use crate::buffer::Buffer;
Expand Down Expand Up @@ -147,9 +146,9 @@ impl View {
let ptr = self as *const View as *const u8;
std::slice::from_raw_parts(ptr.add(4), self.length as usize)
} else {
let data = buffers.get_unchecked_release(self.buffer_idx as usize);
let data = buffers.get_unchecked(self.buffer_idx as usize);
let offset = self.offset as usize;
data.get_unchecked_release(offset..offset + self.length as usize)
data.get_unchecked(offset..offset + self.length as usize)
}
}
}
Expand Down Expand Up @@ -443,10 +442,7 @@ pub(super) unsafe fn validate_utf8_only(
if all_buffers.is_empty() {
for view in views {
let len = view.length;
validate_utf8(
view.to_le_bytes()
.get_unchecked_release(4..4 + len as usize),
)?;
validate_utf8(view.to_le_bytes().get_unchecked(4..4 + len as usize))?;
}
return Ok(());
}
Expand All @@ -456,28 +452,22 @@ pub(super) unsafe fn validate_utf8_only(
for view in views {
let len = view.length;
if len <= 12 {
validate_utf8(
view.to_le_bytes()
.get_unchecked_release(4..4 + len as usize),
)?;
validate_utf8(view.to_le_bytes().get_unchecked(4..4 + len as usize))?;
}
}
} else {
for view in views {
let len = view.length;
if len <= 12 {
validate_utf8(
view.to_le_bytes()
.get_unchecked_release(4..4 + len as usize),
)?;
validate_utf8(view.to_le_bytes().get_unchecked(4..4 + len as usize))?;
} else {
let buffer_idx = view.buffer_idx;
let offset = view.offset;
let data = all_buffers.get_unchecked_release(buffer_idx as usize);
let data = all_buffers.get_unchecked(buffer_idx as usize);

let start = offset as usize;
let end = start + len as usize;
let b = &data.as_slice().get_unchecked_release(start..end);
let b = &data.as_slice().get_unchecked(start..end);
validate_utf8(b)?;
};
}
Expand Down
4 changes: 1 addition & 3 deletions crates/polars-arrow/src/array/growable/binary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::utils::extend_offset_values;
use super::Growable;
use crate::array::growable::utils::{extend_validity, prepare_validity};
Expand Down Expand Up @@ -58,7 +56,7 @@ impl<'a, O: Offset> GrowableBinary<'a, O> {

impl<'a, O: Offset> Growable<'a> for GrowableBinary<'a, O> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

let offsets = array.offsets();
Expand Down
4 changes: 1 addition & 3 deletions crates/polars-arrow/src/array/growable/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::Growable;
use crate::array::growable::utils::{extend_validity, prepare_validity};
use crate::array::{Array, BooleanArray};
Expand Down Expand Up @@ -51,7 +49,7 @@ impl<'a> GrowableBoolean<'a> {

impl<'a> Growable<'a> for GrowableBoolean<'a> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

let values = array.values();
Expand Down
10 changes: 3 additions & 7 deletions crates/polars-arrow/src/array/growable/dictionary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::{make_growable, Growable};
use crate::array::growable::utils::{extend_validity, prepare_validity};
use crate::array::{Array, DictionaryArray, DictionaryKey, PrimitiveArray};
Expand Down Expand Up @@ -93,13 +91,11 @@ impl<'a, T: DictionaryKey> GrowableDictionary<'a, T> {
impl<'a, T: DictionaryKey> Growable<'a> for GrowableDictionary<'a, T> {
#[inline]
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let keys_array = *self.keys.get_unchecked_release(index);
let keys_array = *self.keys.get_unchecked(index);
extend_validity(&mut self.validity, keys_array, start, len);

let values = &keys_array
.values()
.get_unchecked_release(start..start + len);
let offset = self.offsets.get_unchecked_release(index);
let values = &keys_array.values().get_unchecked(start..start + len);
let offset = self.offsets.get_unchecked(index);
self.key_values.extend(
values
.iter()
Expand Down
6 changes: 2 additions & 4 deletions crates/polars-arrow/src/array/growable/fixed_binary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::Growable;
use crate::array::growable::utils::{extend_validity, prepare_validity};
use crate::array::{Array, FixedSizeBinaryArray};
Expand Down Expand Up @@ -53,13 +51,13 @@ impl<'a> GrowableFixedSizeBinary<'a> {

impl<'a> Growable<'a> for GrowableFixedSizeBinary<'a> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

let values = array.values();

self.values.extend_from_slice(
values.get_unchecked_release(start * self.size..start * self.size + len * self.size),
values.get_unchecked(start * self.size..start * self.size + len * self.size),
);
}

Expand Down
6 changes: 2 additions & 4 deletions crates/polars-arrow/src/array/growable/fixed_size_list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::{make_growable, Growable};
use crate::array::growable::utils::{extend_validity, extend_validity_copies, prepare_validity};
use crate::array::{Array, FixedSizeListArray};
Expand Down Expand Up @@ -70,7 +68,7 @@ impl<'a> GrowableFixedSizeList<'a> {

impl<'a> Growable<'a> for GrowableFixedSizeList<'a> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

self.length += len;
Expand All @@ -81,7 +79,7 @@ impl<'a> Growable<'a> for GrowableFixedSizeList<'a> {
}

unsafe fn extend_copies(&mut self, index: usize, start: usize, len: usize, copies: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity_copies(&mut self.validity, array, start, len, copies);

self.length += len * copies;
Expand Down
13 changes: 4 additions & 9 deletions crates/polars-arrow/src/array/growable/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::{make_growable, Growable};
use crate::array::growable::utils::{extend_validity, prepare_validity};
use crate::array::{Array, ListArray};
Expand All @@ -14,19 +12,16 @@ unsafe fn extend_offset_values<O: Offset>(
start: usize,
len: usize,
) {
let array = growable.arrays.get_unchecked_release(index);
let array = growable.arrays.get_unchecked(index);
let offsets = array.offsets();

growable
.offsets
.try_extend_from_slice(offsets, start, len)
.unwrap();

let end = offsets
.buffer()
.get_unchecked_release(start + len)
.to_usize();
let start = offsets.buffer().get_unchecked_release(start).to_usize();
let end = offsets.buffer().get_unchecked(start + len).to_usize();
let start = offsets.buffer().get_unchecked(start).to_usize();
let len = end - start;
growable.values.extend(index, start, len);
}
Expand Down Expand Up @@ -80,7 +75,7 @@ impl<'a, O: Offset> GrowableList<'a, O> {

impl<'a, O: Offset> Growable<'a> for GrowableList<'a, O> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);
extend_offset_values::<O>(self, index, start, len);
}
Expand Down
10 changes: 4 additions & 6 deletions crates/polars-arrow/src/array/growable/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::Growable;
use crate::array::growable::utils::{extend_validity, extend_validity_copies, prepare_validity};
use crate::array::{Array, PrimitiveArray};
Expand Down Expand Up @@ -58,24 +56,24 @@ impl<'a, T: NativeType> GrowablePrimitive<'a, T> {
impl<'a, T: NativeType> Growable<'a> for GrowablePrimitive<'a, T> {
#[inline]
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

let values = array.values().as_slice();
self.values
.extend_from_slice(values.get_unchecked_release(start..start + len));
.extend_from_slice(values.get_unchecked(start..start + len));
}

#[inline]
unsafe fn extend_copies(&mut self, index: usize, start: usize, len: usize, copies: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity_copies(&mut self.validity, array, start, len, copies);

let values = array.values().as_slice();
self.values.reserve(len * copies);
for _ in 0..copies {
self.values
.extend_from_slice(values.get_unchecked_release(start..start + len));
.extend_from_slice(values.get_unchecked(start..start + len));
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/polars-arrow/src/array/growable/structure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::{make_growable, Growable};
use crate::array::growable::utils::{extend_validity, prepare_validity};
use crate::array::{Array, StructArray};
Expand Down Expand Up @@ -71,7 +69,7 @@ impl<'a> GrowableStruct<'a> {

impl<'a> Growable<'a> for GrowableStruct<'a> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

self.length += len;
Expand Down
4 changes: 1 addition & 3 deletions crates/polars-arrow/src/array/growable/utf8.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::sync::Arc;

use polars_utils::slice::GetSaferUnchecked;

use super::utils::extend_offset_values;
use super::Growable;
use crate::array::growable::utils::{extend_validity, prepare_validity};
Expand Down Expand Up @@ -59,7 +57,7 @@ impl<'a, O: Offset> GrowableUtf8<'a, O> {

impl<'a, O: Offset> Growable<'a> for GrowableUtf8<'a, O> {
unsafe fn extend(&mut self, index: usize, start: usize, len: usize) {
let array = *self.arrays.get_unchecked_release(index);
let array = *self.arrays.get_unchecked(index);
extend_validity(&mut self.validity, array, start, len);

let offsets = array.offsets();
Expand Down
8 changes: 3 additions & 5 deletions crates/polars-arrow/src/array/growable/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use polars_utils::slice::GetSaferUnchecked;

use crate::array::Array;
use crate::bitmap::MutableBitmap;
use crate::offset::Offset;
Expand All @@ -12,9 +10,9 @@ pub(super) unsafe fn extend_offset_values<O: Offset>(
start: usize,
len: usize,
) {
let start_values = offsets.get_unchecked_release(start).to_usize();
let end_values = offsets.get_unchecked_release(start + len).to_usize();
let new_values = &values.get_unchecked_release(start_values..end_values);
let start_values = offsets.get_unchecked(start).to_usize();
let end_values = offsets.get_unchecked(start + len).to_usize();
let new_values = &values.get_unchecked(start_values..end_values);
buffer.extend_from_slice(new_values);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod mutable;
pub use mutable::*;
use polars_error::{polars_bail, PolarsResult};
use polars_utils::index::{Bounded, Indexable, NullCount};
use polars_utils::slice::{GetSaferUnchecked, SliceAble};
use polars_utils::slice::SliceAble;

/// A [`PrimitiveArray`] is Arrow's semantically equivalent of an immutable `Vec<Option<T>>` where
/// T is [`NativeType`] (e.g. [`i32`]). It implements [`Array`].
Expand Down Expand Up @@ -215,7 +215,7 @@ impl<T: NativeType> PrimitiveArray<T> {
/// Caller must be sure that `i < self.len()`
#[inline]
pub unsafe fn value_unchecked(&self, i: usize) -> T {
*self.values.get_unchecked_release(i)
*self.values.get_unchecked(i)
}

// /// Returns the element at index `i` or `None` if it is null
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-arrow/src/bitmap/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use chunk_iterator::{BitChunk, BitChunkIterExact, BitChunks, BitChunksExact}
pub use chunks_exact_mut::BitChunksExactMut;
pub use fmt::fmt;
pub use iterator::BitmapIter;
use polars_utils::slice::{load_padded_le_u64, GetSaferUnchecked};
use polars_utils::slice::load_padded_le_u64;
pub use slice_iterator::SlicesIterator;
pub use zip_validity::{ZipValidity, ZipValidityIter};

Expand Down Expand Up @@ -39,7 +39,7 @@ pub fn set_bit_in_byte(byte: u8, i: usize, value: bool) -> u8 {
/// `i >= bytes.len() * 8` results in undefined behavior.
#[inline(always)]
pub unsafe fn get_bit_unchecked(bytes: &[u8], i: usize) -> bool {
let byte = *bytes.get_unchecked_release(i / 8);
let byte = *bytes.get_unchecked(i / 8);
let bit = (byte >> (i % 8)) & 1;
bit != 0
}
Expand Down
Loading

0 comments on commit 0e52706

Please sign in to comment.