#![doc = include_str!("../ABOUT.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",
html_favicon_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg"
)]
#![cfg_attr(not(test), forbid(clippy::unwrap_used))]
#![cfg_attr(not(feature = "profiler"), no_std)]
use core::fmt::{self, Debug};
#[cfg(feature = "profiler")]
use measureme::{EventId, Profiler as MeasuremeProfiler, StringId, TimingGuard};
#[cfg(feature = "profiler")]
use once_cell::sync::OnceCell;
#[cfg(feature = "profiler")]
use rustc_hash::FxHashMap;
#[cfg(feature = "profiler")]
use std::collections::hash_map::Entry;
#[cfg(feature = "profiler")]
use std::sync::RwLock;
#[cfg(feature = "profiler")]
use std::{
path::Path,
thread::{current, ThreadId},
};
#[cfg(feature = "profiler")]
pub struct Profiler {
profiler: MeasuremeProfiler,
string_cache: RwLock<FxHashMap<String, StringId>>,
}
#[cfg(feature = "profiler")]
static mut INSTANCE: OnceCell<Profiler> = OnceCell::new();
#[cfg(feature = "profiler")]
impl Profiler {
pub fn start_event(&self, label: &str, category: &str) -> TimingGuard<'_> {
let kind = self.get_or_alloc_string(category);
let id = EventId::from_label(self.get_or_alloc_string(label));
let thread_id = Self::thread_id_to_u32(current().id());
self.profiler
.start_recording_interval_event(kind, id, thread_id)
}
#[allow(clippy::significant_drop_tightening)]
fn get_or_alloc_string(&self, s: &str) -> StringId {
{
let cache = self
.string_cache
.read()
.expect("Some writer panicked while holding an exclusive lock.");
if let Some(id) = cache.get(s) {
return *id;
}
}
let mut cache = self
.string_cache
.write()
.expect("Some writer panicked while holding an exclusive lock.");
let entry = cache.entry(s.into());
match entry {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let id = self.profiler.alloc_string(s);
*entry.insert(id)
}
}
}
fn default() -> Self {
let profiler =
MeasuremeProfiler::new(Path::new("./my_trace")).expect("must be able to create file");
Self {
profiler,
string_cache: RwLock::new(FxHashMap::default()),
}
}
#[must_use]
pub fn global() -> &'static Self {
unsafe { INSTANCE.get_or_init(Self::default) }
}
pub fn drop(&self) {
unsafe {
INSTANCE
.take()
.expect("Could not take back profiler instance");
}
}
#[allow(clippy::cast_possible_truncation)]
fn thread_id_to_u32(tid: ThreadId) -> u32 {
unsafe { std::mem::transmute::<ThreadId, u64>(tid) as u32 }
}
}
impl Debug for Profiler {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Debug::fmt("no debug implemented", f)
}
}
#[cfg(not(feature = "profiler"))]
#[derive(Copy, Clone)]
pub struct Profiler;
#[cfg(not(feature = "profiler"))]
impl Profiler {
#[allow(clippy::unused_unit)]
pub const fn start_event(&self, _label: &str, _category: &str) -> () {}
pub const fn drop(&self) {}
#[must_use]
pub const fn global() -> Self {
Self
}
}