Skip to content

Commit

Permalink
Load .rrd files in background threads
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Aug 28, 2023
1 parent 0b7e52c commit 8e1ca38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
4 changes: 2 additions & 2 deletions crates/re_data_source/src/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ impl DataSource {
}

DataSource::FileContents(file_contents) => {
let name = &file_contents.name;
let name = file_contents.name.clone();
let (tx, rx) = re_smart_channel::smart_channel(
re_smart_channel::SmartMessageSource::File(name.clone().into()),
re_smart_channel::SmartChannelSource::File {
path: name.clone().into(),
},
);
let store_id = re_log_types::StoreId::random(re_log_types::StoreKind::Recording);
crate::load_file_contents::load_file_contents(store_id, &file_contents, tx)
crate::load_file_contents::load_file_contents(store_id, file_contents, tx)
.with_context(|| format!("{name:?}"))?;
Ok(rx)
}
Expand Down
35 changes: 24 additions & 11 deletions crates/re_data_source/src/load_file_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,37 @@ use crate::FileContents;
#[allow(clippy::needless_pass_by_value)] // false positive on some feature flags
pub fn load_file_contents(
_store_id: re_log_types::StoreId,
file_contents: &FileContents,
file_contents: FileContents,
tx: Sender<LogMsg>,
) -> anyhow::Result<()> {
let file_name = &file_contents.name;
re_tracing::profile_function!(file_name);
let file_name = file_contents.name.clone();
re_tracing::profile_function!(file_name.as_str());
re_log::info!("Loading {file_name:?}…");

if file_name.ends_with(".rrd") {
// TODO: background thread on native
let bytes: &[u8] = &file_contents.bytes;
let decoder = re_log_encoding::decoder::Decoder::new(bytes)?;
for msg in decoder {
tx.send(msg?)?;
if cfg!(target_arch = "wasm32") {
load_rrd_sync(&file_contents, &tx)
} else {
// Load in background thread on native:
rayon::spawn(move || {
if let Err(err) = load_rrd_sync(&file_contents, &tx) {
re_log::error!("Failed to load {file_name:?}: {err}");
}
});
Ok(())
}
re_log::debug!("Finished loading {file_name:?}.");
Ok(())
} else {
// TODO: support images and meshes
// TODO(emilk): support loading images and meshes from file contents
anyhow::bail!("Unsupported file extension for {file_name:?}.");
}
}

fn load_rrd_sync(file_contents: &FileContents, tx: &Sender<LogMsg>) -> Result<(), anyhow::Error> {
let bytes: &[u8] = &file_contents.bytes;
let decoder = re_log_encoding::decoder::Decoder::new(bytes)?;
for msg in decoder {
tx.send(msg?)?;
}
re_log::debug!("Finished loading {:?}.", file_contents.name);
Ok(())
}
4 changes: 2 additions & 2 deletions crates/re_data_source/src/web_sockets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn connect_to_ws_url(
},
);

re_log::info!("Connecting to WS server at {:?}…", url);
re_log::info!("Connecting to WebSocket server at {url:?}…");

let callback = move |binary: Vec<u8>| match re_ws_comms::decode_log_msg(&binary) {
Ok(log_msg) => {
Expand All @@ -36,6 +36,6 @@ pub fn connect_to_ws_url(
};

let connection = re_ws_comms::Connection::viewer_to_server(url.to_owned(), callback)?;
std::mem::drop(connection); // Never close the connection. TODO: is this wise?
std::mem::drop(connection); // Never close the connection. TODO(emilk): is this wise?
Ok(rx)
}

0 comments on commit 8e1ca38

Please sign in to comment.