Skip to content

Commit

Permalink
Move from instant -> web_time (#2093)
Browse files Browse the repository at this point in the history
* Move from instant -> web_time

* Prevent instant::SystemTime in clippy

* Cleanup

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
jleibs and emilk authored May 17, 2023
1 parent dee940e commit 5c652d2
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 49 deletions.
25 changes: 18 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ time = { version = "0.3", default-features = false, features = [
] }
tinyvec = { version = "1.6", features = ["alloc", "rustc_1_55"] }
tokio = { version = "1.24", default-features = false }
web-time = "0.2.0"
wgpu = { version = "0.16" }
wgpu-core = { version = "0.16" }

Expand Down
4 changes: 2 additions & 2 deletions crates/re_log_encoding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ re_smart_channel.workspace = true
ehttp = "0.2"
parking_lot.workspace = true
thiserror.workspace = true
web-time.workspace = true

# Optional external dependencies:
rmp-serde = { version = "1", optional = true }
Expand All @@ -49,9 +50,8 @@ zstd = { version = "0.11.0", optional = true } # native only

# Web dependencies:
[target.'cfg(target_arch = "wasm32")'.dependencies]
instant = { version = "0.1", features = ["wasm-bindgen"] }
js-sys = "0.3"
ruzstd = { version = "0.3.0", optional = true } # works on wasm, in contrast to zstd
ruzstd = { version = "0.3.0", optional = true } # works on wasm, in contrast to zstd
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3.52", features = ["Window"] }
Expand Down
6 changes: 3 additions & 3 deletions crates/re_log_encoding/src/stream_rrd_from_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ mod web_decode {
///
/// This is cooperative multi-tasking.
async fn decode_rrd_async(rrd_bytes: Vec<u8>, on_msg: Arc<dyn Fn(LogMsg) + Send>) {
let mut last_yield = instant::Instant::now();
let mut last_yield = web_time::Instant::now();

match crate::decoder::Decoder::new(rrd_bytes.as_slice()) {
Ok(decoder) => {
Expand All @@ -135,10 +135,10 @@ mod web_decode {
}
}

if last_yield.elapsed() > instant::Duration::from_millis(10) {
if last_yield.elapsed() > web_time::Duration::from_millis(10) {
// yield to the ui task
yield_().await;
last_yield = instant::Instant::now();
last_yield = web_time::Instant::now();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/re_log_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ document-features = "0.2"
fixed = { version = "1.17", default-features = false, features = ["serde"] }
half = { workspace = true, features = ["bytemuck"] }
itertools = { workspace = true }
instant = { version = "0.1" }
web-time.workspace = true
lazy_static.workspace = true
ndarray.workspace = true
nohash-hasher = "0.2"
Expand Down
22 changes: 6 additions & 16 deletions crates/re_log_types/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,10 @@ pub struct Time(i64);
impl Time {
#[inline]
pub fn now() -> Self {
// TODO(https://github.com/rerun-io/rerun/issues/2105): Even though `instant` should work on wasm,
// `elapsed` is broken. See: https://github.com/sebcrozet/instant/issues/49
//
// For now, we implement what elapsed is supposed to do ourselves.
/*
let nanos_since_epoch = instant::SystemTime::UNIX_EPOCH
let nanos_since_epoch = web_time::SystemTime::UNIX_EPOCH
.elapsed()
.expect("Expected system clock to be set to after 1970")
.as_nanos() as _;
*/
let nanos_since_epoch = instant::SystemTime::now()
.duration_since(instant::SystemTime::UNIX_EPOCH)
.expect("Expected system clock to be set to after 1970")
.as_nanos() as _;
Self(nanos_since_epoch)
}

Expand Down Expand Up @@ -165,14 +155,14 @@ impl TryFrom<std::time::SystemTime> for Time {
}
}

// On non-wasm32 builds, `instant::SystemTime` is a re-export of `std::time::SystemTime`,
// On non-wasm32 builds, `web_time::SystemTime` is a re-export of `std::time::SystemTime`,
// so it's covered by the above `TryFrom`.
#[cfg(target_arch = "wasm32")]
impl TryFrom<instant::SystemTime> for Time {
type Error = ();
impl TryFrom<web_time::SystemTime> for Time {
type Error = web_time::SystemTimeError;

fn try_from(time: instant::SystemTime) -> Result<Time, Self::Error> {
time.duration_since(instant::SystemTime::UNIX_EPOCH)
fn try_from(time: web_time::SystemTime) -> Result<Time, Self::Error> {
time.duration_since(web_time::SystemTime::UNIX_EPOCH)
.map(|duration_since_epoch| Time(duration_since_epoch.as_nanos() as _))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/re_memory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ re_log.workspace = true

ahash.workspace = true
emath.workspace = true
instant = { version = "0.1", features = ["wasm-bindgen"] }
itertools = { workspace = true }
nohash-hasher = "0.2"
once_cell = "1.16"
parking_lot.workspace = true
smallvec.workspace = true
web-time.workspace = true

# native dependencies:
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion crates/re_memory/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// Returns monotonically increasing time in seconds.
#[inline]
pub fn sec_since_start() -> f64 {
use instant::Instant;
use once_cell::sync::Lazy;
use web_time::Instant;

static START_INSTANT: Lazy<Instant> = Lazy::new(Instant::now);
START_INSTANT.elapsed().as_nanos() as f64 / 1e9
Expand Down
2 changes: 1 addition & 1 deletion crates/re_renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ wgpu-core.workspace = true
# For examples:
[dev-dependencies]
image = { workspace = true, default-features = false, features = ["png"] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
log = "0.4"
pollster = "0.3"
rand = "0.8"
web-time.workspace = true
winit = "0.28.1"
zip = { version = "0.6", default-features = false, features = ["deflate"] }

Expand Down
6 changes: 3 additions & 3 deletions crates/re_renderer/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::sync::Arc;

use anyhow::Context as _;
use instant::Instant;
use web_time::Instant;

use re_renderer::{
config::{supported_backends, HardwareTier, RenderContextConfig},
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn split_resolution(
pub struct Time {
start_time: Instant,
last_draw_time: Instant,
pub last_frame_duration: instant::Duration,
pub last_frame_duration: web_time::Duration,
}

impl Time {
Expand Down Expand Up @@ -179,7 +179,7 @@ impl<E: Example + 'static> Application<E> {
time: Time {
start_time: Instant::now(),
last_draw_time: Instant::now(),
last_frame_duration: instant::Duration::from_secs(0),
last_frame_duration: web_time::Duration::from_secs(0),
},

example,
Expand Down
2 changes: 1 addition & 1 deletion crates/re_smart_channel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ all-features = true

[dependencies]
crossbeam.workspace = true
instant = { version = "0.1", features = ["wasm-bindgen"] }
web-time.workspace = true
2 changes: 1 addition & 1 deletion crates/re_smart_channel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::{
Arc,
};

use instant::Instant;
use web_time::Instant;

pub use crossbeam::channel::{RecvError, RecvTimeoutError, SendError, TryRecvError};

Expand Down
2 changes: 1 addition & 1 deletion crates/re_tuid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ serde = ["dep:serde"]
[dependencies]
document-features = "0.2"
getrandom = "0.2"
instant = "0.1"
once_cell = "1.16"
web-time.workspace = true

# Optional dependencies:
arrow2 = { workspace = true, optional = true } # used by arrow2_convert
Expand Down
4 changes: 2 additions & 2 deletions crates/re_tuid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ impl Tuid {
#[inline]
fn monotonic_nanos_since_epoch() -> u64 {
// This can maybe be optimized
use instant::Instant;
use once_cell::sync::Lazy;
use web_time::Instant;

static START_TIME: Lazy<(u64, Instant)> = Lazy::new(|| (nanos_since_epoch(), Instant::now()));
START_TIME.0 + START_TIME.1.elapsed().as_nanos() as u64
}

fn nanos_since_epoch() -> u64 {
if let Ok(duration_since_epoch) = instant::SystemTime::UNIX_EPOCH.elapsed() {
if let Ok(duration_since_epoch) = web_time::SystemTime::UNIX_EPOCH.elapsed() {
let mut nanos_since_epoch = duration_since_epoch.as_nanos() as u64;

if cfg!(target_arch = "wasm32") {
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ image = { workspace = true, default-features = false, features = [
"jpeg",
"png",
] }
instant = { version = "0.1", features = ["wasm-bindgen"] }
itertools = { workspace = true }
lazy_static.workspace = true
macaw = { workspace = true, features = ["with_serde"] }
Expand All @@ -103,6 +102,7 @@ smallvec = { workspace = true, features = ["serde"] }
thiserror.workspace = true
time = { workspace = true, features = ["formatting"] }
vec1 = "1.8"
web-time.workspace = true
wgpu.workspace = true

# native dependencies:
Expand Down
12 changes: 6 additions & 6 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::{any::Any, hash::Hash};
use ahash::HashMap;
use anyhow::Context;
use egui::NumExt as _;
use instant::Instant;
use itertools::Itertools as _;
use poll_promise::Promise;
use web_time::Instant;

use re_arrow_store::{DataStoreConfig, DataStoreStats};
use re_data_store::log_db::LogDb;
Expand Down Expand Up @@ -84,7 +84,7 @@ pub struct App {
memory_panel: crate::memory_panel::MemoryPanel,
memory_panel_open: bool,

latest_queue_interest: instant::Instant,
latest_queue_interest: web_time::Instant,

/// Measures how long a frame takes to paint
frame_time_history: egui::util::History<f32>,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl App {
memory_panel: Default::default(),
memory_panel_open: false,

latest_queue_interest: instant::Instant::now(), // TODO(emilk): `Instant::MIN` when we have our own `Instant` that supports it.
latest_queue_interest: web_time::Instant::now(), // TODO(emilk): `Instant::MIN` when we have our own `Instant` that supports it.

frame_time_history: egui::util::History::new(1..100, 0.5),

Expand Down Expand Up @@ -719,7 +719,7 @@ impl App {
fn receive_messages(&mut self, egui_ctx: &egui::Context) {
crate::profile_function!();

let start = instant::Instant::now();
let start = web_time::Instant::now();

while let Ok(msg) = self.rx.try_recv() {
let recording_id = msg.recording_id();
Expand Down Expand Up @@ -752,7 +752,7 @@ impl App {
self.analytics.on_open_recording(log_db);
}

if start.elapsed() > instant::Duration::from_millis(10) {
if start.elapsed() > web_time::Duration::from_millis(10) {
egui_ctx.request_repaint(); // make sure we keep receiving messages asap
break; // don't block the main thread for too long
}
Expand Down Expand Up @@ -1492,7 +1492,7 @@ fn input_latency_label_ui(ui: &mut egui::Ui, app: &mut App) {
&& (!is_latency_interesting || app.state.app_options.warn_latency < latency_sec)
{
// we use this to avoid flicker
app.latest_queue_interest = instant::Instant::now();
app.latest_queue_interest = web_time::Instant::now();
}

if app.latest_queue_interest.elapsed().as_secs_f32() < 1.0 {
Expand Down
Loading

0 comments on commit 5c652d2

Please sign in to comment.