Skip to content

Commit

Permalink
Refactor: re_sdk no longer depends on re_viewer (#1507)
Browse files Browse the repository at this point in the history
* Refactor: move the session log sink to its own file

* Turn LogSink into a trait

* Move spawning of native viewer to `rerun` crate

* Move the `show()` method to `rerun` crate

* re_sdk no longer depend directly on re_viewer

* Prevent re_viewer from accidentally depending on re_web_viewer_server

* Improve doc-strings

* Remove the native_viewer feature from re_sdk

* Remove direct dependency of rerun_py on re_sdk_comms

* Small doc-fixes

* Move `clap` hookup from `re_sdk` to `rerun`

* Move the `session.serve()` method to trait `rerun::WebViewerSessionExt`

* cleanup

* Remove cyclic-dependency-protection because it caused weird warning:

The lib target `re_viewer` in package `re_viewer v0.2.0 (/Users/emilk/code/rerun/rerun/crates/re_viewer)` has the same output filename as the lib target `re_viewer` in package `re_viewer v0.2.0 (/Users/emilk/code/rerun/rerun/crates/re_viewer)`.

* Fix web-viewer

* Fix python build

* Fix remove_dir_all RUSTSEC

❯ cargo update -p tempfile
    Updating crates.io index
    Removing remove_dir_all v0.5.3
    Updating tempfile v3.3.0 -> v3.4.0

* Trim re_sdk dependency list

* Fix doc-tests

* Fix doclinks

* Document rust_version

* Better docs for set_sink

* Hide `global_session()` behind the `global` feature flag

* Fix typos

* Proper rustc & LLVM versions (#1512)

* add rustc & llvm version to build crates

* integrate everywhere

* crash handler too

* Rename "global" feature to "global_session"

* Replace extension trait with `serve_web_viewer` method

---------

Co-authored-by: Clement Rey <[email protected]>
  • Loading branch information
emilk and teh-cmc authored Mar 6, 2023
1 parent bc32a83 commit 51cdb49
Show file tree
Hide file tree
Showing 32 changed files with 584 additions and 371 deletions.
1 change: 1 addition & 0 deletions .github/workflows/typos.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# https://github.com/crate-ci/typos
# Add exceptions to _typos.toml
# install and run locally: cargo install typos-cli && typos

name: Spell Check
on: [pull_request]
Expand Down
10 changes: 3 additions & 7 deletions Cargo.lock

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

24 changes: 19 additions & 5 deletions crates/re_analytics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,27 @@ impl Event {

/// Adds Rerun version, git hash, build date and similar as properties to the event.
pub fn with_build_info(self, build_info: &re_build_info::BuildInfo) -> Event {
self.with_prop("rerun_version", build_info.version.to_string())
.with_prop("target", build_info.target_triple)
let re_build_info::BuildInfo {
crate_name: _,
version,
rustc_version,
llvm_version,
git_hash: _,
git_branch,
is_in_rerun_workspace,
target_triple,
datetime,
} = build_info;

self.with_prop("rerun_version", version.to_string())
.with_prop("rust_version", (*rustc_version).to_owned())
.with_prop("llvm_version", (*llvm_version).to_owned())
.with_prop("target", *target_triple)
.with_prop("git_hash", build_info.git_hash_or_tag())
.with_prop("git_branch", build_info.git_branch)
.with_prop("build_date", build_info.datetime)
.with_prop("git_branch", *git_branch)
.with_prop("build_date", *datetime)
.with_prop("debug", cfg!(debug_assertions)) // debug-build?
.with_prop("rerun_workspace", build_info.is_in_rerun_workspace)
.with_prop("rerun_workspace", *is_in_rerun_workspace)
}
}

Expand Down
46 changes: 45 additions & 1 deletion crates/re_build_build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub fn export_env_vars() {
set_env("RE_BUILD_GIT_HASH", &git_hash().unwrap_or_default());
set_env("RE_BUILD_GIT_BRANCH", &git_branch().unwrap_or_default());

// rust version
let (rustc, llvm) = rust_version().unwrap_or_default();
set_env("RE_BUILD_RUSTC_VERSION", &rustc);
set_env("RE_BUILD_LLVM_VERSION", &llvm);

// We need to check `IS_IN_RERUN_WORKSPACE` in the build-script (here),
// because otherwise it won't show up when compiling through maturin.
// We must also make an exception for when we build actual wheels (on CI) for release.
Expand Down Expand Up @@ -74,7 +79,7 @@ fn set_env(name: &str, value: &str) {
println!("cargo:rustc-env={name}={value}");
}

fn run_command(cmd: &'static str, args: &[&str]) -> anyhow::Result<String> {
fn run_command(cmd: &str, args: &[&str]) -> anyhow::Result<String> {
let output = Command::new(cmd)
.args(args)
.output()
Expand All @@ -101,3 +106,42 @@ fn git_branch() -> anyhow::Result<String> {
fn git_path(path: &str) -> anyhow::Result<String> {
run_command("git", &["rev-parse", "--git-path", path])
}

/// Returns `(rustc, LLVM)` versions.
///
/// Defaults to `"unknown"` if, for whatever reason, the output from `rustc -vV` did not contain
/// version information and/or the output format underwent breaking changes.
fn rust_version() -> anyhow::Result<(String, String)> {
let cmd = std::env::var("RUSTC").unwrap_or("rustc".into());
let args = &["-vV"];

// $ rustc -vV
// rustc 1.67.0 (fc594f156 2023-01-24)
// binary: rustc
// commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
// commit-date: 2023-01-24
// host: x86_64-unknown-linux-gnu
// release: 1.67.0
// LLVM version: 15.0.6

let res = run_command(&cmd, args)?;

let mut rustc_version = None;
let mut llvm_version = None;

for line in res.lines() {
if let Some(version) = line.strip_prefix("rustc ") {
rustc_version = Some(version.to_owned());
} else if let Some(version) = line.strip_prefix("LLVM version: ") {
llvm_version = Some(version.to_owned());
}
}

// NOTE: This should never happen, but if it does, we want to make sure we can differentiate
// between "failed to invoke rustc" vs. "rustc's output did not contain any version (??)
// and/or the output format has changed".
Ok((
rustc_version.unwrap_or_else(|| "unknown".to_owned()),
llvm_version.unwrap_or_else(|| "unknown".to_owned()),
))
}
19 changes: 19 additions & 0 deletions crates/re_build_info/src/build_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ pub struct BuildInfo {
/// Crate version, parsed from `CARGO_PKG_VERSION`, ignoring any `+metadata` suffix.
pub version: super::CrateVersion,

/// The raw version string of the Rust compiler used, or an empty string.
pub rustc_version: &'static str,

/// The raw version string of the LLVM toolchain used, or an empty string.
pub llvm_version: &'static str,

/// Git commit hash, or empty string.
pub git_hash: &'static str,

Expand Down Expand Up @@ -55,15 +61,28 @@ impl std::fmt::Display for BuildInfo {
let Self {
crate_name,
version,
rustc_version,
llvm_version,
git_hash,
git_branch,
is_in_rerun_workspace: _,
target_triple,
datetime,
} = self;

let rustc_version = (!rustc_version.is_empty()).then(|| format!("rustc {rustc_version}"));
let llvm_version = (!llvm_version.is_empty()).then(|| format!("LLVM {llvm_version}"));

write!(f, "{crate_name} {version}")?;

if let Some(rustc_version) = rustc_version {
write!(f, " [{rustc_version}")?;
if let Some(llvm_version) = llvm_version {
write!(f, ", {llvm_version}")?;
}
write!(f, "]")?;
}

write!(f, " {target_triple}")?;

if !git_branch.is_empty() {
Expand Down
4 changes: 4 additions & 0 deletions crates/re_build_info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ macro_rules! build_info {
$crate::BuildInfo {
crate_name: env!("CARGO_PKG_NAME"),
version: $crate::CrateVersion::parse(env!("CARGO_PKG_VERSION")),
rustc_version: env!("RE_BUILD_RUSTC_VERSION"),
llvm_version: env!("RE_BUILD_LLVM_VERSION"),
git_hash: env!("RE_BUILD_GIT_HASH"),
git_branch: env!("RE_BUILD_GIT_BRANCH"),
// TODO(cmc): `PartialEq` is not available in const contexts, so this won't actually
// build if you try to instantiate a BuildInfo in a constant.
is_in_rerun_workspace: env!("RE_BUILD_IS_IN_RERUN_WORKSPACE") == "yes",
target_triple: env!("RE_BUILD_TARGET_TRIPLE"),
datetime: env!("RE_BUILD_DATETIME"),
Expand Down
3 changes: 2 additions & 1 deletion crates/re_log_types/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ fn test_encode_decode() {
is_official_example: true,
started: Time::now(),
recording_source: crate::RecordingSource::RustSdk {
rust_version: env!("CARGO_PKG_RUST_VERSION").into(),
rustc_version: String::new(),
llvm_version: String::new(),
},
},
})];
Expand Down
8 changes: 6 additions & 2 deletions crates/re_log_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ pub enum RecordingSource {

/// The official Rerun Rust Logging SDK
RustSdk {
rust_version: String,
rustc_version: String,
llvm_version: String,
},

/// Perhaps from some manual data ingestion?
Expand All @@ -273,7 +274,10 @@ impl std::fmt::Display for RecordingSource {
match self {
Self::Unknown => "Unknown".fmt(f),
Self::PythonSdk(version) => write!(f, "Python {version} SDK"),
Self::RustSdk { rust_version } => write!(f, "Rust {rust_version} SDK"),
Self::RustSdk {
rustc_version: rust_version,
llvm_version: _,
} => write!(f, "Rust {rust_version} SDK"),
Self::Other(string) => format!("{string:?}").fmt(f), // put it in quotes
}
}
Expand Down
39 changes: 13 additions & 26 deletions crates/re_sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,64 +17,51 @@ all-features = true


[features]
default = ["demo", "glam", "image", "native_viewer"]

## Enable telemetry using our analytics SDK.
analytics = ["re_web_viewer_server?/analytics", "re_viewer?/analytics"]
default = ["demo", "glam", "image"]

## Enable the `demo` module (helpers for Rerun examples).
demo = []

## Add support for some math operations using [`glam`](https://crates.io/crates/glam/).
glam = ["re_log_types/glam"]

## Add the `global_session` method.
global_session = ["dep:once_cell", "dep:parking_lot"]

## Integration with the [`image`](https://crates.io/crates/image/) crate.
image = ["re_log_types/image"]

## Support for the native viewer.
native_viewer = ["image", "dep:re_viewer"]

## Support serving a web viewer over HTTP.
##
## Enabling this adds quite a bit to the compile time and binary size,
## since it requires compiling and bundling the viewer as wasm.
##
## You also need to install some additional tools, which you can do by running
## [`scripts/setup_web.sh`](https://github.com/rerun-io/rerun/blob/main/scripts/setup_web.sh).
web_viewer = ["dep:re_ws_comms", "dep:re_web_viewer_server", "dep:webbrowser"]
# Add a tokio runtime to the `Session` type.
tokio_runtime = ["dep:tokio"]


[dependencies]
re_build_info.workspace = true
re_error.workspace = true
re_log.workspace = true
re_log_types.workspace = true
re_log.workspace = true
re_memory.workspace = true
re_sdk_comms = { workspace = true, features = ["client"] }
re_smart_channel.workspace = true
re_string_interner.workspace = true

anyhow.workspace = true
arrow2.workspace = true
crossbeam = "0.8"
document-features = "0.2"
lazy_static.workspace = true
nohash-hasher = "0.2"
once_cell = "1.12"
parking_lot = "0.12"
thiserror.workspace = true

# Optional dependencies:
re_ws_comms = { workspace = true, optional = true, features = ["server"] }
re_viewer = { workspace = true, default-features = false, optional = true }
re_web_viewer_server = { workspace = true, optional = true }

webbrowser = { version = "0.8", optional = true }
once_cell = { version = "1.12", optional = true }
parking_lot = { version = "0.12", optional = true }

# Native dependencies:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
clap = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
tokio = { workspace = true, optional = true, features = [
"macros",
"rt-multi-thread",
] }


[dev-dependencies]
Expand Down
32 changes: 28 additions & 4 deletions crates/re_sdk/src/demo_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn grid(from: glam::Vec3, to: glam::Vec3, n: usize) -> impl Iterator<Item =
/// * `angular_step`: The factor applied between each step along the trigonometric circle.
/// * `angular_offset`: Offsets the starting position on the trigonometric circle.
/// * `z_step`: The factor applied between between each step along the Z axis.
#[cfg(all(feature = "glam", feature = "native_viewer"))]
#[cfg(feature = "glam")]
pub fn color_spiral(
num_points: usize,
radius: f32,
Expand All @@ -91,10 +91,34 @@ pub fn color_spiral(
.collect();

let colors = (0..num_points)
.map(move |i| {
re_viewer::external::re_renderer::colormap_turbo_srgb(i as f32 / num_points as f32)
})
.map(move |i| colormap_turbo_srgb(i as f32 / num_points as f32))
.collect();

(points, colors)
}

/// Returns sRGB polynomial approximation from Turbo color map, assuming `t` is normalized.
fn colormap_turbo_srgb(t: f32) -> [u8; 4] {
#![allow(clippy::excessive_precision)]
use glam::{Vec2, Vec4, Vec4Swizzles};

const R4: Vec4 = Vec4::new(0.13572138, 4.61539260, -42.66032258, 132.13108234);
const G4: Vec4 = Vec4::new(0.09140261, 2.19418839, 4.84296658, -14.18503333);
const B4: Vec4 = Vec4::new(0.10667330, 12.64194608, -60.58204836, 110.36276771);

const R2: Vec2 = Vec2::new(-152.94239396, 59.28637943);
const G2: Vec2 = Vec2::new(4.27729857, 2.82956604);
const B2: Vec2 = Vec2::new(-89.90310912, 27.34824973);

debug_assert!((0.0..=1.0).contains(&t));

let v4 = glam::vec4(1.0, t, t * t, t * t * t);
let v2 = v4.zw() * v4.z;

[
((v4.dot(R4) + v2.dot(R2)) * 255.0) as u8,
((v4.dot(G4) + v2.dot(G2)) * 255.0) as u8,
((v4.dot(B4) + v2.dot(B2)) * 255.0) as u8,
255,
]
}
5 changes: 4 additions & 1 deletion crates/re_sdk/src/file_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Context as _;

use re_log_types::LogMsg;

/// Stream log messages to a file.
pub struct FileWriter {
// None = quit
tx: std::sync::mpsc::Sender<Option<LogMsg>>,
Expand Down Expand Up @@ -50,8 +51,10 @@ impl FileWriter {
join_handle: Some(join_handle),
})
}
}

pub fn write(&self, msg: LogMsg) {
impl crate::LogSink for FileWriter {
fn send(&mut self, msg: LogMsg) {
self.tx.send(Some(msg)).ok();
}
}
Loading

1 comment on commit 51cdb49

@github-actions
Copy link

Choose a reason for hiding this comment

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

Rust Benchmark

Benchmark suite Current: 51cdb49 Previous: bc32a83 Ratio
datastore/insert/batch/rects/insert 556439 ns/iter (± 1821) 560501 ns/iter (± 1947) 0.99
datastore/latest_at/batch/rects/query 1830 ns/iter (± 3) 1810 ns/iter (± 1) 1.01
datastore/latest_at/missing_components/primary 355 ns/iter (± 1) 355 ns/iter (± 0) 1
datastore/latest_at/missing_components/secondaries 424 ns/iter (± 0) 422 ns/iter (± 0) 1.00
datastore/range/batch/rects/query 153688 ns/iter (± 680) 154909 ns/iter (± 799) 0.99
mono_points_arrow/generate_message_bundles 52397319 ns/iter (± 514396) 43914665 ns/iter (± 1185428) 1.19
mono_points_arrow/generate_messages 137290912 ns/iter (± 1260578) 123732269 ns/iter (± 1105156) 1.11
mono_points_arrow/encode_log_msg 170355282 ns/iter (± 1122469) 154123746 ns/iter (± 1649984) 1.11
mono_points_arrow/encode_total 362512127 ns/iter (± 2203933) 325604077 ns/iter (± 1528243) 1.11
mono_points_arrow/decode_log_msg 190468128 ns/iter (± 1511680) 176239714 ns/iter (± 815437) 1.08
mono_points_arrow/decode_message_bundles 77072670 ns/iter (± 1159949) 63084473 ns/iter (± 821400) 1.22
mono_points_arrow/decode_total 261648081 ns/iter (± 1914859) 237427550 ns/iter (± 1361186) 1.10
batch_points_arrow/generate_message_bundles 320346 ns/iter (± 1066) 325667 ns/iter (± 2325) 0.98
batch_points_arrow/generate_messages 6227 ns/iter (± 17) 6248 ns/iter (± 16) 1.00
batch_points_arrow/encode_log_msg 376020 ns/iter (± 1541) 354852 ns/iter (± 1803) 1.06
batch_points_arrow/encode_total 720479 ns/iter (± 3178) 704617 ns/iter (± 1506) 1.02
batch_points_arrow/decode_log_msg 356727 ns/iter (± 1095) 349053 ns/iter (± 554) 1.02
batch_points_arrow/decode_message_bundles 2077 ns/iter (± 5) 2063 ns/iter (± 11) 1.01
batch_points_arrow/decode_total 365148 ns/iter (± 4762) 354118 ns/iter (± 546) 1.03
arrow_mono_points/insert 7400939365 ns/iter (± 51456077) 6006351360 ns/iter (± 17597406) 1.23
arrow_mono_points/query 1847610 ns/iter (± 36001) 1685346 ns/iter (± 8319) 1.10
arrow_batch_points/insert 2839734 ns/iter (± 94461) 2648695 ns/iter (± 52306) 1.07
arrow_batch_points/query 16151 ns/iter (± 56) 16247 ns/iter (± 96) 0.99
arrow_batch_vecs/insert 42382 ns/iter (± 111) 42691 ns/iter (± 70) 0.99
arrow_batch_vecs/query 507275 ns/iter (± 1747) 505671 ns/iter (± 908) 1.00
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

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

Please sign in to comment.