Skip to content

Commit dc04432

Browse files
committed
Add static category types.
1 parent 7a8d5ee commit dc04432

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

fxprof-processed-profile/src/category.rs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,36 @@ use std::hash::Hash;
33
use indexmap::Equivalent;
44
use serde::ser::{Serialize, SerializeMap, Serializer};
55

6+
use crate::Profile;
7+
68
use super::category_color::CategoryColor;
79
use super::fast_hash_map::FastIndexSet;
810

11+
/// Implemented by [`Category`], [`Subcategory`], [`CategoryHandle`] and [`SubcategoryHandle`].
12+
pub trait IntoSubcategoryHandle {
13+
/// Returns the corresponding [`SubcategoryHandle`].
14+
fn into_subcategory_handle(self, profile: &mut Profile) -> SubcategoryHandle;
15+
}
16+
17+
/// A profiling category. Has a name and a color.
18+
///
19+
/// Used to categorize stack frames and markers in the front-end. The category's
20+
/// color is used in the activity graph and in the call tree, and in a few other places.
921
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
1022
pub struct Category<'a>(pub &'a str, pub CategoryColor);
1123

12-
/// A profiling category, obtained from [`Profile::handle_for_category`](crate::Profile::handle_for_category).
24+
impl IntoSubcategoryHandle for Category<'_> {
25+
fn into_subcategory_handle(self, profile: &mut Profile) -> SubcategoryHandle {
26+
let category_handle = profile.handle_for_category(self);
27+
category_handle.into()
28+
}
29+
}
30+
31+
/// The handle for a [`Category`], obtained from [`Profile::handle_for_category`](crate::Profile::handle_for_category).
32+
///
33+
/// Storing and reusing the handle avoids repeated lookups and can improve performance.
1334
///
14-
/// Used to categorize stack frames and markers in the front-end. Every category has a color;
15-
/// the color is used in the activity graph and in the call tree, and in a few other places.
35+
/// The handle is specific to a [`Profile`] instance and cannot be reused across profiles.
1636
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
1737
pub struct CategoryHandle(pub(crate) u16);
1838

@@ -21,12 +41,33 @@ impl CategoryHandle {
2141
pub const OTHER: Self = CategoryHandle(0);
2242
}
2343

44+
impl IntoSubcategoryHandle for CategoryHandle {
45+
fn into_subcategory_handle(self, _profile: &mut Profile) -> SubcategoryHandle {
46+
self.into()
47+
}
48+
}
49+
2450
impl Serialize for CategoryHandle {
2551
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2652
self.0.serialize(serializer)
2753
}
2854
}
2955

56+
/// A named subcategory of a [`Category`], for fine-grained annotation of stack frames.
57+
///
58+
/// If you don't need named subcategories, you can just pass a [`Category`] or a
59+
/// [`CategoryHandle`] in any place where an [`IntoSubcategoryHandle`] is expected;
60+
/// this will give you the category's default subcategory.
61+
pub struct Subcategory<'a>(pub Category<'a>, pub &'a str);
62+
63+
impl IntoSubcategoryHandle for Subcategory<'_> {
64+
fn into_subcategory_handle(self, profile: &mut Profile) -> SubcategoryHandle {
65+
let Subcategory(category, subcategory_name) = self;
66+
let category_handle = profile.handle_for_category(category);
67+
profile.handle_for_subcategory(category_handle, subcategory_name)
68+
}
69+
}
70+
3071
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
3172
pub struct SubcategoryIndex(pub u16);
3273

@@ -35,16 +76,27 @@ impl SubcategoryIndex {
3576
pub const OTHER: Self = SubcategoryIndex(0);
3677
}
3778

38-
/// A subcategory of a [`CategoryHandle`], used to annotate stack frames.
79+
/// A handle for a [`Subcategory`], or for the default subcategory of a [`CategoryHandle`].
80+
///
81+
/// Used to annotate stack frames.
3982
///
4083
/// Every [`CategoryHandle`] can be turned into a [`SubcategoryHandle`] by calling `.into()` -
4184
/// this will give you the default subcategory of that category.
4285
///
4386
/// Subcategory handles for named subcategories can be obtained from
4487
/// [`Profile::handle_for_subcategory`](crate::Profile::handle_for_subcategory).
88+
/// Storing and reusing the handle avoids repeated lookups and can improve performance.
89+
///
90+
/// The handle is specific to a Profile instance and cannot be reused across profiles.
4591
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
4692
pub struct SubcategoryHandle(pub(crate) CategoryHandle, pub(crate) SubcategoryIndex);
4793

94+
impl IntoSubcategoryHandle for SubcategoryHandle {
95+
fn into_subcategory_handle(self, _profile: &mut Profile) -> SubcategoryHandle {
96+
self
97+
}
98+
}
99+
48100
impl From<CategoryHandle> for SubcategoryHandle {
49101
fn from(category: CategoryHandle) -> Self {
50102
SubcategoryHandle(category, SubcategoryIndex::OTHER)

fxprof-processed-profile/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ mod thread;
5959
mod thread_string_table;
6060
mod timestamp;
6161

62-
pub use category::{Category, CategoryHandle, SubcategoryHandle};
62+
pub use category::{
63+
Category, CategoryHandle, IntoSubcategoryHandle, Subcategory, SubcategoryHandle,
64+
};
6365
pub use category_color::CategoryColor;
6466
pub use counters::CounterHandle;
6567
pub use cpu_delta::CpuDelta;

0 commit comments

Comments
 (0)