Skip to content

Commit

Permalink
SDK batching/revamp 2.1: clock example for Rust (#2000)
Browse files Browse the repository at this point in the history
* version crossbeam at the workspace level

* more DataRow size helpers

* DataTableBatcher

* lints

* lints

* self review

* don't expose shutdown to make errors impossible

* doc

* backport

* backport

* introduce RecordingStream

* clean up old stuff from the before time

* self-review

* ordered data columns in data tables

* tests

* even more tests

* rogue todo

* batching is now a reality

* some extra peace of mind

* added Rust clock example

* revert

* lock shenanigans

* lock shenanigans

* merge shenanigans

* address PR comments

* fix python example

* lock shenanigans

* self-review
  • Loading branch information
teh-cmc authored May 4, 2023
1 parent b2b3064 commit f77a8ec
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 3 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions examples/python/clock/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
LENGTH_H: Final = 4.0

WIDTH_S: Final = 0.25
WIDTH_M: Final = 0.5
WIDTH_H: Final = 0.7
WIDTH_M: Final = 0.4
WIDTH_H: Final = 0.6


def log_clock(steps: int) -> None:
Expand Down Expand Up @@ -61,7 +61,7 @@ def rotate(angle: float, len: float) -> Tuple[float, float, float]:
point_h = np.array(rotate(math.tau * scaled_h, LENGTH_H))
color_h = (int(255 - (scaled_h * 255)), int(scaled_h * 255), 255, 255)
rr.log_point("world/hours_pt", position=point_h, color=color_h)
rr.log_arrow("world/hours_hand", origin=[0.0, 0.0, 0.0], vector=point_h, color=color_h, width_scale=WIDTH_M)
rr.log_arrow("world/hours_hand", origin=[0.0, 0.0, 0.0], vector=point_h, color=color_h, width_scale=WIDTH_H)


if __name__ == "__main__":
Expand Down
14 changes: 14 additions & 0 deletions examples/rust/clock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "clock"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
publish = false

[dependencies]
rerun = { path = "../../../crates/rerun", features = ["web_viewer"] }

anyhow = "1.0"
clap = { version = "4.0", features = ["derive"] }
glam = "0.22"
117 changes: 117 additions & 0 deletions examples/rust/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Builds an analog clock using Rerun's `Arrow3D` primitive.
//!
//! This is a great benchmark for many small events.
//!
//! Usage:
//! ```
//! cargo run -p clock -- --help
//! ```
use std::f32::consts::TAU;

use rerun::components::{Arrow3D, Box3D, ColorRGBA, Radius, Vec3D, ViewCoordinates};
use rerun::coordinates::SignedAxis3;
use rerun::time::{Time, TimePoint, TimeType, Timeline};
use rerun::{external::re_log, MsgSender, RecordingStream};

#[derive(Debug, clap::Parser)]
#[clap(author, version, about)]
struct Args {
#[command(flatten)]
rerun: rerun::clap::RerunArgs,

#[clap(long, default_value = "10000")]
steps: usize,
}

fn run(rec_stream: &RecordingStream, args: &Args) -> anyhow::Result<()> {
const LENGTH_S: f32 = 20.0;
const LENGTH_M: f32 = 10.0;
const LENGTH_H: f32 = 4.0;
const WIDTH_S: f32 = 0.25;
const WIDTH_M: f32 = 0.4;
const WIDTH_H: f32 = 0.6;

let view_coords = ViewCoordinates::from_up_and_handedness(
SignedAxis3::POSITIVE_Y,
rerun::coordinates::Handedness::Right,
);
MsgSender::new("world")
.with_timeless(true)
.with_component(&[view_coords])?
.send(rec_stream)?;

MsgSender::new("world/frame")
.with_timeless(true)
.with_component(&[Box3D::new(LENGTH_S, LENGTH_S, 1.0)])?
.send(rec_stream)?;

fn sim_time(at: f64) -> TimePoint {
let timeline_sim_time = Timeline::new("sim_time", TimeType::Time);
let time = Time::from_seconds_since_epoch(at);
[(timeline_sim_time, time.into())].into()
}

fn pos(angle: f32, length: f32) -> Vec3D {
Vec3D::new(length * angle.sin(), length * angle.cos(), 0.0)
}

fn color(angle: f32, blue: u8) -> ColorRGBA {
let c = (angle * 255.0) as u8;
ColorRGBA::from_unmultiplied_rgba(255 - c, c, blue, u8::max(128, blue))
}

fn log_hand(
rec_stream: &RecordingStream,
name: &str,
step: usize,
angle: f32,
length: f32,
width: f32,
blue: u8,
) -> anyhow::Result<()> {
let point = pos(angle * TAU, length);
let color = color(angle, blue);
MsgSender::new(format!("world/{name}_pt"))
.with_timepoint(sim_time(step as _))
.with_component(&[point])?
.with_component(&[color])?
.send(rec_stream)?;
MsgSender::new(format!("world/{name}_hand"))
.with_timepoint(sim_time(step as _))
.with_component(&[Arrow3D {
origin: glam::Vec3::ZERO.into(),
vector: point,
}])?
.with_component(&[color])?
.with_component(&[Radius(width * 0.5)])?
.send(rec_stream)?;

Ok(())
}

for step in 0..args.steps {
#[rustfmt::skip]
log_hand(rec_stream, "seconds", step, (step % 60) as f32 / 60.0, LENGTH_S, WIDTH_S, 0)?;
#[rustfmt::skip]
log_hand(rec_stream, "minutes", step, (step % 3600) as f32 / 3600.0, LENGTH_M, WIDTH_M, 128)?;
#[rustfmt::skip]
log_hand(rec_stream, "hours", step, (step % 43200) as f32 / 43200.0, LENGTH_H, WIDTH_H, 255)?;
}

Ok(())
}

fn main() -> anyhow::Result<()> {
re_log::setup_native_logging();

use clap::Parser as _;
let args = Args::parse();

let default_enabled = true;
args.rerun
.clone()
.run("clock", default_enabled, move |rec_stream| {
run(&rec_stream, &args).unwrap();
})
}

1 comment on commit f77a8ec

@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: f77a8ec Previous: b2b3064 Ratio
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at/default 390 ns/iter (± 3) 283 ns/iter (± 1) 1.38
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/primary/default 265 ns/iter (± 0) 207 ns/iter (± 4) 1.28
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/secondaries/default 427 ns/iter (± 3) 336 ns/iter (± 0) 1.27
datastore/num_rows=1000/num_instances=1000/gc/default 2475365 ns/iter (± 8749) 1727435 ns/iter (± 7681) 1.43
mono_points_arrow/generate_message_bundles 28987832 ns/iter (± 459051) 21352857 ns/iter (± 397047) 1.36
mono_points_arrow_batched/generate_message_bundles 22042776 ns/iter (± 1122619) 15775910 ns/iter (± 48880) 1.40
mono_points_arrow_batched/generate_messages 4985911 ns/iter (± 308597) 3005202 ns/iter (± 10855) 1.66
mono_points_arrow_batched/encode_log_msg 1323711 ns/iter (± 5884) 1056809 ns/iter (± 6483) 1.25
mono_points_arrow_batched/encode_total 31800525 ns/iter (± 1025418) 20078853 ns/iter (± 58621) 1.58
mono_points_arrow_batched/decode_log_msg 742123 ns/iter (± 9136) 456570 ns/iter (± 908) 1.63
mono_points_arrow_batched/decode_message_bundles 7875347 ns/iter (± 484025) 6036403 ns/iter (± 6711) 1.30
mono_points_arrow_batched/decode_total 9203597 ns/iter (± 489102) 6500443 ns/iter (± 11306) 1.42
batch_points_arrow/encode_log_msg 321363 ns/iter (± 1198) 228986 ns/iter (± 573) 1.40
batch_points_arrow/encode_total 579013 ns/iter (± 3345) 439261 ns/iter (± 458) 1.32
arrow_mono_points/insert 2345801450 ns/iter (± 10423925) 1533346633 ns/iter (± 4365327) 1.53
arrow_mono_points/query 1189880 ns/iter (± 9185) 875906 ns/iter (± 1442) 1.36
arrow_batch_points/query 16875 ns/iter (± 17) 12035 ns/iter (± 9) 1.40
arrow_batch_vecs/query 387662 ns/iter (± 964) 294137 ns/iter (± 819) 1.32

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

Please sign in to comment.