Skip to content

Commit

Permalink
Introduce new Chunk iteration APIs (#8553)
Browse files Browse the repository at this point in the history
* Way more ergonomic
* Same performance
* Less code
* Support for structs!
* Basis for new upcoming codegen'd deserialization APIs (...at some
point)
  • Loading branch information
teh-cmc authored Jan 6, 2025
1 parent 97f816d commit 12f8e06
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 17 deletions.
475 changes: 468 additions & 7 deletions crates/store/re_chunk/src/iter.rs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion crates/store/re_chunk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub use self::builder::{ChunkBuilder, TimeColumnBuilder};
pub use self::chunk::{Chunk, ChunkComponents, ChunkError, ChunkResult, TimeColumn};
pub use self::helpers::{ChunkShared, UnitChunkShared};
pub use self::id::{ChunkId, RowId};
pub use self::iter::{ChunkComponentIter, ChunkComponentIterItem, ChunkIndicesIter};
pub use self::iter::{
ChunkComponentIter, ChunkComponentIterItem, ChunkComponentSlicer, ChunkIndicesIter,
};
pub use self::latest_at::LatestAtQuery;
pub use self::range::{RangeQuery, RangeQueryOptions};
pub use self::transport::TransportChunk;
Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_grpc_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ async fn stream_catalog_async(
)))?;

let recording_uri_arrays: Vec<Box<dyn Arrow2Array>> = chunk
.iter_string(&"id".into())
.iter_slices::<String>("id".into())
.map(|id| {
let rec_id = &id[0]; // each component batch is of length 1 i.e. single 'id' value

Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_query/examples/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn main() -> anyhow::Result<()> {
.flat_map(|chunk| {
izip!(
chunk.iter_component_indices(&query.timeline(), &MyColor::name()),
chunk.iter_primitive::<u32>(&MyColor::name()),
chunk.iter_slices::<u32>(MyColor::name()),
)
});

Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_query/tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ fn query_and_compare(
.flat_map(|chunk| {
itertools::izip!(
chunk.iter_component_indices(&query.timeline(), &MyColor::name()),
chunk.iter_primitive::<u32>(&MyColor::name()),
chunk.iter_slices::<u32>(MyColor::name()),
)
})
.collect_vec();
Expand Down
2 changes: 1 addition & 1 deletion crates/store/re_types_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ pub mod external {
///
#[macro_export]
macro_rules! static_assert_struct_has_fields {
($strct:ty, $($field:ident: $field_typ:ty),+) => {
($strct:ty, $($field:ident: $field_typ:ty),+ $(,)?) => {
const _: fn(&$strct) = |s: &$strct| {
$(let _: &$field_typ = &s.$field;)+
};
Expand Down
36 changes: 32 additions & 4 deletions crates/viewer/re_view/src/results_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ pub trait RangeResultsExt {
/// Returns a zero-copy iterator over all the results for the given `(timeline, component)` pair.
///
/// Call one of the following methods on the returned [`HybridResultsChunkIter`]:
/// * [`HybridResultsChunkIter::primitive`]
/// * [`HybridResultsChunkIter::primitive_array`]
/// * [`HybridResultsChunkIter::string`]
/// * [`HybridResultsChunkIter::slice`]
/// * [`HybridResultsChunkIter::slice_from_struct_field`]
fn iter_as(
&self,
timeline: Timeline,
Expand Down Expand Up @@ -408,7 +407,7 @@ impl RangeResultsExt for HybridResults<'_> {
// ---

use re_chunk::{ChunkComponentIterItem, RowId, TimeInt, Timeline};
use re_chunk_store::external::{re_chunk, re_chunk::external::arrow2};
use re_chunk_store::external::re_chunk;

/// The iterator type backing [`HybridResults::iter_as`].
pub struct HybridResultsChunkIter<'a> {
Expand Down Expand Up @@ -523,4 +522,33 @@ impl<'a> HybridResultsChunkIter<'a> {
)
})
}

/// Iterate as indexed, sliced, deserialized component batches.
///
/// See [`Chunk::iter_slices`] for more information.
pub fn slice<S: 'a + re_chunk::ChunkComponentSlicer>(
&'a self,
) -> impl Iterator<Item = ((TimeInt, RowId), S::Item<'a>)> + 'a {
self.chunks.iter().flat_map(|chunk| {
itertools::izip!(
chunk.iter_component_indices(&self.timeline, &self.component_name),
chunk.iter_slices::<S>(self.component_name)
)
})
}

/// Iterate as indexed, sliced, deserialized component batches for a specific struct field.
///
/// See [`Chunk::iter_slices_from_struct_field`] for more information.
pub fn slice_from_struct_field<S: 'a + re_chunk::ChunkComponentSlicer>(
&'a self,
field_name: &'a str,
) -> impl Iterator<Item = ((TimeInt, RowId), S::Item<'a>)> + 'a {
self.chunks.iter().flat_map(|chunk| {
itertools::izip!(
chunk.iter_component_indices(&self.timeline, &self.component_name),
chunk.iter_slices_from_struct_field::<S>(self.component_name, field_name)
)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ where
// ---

use re_chunk::{Chunk, ChunkComponentIterItem, ComponentName, RowId};
use re_chunk_store::external::{re_chunk, re_chunk::external::arrow2};
use re_chunk_store::external::re_chunk;

/// Iterate `chunks` as indexed deserialized batches.
///
Expand Down Expand Up @@ -239,3 +239,19 @@ pub fn iter_buffer<'a, T: arrow::datatypes::ArrowNativeType + arrow2::types::Nat
)
})
}

/// Iterate `chunks` as indexed primitives.
///
/// See [`Chunk::iter_slices`] for more information.
pub fn iter_slices<'a, T: 'a + re_chunk::ChunkComponentSlicer>(
chunks: &'a std::borrow::Cow<'a, [Chunk]>,
timeline: Timeline,
component_name: ComponentName,
) -> impl Iterator<Item = ((TimeInt, RowId), T::Item<'a>)> + 'a {
chunks.iter().flat_map(move |chunk| {
itertools::izip!(
chunk.iter_component_indices(&timeline, &component_name),
chunk.iter_slices::<T>(component_name)
)
})
}

0 comments on commit 12f8e06

Please sign in to comment.