Skip to content

Commit 215b377

Browse files
committedApr 22, 2022
extend EventArgRecorder into span-aware SpannedEventArgRecorder
The self-profiler's `EventArgRecorder` is general-purpose in its ability to record Strings (and `rustc_span` depends on the crate its defined in, `rustc_data_structure`). Some generic activities could use recording locations where they happen in the user's code: to allow e.g. to track macro expansions and diagnose performance issues there. This adds a `SpannedEventArgRecorder` that can record an argument given as a span, rather than a String, since turning spans into Strings can be tricky if you're not happy with its default Debug output. This way the recorder can have a `record_arg_spanned` method which will do that.
1 parent a8272f2 commit 215b377

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎compiler/rustc_span/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ pub use symbol::{sym, Symbol};
5959
mod analyze_source_file;
6060
pub mod fatal_error;
6161

62+
pub mod profiling;
63+
6264
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
6365
use rustc_data_structures::sync::{Lock, Lrc};
6466

‎compiler/rustc_span/src/profiling.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::borrow::Borrow;
2+
3+
use rustc_data_structures::profiling::EventArgRecorder;
4+
5+
/// Extension trait for self-profiling purposes: allows to record spans within a generic activity's
6+
/// event arguments.
7+
pub trait SpannedEventArgRecorder {
8+
/// Records the following event arguments within the current generic activity being profiled:
9+
/// - the provided `event_arg`
10+
/// - a string representation of the provided `span`
11+
///
12+
/// Note: when self-profiling with costly event arguments, at least one argument
13+
/// needs to be recorded. A panic will be triggered if that doesn't happen.
14+
fn record_arg_with_span<A>(&mut self, event_arg: A, span: crate::Span)
15+
where
16+
A: Borrow<str> + Into<String>;
17+
}
18+
19+
impl SpannedEventArgRecorder for EventArgRecorder<'_> {
20+
fn record_arg_with_span<A>(&mut self, event_arg: A, span: crate::Span)
21+
where
22+
A: Borrow<str> + Into<String>,
23+
{
24+
self.record_arg(event_arg);
25+
26+
let span_arg = crate::with_session_globals(|session_globals| {
27+
if let Some(source_map) = &*session_globals.source_map.borrow() {
28+
source_map.span_to_embeddable_string(span)
29+
} else {
30+
format!("{:?}", span)
31+
}
32+
});
33+
self.record_arg(span_arg);
34+
}
35+
}

0 commit comments

Comments
 (0)
Please sign in to comment.