Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rust/arrow/src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use super::MutableBuffer;
#[derive(Clone, PartialEq, Debug)]
pub struct Buffer {
/// the internal byte buffer.
data: Arc<Bytes>,
data: Arc<Bytes<u8>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jorgecarleitao if Buffer always has Bytes<u8>, I think this alone doesn't address one of the motivations of doing this; being that we'd like to know what boundary to slice buffers in when offsetting arrays.

This would be when using Buffer::offset and Buffer::length on nested arrays.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

However, we can't make Buffer<T> because ArrayData force us to use an homogeneous type across all buffers. We can't drop ArrayData because FFI, IPC, CSV, JSON, equality and array/transform depend on it.

This PR was a stop-gap.

Th target design for me is in the proposal. Specifically, for primitive arrays, it would look like this

But because of what I wrote (a lot of dependencies on ArrayData), it requires a large (like +10k lines large) refactor of this crate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth still pursuing adding more information to Buffer while working separately on the proposal? I'm blocked on some parquet work that I'm doing, because it depends on being able to correctly slice nested arrays.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that any work that unblocks you is worth pursuing :)

A path could be:

  1. Make Buffer support both bit slices and byte slices:
  • Add a new offset_bits to Buffer.
  • Expose a method Buffer::slice_bitmap that increments offset_bits (and panics if called with offset != 0).
  • Make Buffer::slice panic if called with offset_bits != 0.

These will allow us to slice Buffer when it is either holder of bits and a holder of bytes (we just need to use either API independently). This is required because the Boolean array's values are in bits (and thus part of ArrayData::buffers).

  1. Implement a method

pub fn slice(&self, offset: usize, length: usize) -> Self

on every array, that calls slice or slice_bits depending on what the Buffer represents (and also fix the incorrect slicing of arrays with offsets).

  1. make Array::slice call the concrete implementations

  2. Make all calls of get_bit and the like to use Buffer::offset_bits to compute slot nullability and/or values.

Note that I have not tested this and thus this may not work. I also do not know the consequences to IPC and FFI. :/

I am more confident that the proposal works because it passes IPC tests (IPC read and write, little and big endian ;)).


/// The offset into the buffer.
offset: usize,
Expand All @@ -45,7 +45,7 @@ pub struct Buffer {
impl Buffer {
/// Auxiliary method to create a new Buffer
#[inline]
pub fn from_bytes(bytes: Bytes) -> Self {
pub fn from_bytes(bytes: Bytes<u8>) -> Self {
Buffer {
data: Arc::new(bytes),
offset: 0,
Expand Down
47 changes: 26 additions & 21 deletions rust/arrow/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@

//! This module contains an implementation of a contiguous immutable memory region that knows
//! how to de-allocate itself, [`Bytes`].
//! Note that this is a low-level functionality of this crate.

use core::slice;
use std::ptr::NonNull;
use std::sync::Arc;
use std::{fmt::Debug, fmt::Formatter};
use std::{ptr::NonNull, sync::Arc};

use crate::{alloc, ffi};
use crate::ffi;

use super::{alloc, alloc::NativeType};

/// Mode of deallocating memory regions
pub enum Deallocation {
/// Native deallocation, using Rust deallocator with Arrow-specific memory aligment
Native(usize),
/// Foreign interface, via a callback
// Foreign interface, via a callback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to change from // to ///?

Foreign(Arc<ffi::FFI_ArrowArray>),
}

Expand All @@ -55,9 +55,9 @@ impl Debug for Deallocation {
/// and deallocated accordingly [`free_aligned`](memory::free_aligned).
/// When the region is allocated by an foreign allocator, [Deallocation::Foreign], this calls the
/// foreign deallocator to deallocate the region when it is no longer needed.
pub struct Bytes {
pub struct Bytes<T: NativeType> {
/// The raw pointer to be begining of the region
ptr: NonNull<u8>,
ptr: NonNull<T>,

/// The number of bytes visible to this region. This is always smaller than its capacity (when avaliable).
len: usize,
Expand All @@ -66,7 +66,7 @@ pub struct Bytes {
deallocation: Deallocation,
}

impl Bytes {
impl<T: NativeType> Bytes<T> {
/// Takes ownership of an allocated memory region,
///
/// # Arguments
Expand All @@ -81,18 +81,19 @@ impl Bytes {
/// bytes. If the `ptr` and `capacity` come from a `Buffer`, then this is guaranteed.
#[inline]
pub unsafe fn new(
ptr: std::ptr::NonNull<u8>,
ptr: std::ptr::NonNull<T>,
len: usize,
deallocation: Deallocation,
) -> Bytes {
Bytes {
) -> Self {
Self {
ptr,
len,
deallocation,
}
}

fn as_slice(&self) -> &[u8] {
#[inline]
fn as_slice(&self) -> &[T] {
self
}

Expand All @@ -107,7 +108,7 @@ impl Bytes {
}

#[inline]
pub fn ptr(&self) -> NonNull<u8> {
pub fn ptr(&self) -> NonNull<T> {
self.ptr
}

Expand All @@ -121,34 +122,34 @@ impl Bytes {
}
}

impl Drop for Bytes {
impl<T: NativeType> Drop for Bytes<T> {
#[inline]
fn drop(&mut self) {
match &self.deallocation {
Deallocation::Native(capacity) => {
unsafe { alloc::free_aligned::<u8>(self.ptr, *capacity) };
unsafe { alloc::free_aligned(self.ptr, *capacity) };
}
// foreign interface knows how to deallocate itself.
Deallocation::Foreign(_) => (),
}
}
}

impl std::ops::Deref for Bytes {
type Target = [u8];
impl<T: NativeType> std::ops::Deref for Bytes<T> {
type Target = [T];

fn deref(&self) -> &[u8] {
fn deref(&self) -> &[T] {
unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.len) }
}
}

impl PartialEq for Bytes {
fn eq(&self, other: &Bytes) -> bool {
impl<T: NativeType> PartialEq for Bytes<T> {
fn eq(&self, other: &Bytes<T>) -> bool {
self.as_slice() == other.as_slice()
}
}

impl Debug for Bytes {
impl<T: NativeType> Debug for Bytes<T> {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "Bytes {{ ptr: {:?}, len: {}, data: ", self.ptr, self.len,)?;

Expand All @@ -157,3 +158,7 @@ impl Debug for Bytes {
write!(f, " }}")
}
}

// This is sound because `Bytes` is an immutable container
unsafe impl<T: NativeType> Send for Bytes<T> {}
unsafe impl<T: NativeType> Sync for Bytes<T> {}