Skip to content

Commit

Permalink
Clean function and shorten file names only in macros (#182)
Browse files Browse the repository at this point in the history
### Checklist

* [x] I have read the [Contributor Guide](../CONTRIBUTING.md)
* [x] I have read and agree to the [Code of
Conduct](../CODE_OF_CONDUCT.md)
* [x] I have added a description of my changes and why I'd like them
included in the section below

### Description of Changes

Clean the function names and shorten the file paths from the
`::{{closure}}::{{closure}}::f` suffix directly in the
`profile_function!` and `profile_scope!` macros when registering the
scope (in the `OnceLock::get_or_init` closure).

Motivation: Avoid making assumptions about the format of the function
names and file paths, for those who are using puffin directly as a
general profiling tool (not going through `profile_function!` and
`profile_scope!` macros).
  • Loading branch information
fornwall authored Jan 17, 2024
1 parent e076333 commit ae97072
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 25 deletions.
7 changes: 2 additions & 5 deletions puffin/src/global_profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,8 @@ impl GlobalProfiler {
) {
if !scope_details.is_empty() {
// Here we can run slightly heavy logic as its only ran once for each scope.
self.new_scopes.extend(
scope_details
.iter()
.map(|x| Arc::new(x.clone().into_readable().clone())),
);
self.new_scopes
.extend(scope_details.iter().map(|x| Arc::new(x.clone())));
}

self.current_frame
Expand Down
8 changes: 4 additions & 4 deletions puffin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ macro_rules! profile_function {
let scope_id = SCOPE_ID.get_or_init(|| {
$crate::ThreadProfiler::call(|tp| {
let id = tp.register_function_scope(
$crate::current_function_name!(),
file!(),
$crate::clean_function_name($crate::current_function_name!()),
$crate::short_file_name(file!()),
line!(),
);
id
Expand Down Expand Up @@ -254,8 +254,8 @@ macro_rules! profile_scope {
$crate::ThreadProfiler::call(|tp| {
let id = tp.register_named_scope(
$name,
$crate::current_function_name!(),
file!(),
$crate::clean_function_name($crate::current_function_name!()),
$crate::short_file_name(file!()),
line!(),
);
id
Expand Down
17 changes: 1 addition & 16 deletions puffin/src/scope_details.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{clean_function_name, short_file_name, ScopeId};
use crate::ScopeId;
use std::{borrow::Cow, collections::HashMap, sync::Arc};

#[derive(Default, Clone)]
Expand Down Expand Up @@ -186,21 +186,6 @@ impl ScopeDetails {
}
}

/// Turns the scope details into a more readable version:
///
/// * Consistent / shortened file path across platforms
/// * Consistent / shortened function name
#[inline]
pub(crate) fn into_readable(self) -> Self {
Self {
scope_id: self.scope_id,
scope_name: self.scope_name,
function_name: clean_function_name(&self.function_name).into(),
file_path: short_file_name(&self.file_path).into(),
line_nr: self.line_nr,
}
}

// This function should not be exposed as only puffin should allocate ids.
#[inline]
pub(crate) fn with_scope_id(mut self, scope_id: ScopeId) -> Self {
Expand Down

0 comments on commit ae97072

Please sign in to comment.