Skip to content

Commit

Permalink
more things
Browse files Browse the repository at this point in the history
  • Loading branch information
scsmithr committed Dec 19, 2024
1 parent d73f410 commit 5615ac2
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/rayexec_bullet/src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;

use rayexec_error::{RayexecError, Result};

use crate::compute::util::IntoExtactSizeIterator;
use crate::compute::util::IntoExactSizedIterator;

/// An LSB ordered bitmap.
#[derive(Clone, Default, PartialEq, Eq)]
Expand Down Expand Up @@ -301,7 +301,7 @@ impl Extend<bool> for Bitmap {
}
}

impl<'a> IntoExtactSizeIterator for &'a Bitmap {
impl<'a> IntoExactSizedIterator for &'a Bitmap {
type Item = bool;
type IntoIter = BitmapIter<'a>;

Expand Down
21 changes: 20 additions & 1 deletion crates/rayexec_bullet/src/compute/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
use rayexec_error::Result;

/// Similar to `IntoIterator`, but for an iterator with an exact size.
pub trait IntoExtactSizeIterator {
pub trait IntoExactSizedIterator {
type Item;
type IntoIter: ExactSizeIterator<Item = Self::Item>;

/// Converts self into the `ExactSizeIteror`.
fn into_iter(self) -> Self::IntoIter;
}

impl<I> IntoExactSizedIterator for I
where
I: IntoIterator,
I::IntoIter: ExactSizeIterator,
{
type Item = I::Item;
type IntoIter = I::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.into_iter()
}
}

pub trait FromExactSizedIterator<A>: Sized {
fn from_iter<T: IntoExactSizedIterator<Item = A>>(iter: T) -> Self;
}
9 changes: 9 additions & 0 deletions crates/rayexec_bullet/src/exp/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ impl<R> Array<R>
where
R: ReservationTracker,
{
pub fn new(datatype: DataType, buffer: ArrayBuffer<R>) -> Self {
let validity = Validity::new_all_valid(buffer.len());
Array {
datatype,
validity,
buffer,
}
}

pub fn validity(&self) -> &Validity {
&self.validity
}
Expand Down
97 changes: 93 additions & 4 deletions crates/rayexec_bullet/src/exp/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ pub mod physical_type;
pub mod reservation;
pub mod string_view;

use physical_type::{PhysicalStorage, PhysicalType, PhysicalUtf8};
use std::marker::PhantomData;

use physical_type::{PhysicalI32, PhysicalStorage, PhysicalType, PhysicalUtf8};
use rayexec_error::{RayexecError, Result};
use reservation::{NopReservationTracker, Reservation, ReservationTracker};
use string_view::{StringViewBuffer, StringViewHeap};
use string_view::{StringViewBuffer, StringViewHeap, StringViewMetadataUnion};

use crate::compute::util::{FromExactSizedIterator, IntoExactSizedIterator};

#[derive(Debug)]
pub struct ArrayBuffer<R: ReservationTracker = NopReservationTracker> {
Expand Down Expand Up @@ -55,6 +59,14 @@ where
})
}

pub fn len(&self) -> usize {
self.data.len
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn try_as_slice<S: PhysicalStorage>(&self) -> Result<&[S::PrimaryBufferType]> {
if S::PHYSICAL_TYPE != self.physical_type {
return Err(
Expand Down Expand Up @@ -97,8 +109,8 @@ impl<R: ReservationTracker> Drop for ArrayBuffer<R> {
fn drop(&mut self) {
let ptr = self.data.ptr;

let len = self.data.len * self.physical_type.buffer_mem_size();
let cap = self.data.cap * self.physical_type.buffer_mem_size();
let len = self.data.len * self.physical_type.primary_buffer_mem_size();
let cap = self.data.cap * self.physical_type.primary_buffer_mem_size();

let vec = unsafe { Vec::from_raw_parts(ptr, len, cap) };
std::mem::drop(vec);
Expand Down Expand Up @@ -160,8 +172,65 @@ impl<R: ReservationTracker> RawBufferParts<R> {
}
}

#[derive(Debug)]
pub struct PrimBufferBuilder<S: PhysicalStorage> {
_s: PhantomData<S>,
}

impl<S: PhysicalStorage> PrimBufferBuilder<S> {
pub fn from_iter<I>(iter: I) -> Result<ArrayBuffer>
where
I: IntoExactSizedIterator<Item = S::PrimaryBufferType>,
{
let iter = iter.into_iter();
let mut data =
RawBufferParts::try_new::<S::PrimaryBufferType>(&NopReservationTracker, iter.len())?;

let data_slice = unsafe { data.as_slice_mut() };
for (idx, val) in iter.enumerate() {
data_slice[idx] = val;
}

Ok(ArrayBuffer {
physical_type: S::PHYSICAL_TYPE,
data,
child: ChildBuffer::None,
})
}
}

#[derive(Debug)]
pub struct StringViewBufferBuilder;

impl StringViewBufferBuilder {
pub fn from_iter<A, I>(iter: I) -> Result<ArrayBuffer>
where
A: AsRef<str>,
I: IntoExactSizedIterator<Item = A>,
{
let iter = iter.into_iter();
let mut data =
RawBufferParts::try_new::<StringViewMetadataUnion>(&NopReservationTracker, iter.len())?;

let mut heap = StringViewHeap::new();

let data_slice = unsafe { data.as_slice_mut() };
for (idx, val) in iter.enumerate() {
let metadata = heap.push_bytes(val.as_ref().as_bytes());
data_slice[idx] = metadata;
}

Ok(ArrayBuffer {
physical_type: PhysicalUtf8::PHYSICAL_TYPE,
data,
child: ChildBuffer::StringViewHeap(heap),
})
}
}

#[cfg(test)]
mod tests {
use addressable::AddressableStorage;
use physical_type::PhysicalI32;
use reservation::AtomicReservationTracker;

Expand All @@ -184,4 +253,24 @@ mod tests {
let total_reserved = tracker.total_reserved();
assert_eq!(0, total_reserved);
}

#[test]
fn new_from_prim_iter() {
let buf = PrimBufferBuilder::<PhysicalI32>::from_iter([4, 5, 6]).unwrap();
let slice = buf.try_as_slice::<PhysicalI32>().unwrap();

assert_eq!(&[4, 5, 6], slice)
}

#[test]
fn new_from_strings_iter() {
let buf =
StringViewBufferBuilder::from_iter(["a", "bb", "ccc", "ddddddddddddddd"]).unwrap();
let view_buf = buf.try_as_string_view_buffer().unwrap();

assert_eq!("a", view_buf.get(0).unwrap());
assert_eq!("bb", view_buf.get(1).unwrap());
assert_eq!("ccc", view_buf.get(2).unwrap());
assert_eq!("ddddddddddddddd", view_buf.get(3).unwrap());
}
}
3 changes: 2 additions & 1 deletion crates/rayexec_bullet/src/exp/buffer/physical_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ pub enum PhysicalType {
}

impl PhysicalType {
pub fn buffer_mem_size(&self) -> usize {
pub fn primary_buffer_mem_size(&self) -> usize {
match self {
Self::Int8 => PhysicalI8::buffer_mem_size(),
Self::Int32 => PhysicalI32::buffer_mem_size(),
Self::Interval => PhysicalInterval::buffer_mem_size(),
Self::Utf8 => PhysicalUtf8::buffer_mem_size(),
_ => unimplemented!(),
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/rayexec_bullet/src/exp/buffer/string_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ pub struct StringViewHeap {
}

impl StringViewHeap {
// TODO: Tracker
pub const fn new() -> Self {
StringViewHeap { buffer: Vec::new() }
}

pub fn push_bytes(&mut self, value: &[u8]) -> StringViewMetadataUnion {
if value.len() as i32 <= 12 {
// Store completely inline.
Expand Down

0 comments on commit 5615ac2

Please sign in to comment.