diff --git a/Cargo.lock b/Cargo.lock index dff9253f..2c501357 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3877,6 +3877,7 @@ dependencies = [ "parking_lot", "serde", "serde_json", + "tinymist-std", "tokio", "tokio-util", ] diff --git a/crates/sync-lsp/Cargo.toml b/crates/sync-lsp/Cargo.toml index 8685636b..ae4fcf2f 100644 --- a/crates/sync-lsp/Cargo.toml +++ b/crates/sync-lsp/Cargo.toml @@ -22,6 +22,7 @@ lsp-types = { workspace = true, optional = true } parking_lot.workspace = true serde.workspace = true serde_json.workspace = true +tinymist-std.workspace = true clap = { workspace = true, optional = true } tokio = { workspace = true, features = ["rt"], optional = true } diff --git a/crates/sync-lsp/src/server.rs b/crates/sync-lsp/src/server.rs index f325be63..4ffc9d75 100644 --- a/crates/sync-lsp/src/server.rs +++ b/crates/sync-lsp/src/server.rs @@ -12,12 +12,12 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::pin::Pin; use std::sync::{Arc, Weak}; -use std::time::Instant; use futures::future::MaybeDone; use parking_lot::Mutex; use serde::Serialize; use serde_json::{from_value, Value as JsonValue}; +use tinymist_std::time::Instant; #[cfg(feature = "lsp")] use crate::lsp::{Notification, Request}; diff --git a/crates/sync-lsp/src/server/dap_srv.rs b/crates/sync-lsp/src/server/dap_srv.rs index 86371982..f6d34269 100644 --- a/crates/sync-lsp/src/server/dap_srv.rs +++ b/crates/sync-lsp/src/server/dap_srv.rs @@ -110,7 +110,7 @@ where if is_replay { let client = self.client.clone(); let _ = std::thread::spawn(move || { - let since = std::time::Instant::now(); + let since = tinymist_std::time::Instant::now(); let timeout = std::env::var("REPLAY_TIMEOUT") .ok() .and_then(|s| s.parse().ok()) @@ -122,7 +122,7 @@ where client.begin_panic(); } - tokio::time::sleep(std::time::Duration::from_millis(10)).await; + tokio::time::sleep(tinymist_std::time::Duration::from_millis(10)).await; } }) }) diff --git a/crates/sync-lsp/src/server/lsp_srv.rs b/crates/sync-lsp/src/server/lsp_srv.rs index 51f1a894..4d6f5a54 100644 --- a/crates/sync-lsp/src/server/lsp_srv.rs +++ b/crates/sync-lsp/src/server/lsp_srv.rs @@ -180,7 +180,7 @@ where if is_replay { let client = self.client.clone(); let _ = std::thread::spawn(move || { - let since = std::time::Instant::now(); + let since = tinymist_std::time::Instant::now(); let timeout = std::env::var("REPLAY_TIMEOUT") .ok() .and_then(|s| s.parse().ok()) diff --git a/crates/tinymist-cli/src/main.rs b/crates/tinymist-cli/src/main.rs index 844e58a8..84946ec8 100644 --- a/crates/tinymist-cli/src/main.rs +++ b/crates/tinymist-cli/src/main.rs @@ -462,7 +462,12 @@ impl LsHook for TypstLsHook { } } - fn stop_request(&self, req_id: &RequestId, method: &str, received_at: std::time::Instant) { + fn stop_request( + &self, + req_id: &RequestId, + method: &str, + received_at: tinymist_std::time::Instant, + ) { ().stop_request(req_id, method, received_at); if let Some(scope) = self.0.lock().remove(req_id) { @@ -477,7 +482,7 @@ impl LsHook for TypstLsHook { fn stop_notification( &self, method: &str, - received_at: std::time::Instant, + received_at: tinymist_std::time::Instant, result: LspResult<()>, ) { ().stop_notification(method, received_at, result); diff --git a/crates/tinymist-cli/src/testing.rs b/crates/tinymist-cli/src/testing.rs index 7443b8f3..1811ab03 100644 --- a/crates/tinymist-cli/src/testing.rs +++ b/crates/tinymist-cli/src/testing.rs @@ -180,7 +180,7 @@ pub async fn test_main(args: TestArgs) -> Result<()> { } = start_project(verse, None, move |c, mut i, next| { if let Interrupt::Compiled(artifact) = &mut i { let mut config = ctx.lock(); - let instant = std::time::Instant::now(); + let instant = tinymist_std::time::Instant::now(); // todo: well term support // Clear the screen and then move the cursor to the top left corner. eprintln!("\x1B[2J\x1B[1;1H"); diff --git a/crates/tinymist-project/src/compiler.rs b/crates/tinymist-project/src/compiler.rs index d98fed7c..95b3d776 100644 --- a/crates/tinymist-project/src/compiler.rs +++ b/crates/tinymist-project/src/compiler.rs @@ -825,7 +825,7 @@ impl ProjectInsState { graph: Arc>, export_target: ExportTarget, ) -> impl FnOnce() -> CompiledArtifact { - let start = tinymist_std::time::now(); + let start = tinymist_std::time::Instant::now(); // todo unwrap main id let id = graph.world().main_id().unwrap(); @@ -846,7 +846,7 @@ impl ProjectInsState { let res = CompileStatusResult { diag: (compiled.warning_cnt() + compiled.error_cnt()) as u32, - elapsed: start.elapsed().unwrap_or_default(), + elapsed: start.elapsed(), }; let rep = CompileReport { id: compiled.id().clone(), @@ -898,7 +898,7 @@ impl ProjectInsState { // Trigger an evict task. rayon::spawn(move || { - let evict_start = std::time::Instant::now(); + let evict_start = tinymist_std::time::Instant::now(); if is_primary { comemo::evict(10); diff --git a/crates/tinymist-project/src/watch.rs b/crates/tinymist-project/src/watch.rs index 09ea507b..306027d3 100644 --- a/crates/tinymist-project/src/watch.rs +++ b/crates/tinymist-project/src/watch.rs @@ -460,11 +460,11 @@ impl NotifyActor { // The async scheduler is not accurate, so we need to ensure a window here let reserved = now - event.at_realtime; - if reserved < std::time::Duration::from_millis(50) { + if reserved < tinymist_std::time::Duration::from_millis(50) { let send = self.undetermined_send.clone(); tokio::spawn(async move { // todo: sleep in browser - tokio::time::sleep(std::time::Duration::from_millis(50) - reserved).await; + tokio::time::sleep(tinymist_std::time::Duration::from_millis(50) - reserved).await; log_send_error("reschedule", send.send(event)); }); return None; diff --git a/crates/tinymist-query/src/analysis/global.rs b/crates/tinymist-query/src/analysis/global.rs index c85b2f19..7ef34806 100644 --- a/crates/tinymist-query/src/analysis/global.rs +++ b/crates/tinymist-query/src/analysis/global.rs @@ -1089,7 +1089,7 @@ impl SharedContext { let entry = entry.entry(query).or_default(); QueryStatGuard { bucket: entry.clone(), - since: std::time::SystemTime::now(), + since: tinymist_std::time::Instant::now(), } } diff --git a/crates/tinymist-query/src/analysis/stats.rs b/crates/tinymist-query/src/analysis/stats.rs index 6b913183..eb0f9956 100644 --- a/crates/tinymist-query/src/analysis/stats.rs +++ b/crates/tinymist-query/src/analysis/stats.rs @@ -1,9 +1,10 @@ //! Statistics about the analyzers -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; use parking_lot::Mutex; use tinymist_std::hash::FxDashMap; +use tinymist_std::time::Duration; use typst::syntax::FileId; #[derive(Clone)] @@ -35,12 +36,12 @@ pub(crate) struct QueryStatBucket { pub(crate) struct QueryStatGuard { pub bucket: QueryStatBucket, - pub since: std::time::SystemTime, + pub since: tinymist_std::time::Instant, } impl Drop for QueryStatGuard { fn drop(&mut self) { - let elapsed = self.since.elapsed().unwrap_or_default(); + let elapsed = self.since.elapsed(); let mut data = self.bucket.data.lock(); data.query += 1; data.total += elapsed; diff --git a/crates/tinymist-query/src/analysis/tyck.rs b/crates/tinymist-query/src/analysis/tyck.rs index 2366d6d6..81d04498 100644 --- a/crates/tinymist-query/src/analysis/tyck.rs +++ b/crates/tinymist-query/src/analysis/tyck.rs @@ -54,7 +54,7 @@ pub(crate) fn type_check( module_exports: Default::default(), }; - let type_check_start = std::time::Instant::now(); + let type_check_start = tinymist_std::time::Instant::now(); checker.check(&root); diff --git a/crates/tinymist-query/src/syntax/lexical_hierarchy.rs b/crates/tinymist-query/src/syntax/lexical_hierarchy.rs index 9be05cfd..c9820c78 100644 --- a/crates/tinymist-query/src/syntax/lexical_hierarchy.rs +++ b/crates/tinymist-query/src/syntax/lexical_hierarchy.rs @@ -16,7 +16,7 @@ pub(crate) fn get_lexical_hierarchy( source: &Source, scope_kind: LexicalScopeKind, ) -> Option> { - let start = std::time::Instant::now(); + let start = tinymist_std::time::Instant::now(); let root = LinkedNode::new(source.root()); let mut worker = LexicalHierarchyWorker { diff --git a/crates/tinymist-world/src/font/profile.rs b/crates/tinymist-world/src/font/profile.rs index 57389025..3a13ba11 100644 --- a/crates/tinymist-world/src/font/profile.rs +++ b/crates/tinymist-world/src/font/profile.rs @@ -81,7 +81,7 @@ impl FontProfileItem { pub fn mtime(&self) -> Option { self.meta.get("mtime").and_then(|v| { let v = v.parse::().ok(); - v.map(|v| SystemTime::UNIX_EPOCH + std::time::Duration::from_micros(v)) + v.map(|v| SystemTime::UNIX_EPOCH + tinymist_std::time::Duration::from_micros(v)) }) } diff --git a/crates/tinymist/src/project.rs b/crates/tinymist/src/project.rs index b457121c..68478c33 100644 --- a/crates/tinymist/src/project.rs +++ b/crates/tinymist/src/project.rs @@ -565,7 +565,7 @@ impl CompileHandler for CompileHandlerImpl == 0; if check_stalled { - let since = (tinymist_std::time::Time::now().duration_since(*compiling_since)) + let since = (tinymist_std::time::now().duration_since(*compiling_since)) .unwrap_or_default(); if since.as_secs() > 60 { @@ -630,7 +630,7 @@ impl CompileHandler for CompileHandlerImpl let Some(compile_fn) = s.may_compile(&c.handler) else { continue; }; - s.ext.compiling_since = Some(tinymist_std::time::Time::now()); + s.ext.compiling_since = Some(tinymist_std::time::now()); rayon::spawn(move || { compile_fn(); }); diff --git a/crates/tinymist/src/server.rs b/crates/tinymist/src/server.rs index 2d93c9f3..320ad228 100644 --- a/crates/tinymist/src/server.rs +++ b/crates/tinymist/src/server.rs @@ -345,7 +345,7 @@ impl ServerState { mut state: ServiceState, params: LspInterrupt, ) -> anyhow::Result<()> { - let _start = std::time::Instant::now(); + let _start = tinymist_std::time::Instant::now(); // log::info!("incoming interrupt: {params:?}"); let Some(ready) = state.ready() else { log::info!("interrupted on not ready server"); @@ -362,7 +362,7 @@ impl ServerState { mut state: ServiceState, params: ServerEvent, ) -> anyhow::Result<()> { - let _start = std::time::Instant::now(); + let _start = tinymist_std::time::Instant::now(); // log::info!("incoming interrupt: {params:?}"); let Some(ready) = state.ready() else { log::info!("server event sent to not ready server"); diff --git a/crates/tinymist/src/stats.rs b/crates/tinymist/src/stats.rs index 20ea771b..d90274f9 100644 --- a/crates/tinymist/src/stats.rs +++ b/crates/tinymist/src/stats.rs @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, OnceLock}; -use std::time::Duration; +use tinymist_std::time::Duration; use parking_lot::Mutex; use reflexo::{hash::FxDashMap, path::unix_slash}; @@ -36,13 +36,13 @@ pub struct QueryStatBucket { pub struct QueryStatGuard { pub bucket: QueryStatBucket, - pub since: std::time::SystemTime, - pub snap_since: OnceLock, + pub since: tinymist_std::time::Instant, + pub snap_since: OnceLock, } impl Drop for QueryStatGuard { fn drop(&mut self) { - let elapsed = self.since.elapsed().unwrap_or_default(); + let elapsed = self.since.elapsed(); let mut data = self.bucket.data.lock(); data.query += 1; data.total += elapsed; @@ -54,8 +54,7 @@ impl Drop for QueryStatGuard { impl QueryStatGuard { pub(crate) fn snap(&self) { - self.snap_since - .get_or_init(|| self.since.elapsed().unwrap_or_default()); + self.snap_since.get_or_init(|| self.since.elapsed()); } } @@ -76,7 +75,7 @@ impl CompilerQueryStats { let refs2 = refs.entry(name).or_default(); QueryStatGuard { bucket: refs2.clone(), - since: std::time::SystemTime::now(), + since: tinymist_std::time::Instant::now(), snap_since: OnceLock::new(), } }