Skip to content

Commit

Permalink
Codegen'd Rust/Arrow (de)ser 7: deserialization (#2554)
Browse files Browse the repository at this point in the history
**Best reviewed on a commit-by-commit basis; in particular the `rerun
codegen` commit is nothing but generated code.**

Implements deserialization for the Rust codegen backend.

---

- #2484
- #2485 
- #2487 
- #2545
- #2546
- #2549
- #2554
- #2570
- #2571

---

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2487

<!-- pr-link-docs:start -->
Docs preview: https://rerun.io/preview/303b165/docs
Examples preview: https://rerun.io/preview/303b165/examples
<!-- pr-link-docs:end -->
  • Loading branch information
teh-cmc committed Jun 30, 2023
1 parent b45a5bf commit de97832
Show file tree
Hide file tree
Showing 19 changed files with 3,337 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This is a sha256 hash for all direct and indirect dependencies of this crate's build script.
# It can be safely removed at anytime to force the build script to run again.
# Check out build.rs to see how it's computed.
70d271f9866784d9f0d25b818cd0d45fff1df72aec62ceae9f167f523a5c2d08
7d42813c538b5c4716e75e225572409d5799dc142ede36243043ef82fc90399e
433 changes: 433 additions & 0 deletions crates/re_types/src/archetypes/fuzzy.rs

Large diffs are not rendered by default.

133 changes: 133 additions & 0 deletions crates/re_types/src/archetypes/points2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,139 @@ impl crate::Archetype for Points2D {
.flatten()
.collect())
}

#[inline]
fn try_from_arrow(
data: impl IntoIterator<Item = (::arrow2::datatypes::Field, Box<dyn ::arrow2::array::Array>)>,
) -> crate::DeserializationResult<Self> {
use crate::Component as _;
let arrays_by_name: ::std::collections::HashMap<_, _> = data
.into_iter()
.map(|(field, array)| (field.name, array))
.collect();
let points = {
let array = arrays_by_name.get("points").ok_or_else(|| {
crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
}
})?;
<crate::components::Point2D>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?
};
let radii = if let Some(array) = arrays_by_name.get("radii") {
Some(
<crate::components::Radius>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?,
)
} else {
None
};
let colors = if let Some(array) = arrays_by_name.get("colors") {
Some(
<crate::components::Color>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?,
)
} else {
None
};
let labels = if let Some(array) = arrays_by_name.get("labels") {
Some(
<crate::components::Label>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?,
)
} else {
None
};
let draw_order = if let Some(array) = arrays_by_name.get("draw_order") {
Some(
<crate::components::DrawOrder>::try_from_arrow_opt(&**array)?
.into_iter()
.next()
.flatten()
.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})?,
)
} else {
None
};
let class_ids = if let Some(array) = arrays_by_name.get("class_ids") {
Some(
<crate::components::ClassId>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?,
)
} else {
None
};
let keypoint_ids = if let Some(array) = arrays_by_name.get("keypoint_ids") {
Some(
<crate::components::KeypointId>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?,
)
} else {
None
};
let instance_keys = if let Some(array) = arrays_by_name.get("instance_keys") {
Some(
<crate::components::InstanceKey>::try_from_arrow_opt(&**array)?
.into_iter()
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: ::arrow2::datatypes::DataType::Null,
})
})
.collect::<crate::DeserializationResult<Vec<_>>>()?,
)
} else {
None
};
Ok(Self {
points,
radii,
colors,
labels,
draw_order,
class_ids,
keypoint_ids,
instance_keys,
})
}
}

impl Points2D {
Expand Down
28 changes: 28 additions & 0 deletions crates/re_types/src/components/class_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,32 @@ impl crate::Component for ClassId {
.boxed()
})
}

#[allow(unused_imports, clippy::wildcard_imports)]
fn try_from_arrow_opt(
data: &dyn ::arrow2::array::Array,
) -> crate::DeserializationResult<Vec<Option<Self>>>
where
Self: Sized,
{
use crate::{Component as _, Datatype as _};
use ::arrow2::{array::*, datatypes::*};
Ok(data
.as_any()
.downcast_ref::<UInt16Array>()
.unwrap()
.into_iter()
.map(|v| v.copied())
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: DataType::Extension(
"rerun.components.ClassId".to_owned(),
Box::new(DataType::UInt16),
None,
),
})
})
.map(|res| res.map(|v| Some(Self(v))))
.collect::<crate::DeserializationResult<Vec<Option<_>>>>()?)
}
}
28 changes: 28 additions & 0 deletions crates/re_types/src/components/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,32 @@ impl crate::Component for Color {
.boxed()
})
}

#[allow(unused_imports, clippy::wildcard_imports)]
fn try_from_arrow_opt(
data: &dyn ::arrow2::array::Array,
) -> crate::DeserializationResult<Vec<Option<Self>>>
where
Self: Sized,
{
use crate::{Component as _, Datatype as _};
use ::arrow2::{array::*, datatypes::*};
Ok(data
.as_any()
.downcast_ref::<UInt32Array>()
.unwrap()
.into_iter()
.map(|v| v.copied())
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: DataType::Extension(
"rerun.components.Color".to_owned(),
Box::new(DataType::UInt32),
None,
),
})
})
.map(|res| res.map(|v| Some(Self(v))))
.collect::<crate::DeserializationResult<Vec<Option<_>>>>()?)
}
}
28 changes: 28 additions & 0 deletions crates/re_types/src/components/draw_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,32 @@ impl crate::Component for DrawOrder {
.boxed()
})
}

#[allow(unused_imports, clippy::wildcard_imports)]
fn try_from_arrow_opt(
data: &dyn ::arrow2::array::Array,
) -> crate::DeserializationResult<Vec<Option<Self>>>
where
Self: Sized,
{
use crate::{Component as _, Datatype as _};
use ::arrow2::{array::*, datatypes::*};
Ok(data
.as_any()
.downcast_ref::<Float32Array>()
.unwrap()
.into_iter()
.map(|v| v.copied())
.map(|v| {
v.ok_or_else(|| crate::DeserializationError::MissingData {
datatype: DataType::Extension(
"rerun.components.DrawOrder".to_owned(),
Box::new(DataType::Float32),
None,
),
})
})
.map(|res| res.map(|v| Some(Self(v))))
.collect::<crate::DeserializationResult<Vec<Option<_>>>>()?)
}
}

1 comment on commit de97832

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Rust Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.25.

Benchmark suite Current: de97832 Previous: afeaa91 Ratio
mono_points_arrow_batched/encode_log_msg 517474 ns/iter (± 942) 411415 ns/iter (± 1178) 1.26
batch_points_arrow/encode_log_msg 88032 ns/iter (± 561) 48852 ns/iter (± 164) 1.80

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.