Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - [profiler] Cache StringId #2495

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion boa_profiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ repository.workspace = true
rust-version.workspace = true

[features]
profiler = ["measureme", "once_cell"]
profiler = ["measureme", "once_cell", "rustc-hash"]

[dependencies]
measureme = { version = "10.1.0", optional = true }
once_cell = { version = "1.16.0", optional = true }
rustc-hash = { version = "1.1.0", optional = true }
43 changes: 39 additions & 4 deletions boa_profiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@
use std::fmt::{self, Debug};

#[cfg(feature = "profiler")]
use measureme::{EventId, Profiler as MeasuremeProfiler, TimingGuard};
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},
Expand All @@ -93,6 +99,7 @@ use std::{
#[cfg(feature = "profiler")]
pub struct Profiler {
profiler: MeasuremeProfiler,
string_cache: RwLock<FxHashMap<String, StringId>>,
}

/// This static instance must never be public, and its only access must be done through the
Expand All @@ -105,17 +112,45 @@ static mut INSTANCE: OnceCell<Profiler> = OnceCell::new();
impl Profiler {
/// Start a new profiled event.
pub fn start_event(&self, label: &str, category: &str) -> TimingGuard<'_> {
let kind = self.profiler.alloc_string(category);
let id = EventId::from_label(self.profiler.alloc_string(label));
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)
}

fn get_or_alloc_string(&self, s: &str) -> StringId {
{
// Check the cache only with the read lock first.
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 }
Self {
profiler,
string_cache: RwLock::new(FxHashMap::default()),
}
}

/// Return the global instance of the profiler.
Expand Down