Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Investigate perf issues with large pointclouds #1006

Closed
wants to merge 4 commits into from
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/re_query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ thiserror.workspace = true


[dev-dependencies]
arrow2_convert.workspace = true
criterion = "0.4"
itertools = "0.10"
mimalloc = "0.1"
Expand Down Expand Up @@ -70,3 +71,7 @@ required-features = ["polars"]
[[bench]]
name = "obj_query_benchmark"
harness = false

[[bench]]
name = "fixedarray"
harness = false
111 changes: 111 additions & 0 deletions crates/re_query/benches/fixedarray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use arrow2::array::FixedSizeListArray;
use arrow2::array::Float32Array;
use arrow2::datatypes::DataType;

use criterion::criterion_group;
use criterion::criterion_main;
use criterion::BatchSize;
use criterion::Criterion;
use criterion::Throughput;
use itertools::Itertools;

const N: usize = 400_000; // roughly a frame of nyud

fn bench(c: &mut Criterion) {
let data = vec![[42.0, 420.0, 4200.0]; N];
let arr = {
let array_flattened = Float32Array::from_vec(data.into_iter().flatten().collect()).boxed();
FixedSizeListArray::new(
FixedSizeListArray::default_datatype(DataType::Float32, 3),
array_flattened,
None,
)
};

let mut group = c.benchmark_group("fixed_size_list_array");
group.throughput(Throughput::Elements(N as u64));

// Test iteration using iter()
group.bench_function("iter", |b| {
let mut count = 0usize;
b.iter_batched(
|| arr.clone(),
|data| {
for p in data.iter() {
let p = p.unwrap();
let mut iter = p
.as_any()
.downcast_ref::<Float32Array>()
.unwrap()
.values_iter();
let x = iter.next().unwrap();
let y = iter.next().unwrap();
let z = iter.next().unwrap();
assert!(iter.next().is_none());
assert_eq!(*x, 42.0);
assert_eq!(*y, 420.0);
assert_eq!(*z, 4200.0);
count += 1;
}
},
BatchSize::SmallInput,
);
});

// Test iteration using values_iter()
group.bench_function("values_iter", |b| {
let mut count = 0usize;
b.iter_batched(
|| arr.clone(),
|data| {
for p in data.values_iter() {
let mut iter = p
.as_any()
.downcast_ref::<Float32Array>()
.unwrap()
.values_iter();
let x = iter.next().unwrap();
let y = iter.next().unwrap();
let z = iter.next().unwrap();
assert!(iter.next().is_none());
assert_eq!(*x, 42.0);
assert_eq!(*y, 420.0);
assert_eq!(*z, 4200.0);
count += 1;
}
},
BatchSize::SmallInput,
);
});

// Test iteration manually using chunks()
group.bench_function("raw_iter", |b| {
let mut count = 0usize;
b.iter_batched(
|| arr.clone(),
|data| {
for mut iter in &data
.values()
.as_any()
.downcast_ref::<Float32Array>()
.unwrap()
.values_iter()
.chunks(3)
{
let x = iter.next().unwrap();
let y = iter.next().unwrap();
let z = iter.next().unwrap();
assert!(iter.next().is_none());
assert_eq!(*x, 42.0);
assert_eq!(*y, 420.0);
assert_eq!(*z, 4200.0);
count += 1;
}
},
BatchSize::SmallInput,
);
});
}

criterion_group!(benches, bench);
criterion_main!(benches);
168 changes: 168 additions & 0 deletions crates/re_query/examples/struct_vs_fixedarray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
use arrow2::array::Array;
use arrow2::array::FixedSizeListArray;
use arrow2::array::Float32Array;
use arrow2::datatypes::DataType;
use arrow2_convert::deserialize::*;
use arrow2_convert::serialize::*;
use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize};

use re_log_types::field_types::FixedSizeArrayField;

#[derive(Clone, Copy, Debug, ArrowField, ArrowSerialize, ArrowDeserialize, PartialEq)]
pub struct Point3D {
pub x: f32,
pub y: f32,
pub z: f32,
}

#[derive(Clone, Copy, Debug, ArrowField, ArrowSerialize, ArrowDeserialize, PartialEq)]
pub struct Point3DFlattened(#[arrow_field(type = "FixedSizeArrayField<f32,3>")] pub [f32; 3]);

fn main() {
const N: usize = 400_000; // roughly a frame of nyud

{
let points = vec![
Point3D {
x: 42.0,
y: 420.0,
z: 4200.0,
};
N
];

let now = std::time::Instant::now();
let mut count = 0usize;
for p in points {
count += 1;
}
eprintln!("iterated {count} native points in {:?}", now.elapsed());
}

eprintln!("---");

{
let points = vec![
Point3D {
x: 42.0,
y: 420.0,
z: 4200.0,
};
N
];

let now = std::time::Instant::now();
let arr: Box<dyn Array> = points.try_into_arrow().unwrap();
// dbg!(arr.data_type());
eprintln!(
"serialized {} arrow struct points in {:?}",
arr.len(),
now.elapsed()
);

let now = std::time::Instant::now();
let mut count = 0usize;
let iter = arrow_array_deserialize_iterator::<Option<Point3D>>(&*arr).unwrap();
for p in iter {
count += 1;
}
eprintln!(
"iterated {count} arrow struct points in {:?}",
now.elapsed()
);
}

eprintln!("---");

{
let points = vec![Point3DFlattened([42.0, 420.0, 4200.0]); N];

let now = std::time::Instant::now();
let arr: Box<dyn Array> = points.try_into_arrow().unwrap();
// dbg!(arr.data_type());
eprintln!(
"serialized {} arrow flat points in {:?}",
arr.len(),
now.elapsed()
);

{
let iter = arrow_array_deserialize_iterator::<Point3DFlattened>(&*arr).unwrap();
let now = std::time::Instant::now();
let mut count = 0usize;
for p in iter {
count += 1;
}
eprintln!("iterated {count} arrow flat points in {:?}", now.elapsed());
// unsafe {
// let N1 = re_log_types::field_types::N1;
// let N2 = re_log_types::field_types::N2;
// eprintln!("step 1 ({N1}) in {:?}", re_log_types::field_types::STEP1);
// eprintln!("step 2 ({N2}) in {:?}", re_log_types::field_types::STEP2);
// }
}
}

eprintln!("---");

{
let data = vec![[42.0, 420.0, 4200.0]; N];

let now = std::time::Instant::now();
let arr = {
let array_flattened =
Float32Array::from_vec(data.into_iter().flatten().collect()).boxed();

FixedSizeListArray::new(
FixedSizeListArray::default_datatype(DataType::Float32, 3),
array_flattened,
None,
)
.boxed()
};
// dbg!(arr.data_type());
eprintln!(
"serialized {} primitive arrow points in {:?}",
arr.len(),
now.elapsed()
);

let list = arr.as_any().downcast_ref::<FixedSizeListArray>().unwrap();
let iter = list.iter();

let now = std::time::Instant::now();
let mut count = 0usize;
for p in iter {
count += 1;
}
eprintln!(
"iterated {count} primitive arrow points in {:?}",
now.elapsed()
);
}

eprintln!("---");

{
let data = vec![[42.0, 420.0, 4200.0]; N];

let now = std::time::Instant::now();
let arr = Float32Array::from_vec(data.into_iter().flatten().collect()).boxed();
// dbg!(arr.data_type());
eprintln!(
"serialized {} arrow floats in {:?}",
arr.len(),
now.elapsed()
);

let list = arr.as_any().downcast_ref::<Float32Array>().unwrap();
let iter = list.iter();

let now = std::time::Instant::now();
let mut count = 0usize;
for p in iter {
count += 1;
}
eprintln!("iterated {count} arrow floats in {:?}", now.elapsed());
}
}
9 changes: 6 additions & 3 deletions crates/re_query/src/entity_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ impl ComponentWithInstances {
});
}

Ok(arrow_array_deserialize_iterator::<Option<C>>(
self.values.as_ref(),
)?)
let now = std::time::Instant::now();
let iter = arrow_array_deserialize_iterator::<Option<C>>(self.values.as_ref())?;
let iter = iter.collect::<Vec<_>>();
eprintln!("took {:?}", now.elapsed());

Ok(iter.into_iter())
}

/// Look up the value that corresponds to a given `Instance` and convert to `Component`
Expand Down
Loading