diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-10-27 19:04:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-27 18:04:55 +0000 |
| commit | be7cfc85d08c545abfac08098b7b33b4bd71f37e (patch) | |
| tree | f4137fa2aaa57babae1f7603a9b2ed7e688f43d8 /crates/typst-timing | |
| parent | b8034a343831e8609aec2ec81eb7eeda57aa5d81 (diff) | |
Split out four new crates (#5302)
Diffstat (limited to 'crates/typst-timing')
| -rw-r--r-- | crates/typst-timing/Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/typst-timing/src/lib.rs | 41 |
2 files changed, 29 insertions, 13 deletions
diff --git a/crates/typst-timing/Cargo.toml b/crates/typst-timing/Cargo.toml index 525d65c5..2d42269f 100644 --- a/crates/typst-timing/Cargo.toml +++ b/crates/typst-timing/Cargo.toml @@ -13,7 +13,6 @@ keywords = { workspace = true } readme = { workspace = true } [dependencies] -typst-syntax = { workspace = true } parking_lot = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } 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<Span>, + /// The raw value of the span of code that this event was recorded in. + span: Option<NonZeroU64>, /// 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>, + span: Option<NonZeroU64>, id: u64, thread_id: ThreadId, } impl TimingScope { /// Create a new scope if timing is enabled. - pub fn new(name: &'static str, span: Option<Span>) -> Option<Self> { - if !is_enabled() { - return None; + #[inline] + pub fn new(name: &'static str) -> Option<Self> { + 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<NonZeroU64>) -> Option<Self> { + #[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<NonZeroU64>) -> 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<W: Write>( writer: W, - mut source: impl FnMut(Span) -> (String, u32), + mut source: impl FnMut(NonZeroU64) -> (String, u32), ) -> Result<(), String> { #[derive(Serialize)] struct Entry { |
