From be7cfc85d08c545abfac08098b7b33b4bd71f37e Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 27 Oct 2024 19:04:55 +0100 Subject: Split out four new crates (#5302) --- crates/typst-timing/src/lib.rs | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) (limited to 'crates/typst-timing/src/lib.rs') diff --git a/crates/typst-timing/src/lib.rs b/crates/typst-timing/src/lib.rs index 4e711d5f..0df65443 100644 --- a/crates/typst-timing/src/lib.rs +++ b/crates/typst-timing/src/lib.rs @@ -2,14 +2,15 @@ use std::hash::Hash; use std::io::Write; -use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; +use std::num::NonZeroU64; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering::Relaxed; use std::thread::ThreadId; use std::time::{Duration, SystemTime}; use parking_lot::Mutex; use serde::ser::SerializeSeq; use serde::{Serialize, Serializer}; -use typst_syntax::Span; /// Whether the timer is enabled. Defaults to `false`. static ENABLED: AtomicBool = AtomicBool::new(false); @@ -43,8 +44,8 @@ struct Event { id: u64, /// The name of this event. name: &'static str, - /// The span of code that this event was recorded in. - span: Option, + /// The raw value of the span of code that this event was recorded in. + span: Option, /// The thread ID of this event. thread_id: ThreadId, } @@ -79,18 +80,34 @@ pub fn clear() { /// A scope that records an event when it is dropped. pub struct TimingScope { name: &'static str, - span: Option, + span: Option, id: u64, thread_id: ThreadId, } impl TimingScope { /// Create a new scope if timing is enabled. - pub fn new(name: &'static str, span: Option) -> Option { - if !is_enabled() { - return None; + #[inline] + pub fn new(name: &'static str) -> Option { + Self::with_span(name, None) + } + + /// Create a new scope with a span if timing is enabled. + /// + /// The span is a raw number because `typst-timing` can't depend on + /// `typst-syntax` (or else `typst-syntax` couldn't depend on + /// `typst-timing`). + #[inline] + pub fn with_span(name: &'static str, span: Option) -> Option { + #[cfg(not(target_arch = "wasm32"))] + if is_enabled() { + return Some(Self::new_impl(name, span)); } + None + } + /// Create a new scope without checking if timing is enabled. + fn new_impl(name: &'static str, span: Option) -> Self { let timestamp = SystemTime::now(); let thread_id = std::thread::current().id(); @@ -106,7 +123,7 @@ impl TimingScope { thread_id, }); - Some(TimingScope { name, span, id, thread_id }) + Self { name, span, id, thread_id } } } @@ -151,11 +168,11 @@ impl Drop for TimingScope { #[macro_export] macro_rules! timed { ($name:expr, span = $span:expr, $body:expr $(,)?) => {{ - let __scope = $crate::TimingScope::new($name, Some($span)); + let __scope = $crate::TimingScope::with_span($name, Some($span)); $body }}; ($name:expr, $body:expr $(,)?) => {{ - let __scope = $crate::TimingScope::new($name, None); + let __scope = $crate::TimingScope::new($name); $body }}; } @@ -167,7 +184,7 @@ macro_rules! timed { /// the second element is the line number. pub fn export_json( writer: W, - mut source: impl FnMut(Span) -> (String, u32), + mut source: impl FnMut(NonZeroU64) -> (String, u32), ) -> Result<(), String> { #[derive(Serialize)] struct Entry { -- cgit v1.2.3