1 use measureme::{StringComponent, StringId};
2 use rustc_data_structures::fx::FxHashMap;
3 use rustc_data_structures::profiling::SelfProfiler;
4 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, LOCAL_CRATE};
5 use rustc_hir::definitions::DefPathData;
6 use rustc_middle::ty::{TyCtxt, WithOptConstParam};
7 use rustc_query_system::query::QueryCache;
11 struct QueryKeyStringCache {
12 def_id_cache: FxHashMap<DefId, StringId>,
15 impl QueryKeyStringCache {
16 fn new() -> QueryKeyStringCache {
17 QueryKeyStringCache { def_id_cache: Default::default() }
21 struct QueryKeyStringBuilder<'p, 'c, 'tcx> {
22 profiler: &'p SelfProfiler,
24 string_cache: &'c mut QueryKeyStringCache,
27 impl<'p, 'c, 'tcx> QueryKeyStringBuilder<'p, 'c, 'tcx> {
29 profiler: &'p SelfProfiler,
31 string_cache: &'c mut QueryKeyStringCache,
32 ) -> QueryKeyStringBuilder<'p, 'c, 'tcx> {
33 QueryKeyStringBuilder { profiler, tcx, string_cache }
36 // The current implementation is rather crude. In the future it might be a
37 // good idea to base this on `ty::print` in order to get nicer and more
38 // efficient query keys.
39 fn def_id_to_string_id(&mut self, def_id: DefId) -> StringId {
40 if let Some(&string_id) = self.string_cache.def_id_cache.get(&def_id) {
44 let def_key = self.tcx.def_key(def_id);
46 let (parent_string_id, start_index) = match def_key.parent {
47 Some(parent_index) => {
48 let parent_def_id = DefId { index: parent_index, krate: def_id.krate };
50 (self.def_id_to_string_id(parent_def_id), 0)
52 None => (StringId::INVALID, 2),
55 let dis_buffer = &mut [0u8; 16];
62 match def_key.disambiguated_data.data {
63 DefPathData::CrateRoot => {
64 crate_name = self.tcx.crate_name(def_id.krate);
65 name = crate_name.as_str();
70 other_name = other.to_string();
71 name = other_name.as_str();
72 if def_key.disambiguated_data.disambiguator == 0 {
76 write!(&mut dis_buffer[..], "[{}]", def_key.disambiguated_data.disambiguator)
78 let end_of_dis = dis_buffer.iter().position(|&c| c == b']').unwrap();
79 dis = std::str::from_utf8(&dis_buffer[..end_of_dis + 1]).unwrap();
86 StringComponent::Ref(parent_string_id),
87 StringComponent::Value("::"),
88 StringComponent::Value(name),
89 StringComponent::Value(dis),
92 let string_id = self.profiler.alloc_string(&components[start_index..end_index]);
94 self.string_cache.def_id_cache.insert(def_id, string_id);
100 trait IntoSelfProfilingString {
101 fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>) -> StringId;
104 // The default implementation of `IntoSelfProfilingString` just uses `Debug`
105 // which is slow and causes lots of duplication of string data.
106 // The specialized impls below take care of making the `DefId` case more
108 impl<T: Debug> IntoSelfProfilingString for T {
109 default fn to_self_profile_string(
111 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
113 let s = format!("{:?}", self);
114 builder.profiler.alloc_string(&s[..])
118 impl<T: SpecIntoSelfProfilingString> IntoSelfProfilingString for T {
119 fn to_self_profile_string(&self, builder: &mut QueryKeyStringBuilder<'_, '_, '_>) -> StringId {
120 self.spec_to_self_profile_string(builder)
124 #[rustc_specialization_trait]
125 trait SpecIntoSelfProfilingString: Debug {
126 fn spec_to_self_profile_string(
128 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
132 impl SpecIntoSelfProfilingString for DefId {
133 fn spec_to_self_profile_string(
135 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
137 builder.def_id_to_string_id(*self)
141 impl SpecIntoSelfProfilingString for CrateNum {
142 fn spec_to_self_profile_string(
144 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
146 builder.def_id_to_string_id(self.as_def_id())
150 impl SpecIntoSelfProfilingString for DefIndex {
151 fn spec_to_self_profile_string(
153 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
155 builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: *self })
159 impl SpecIntoSelfProfilingString for LocalDefId {
160 fn spec_to_self_profile_string(
162 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
164 builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: self.local_def_index })
168 impl<T: SpecIntoSelfProfilingString> SpecIntoSelfProfilingString for WithOptConstParam<T> {
169 fn spec_to_self_profile_string(
171 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
173 // We print `WithOptConstParam` values as tuples to make them shorter
174 // and more readable, without losing information:
176 // "WithOptConstParam { did: foo::bar, const_param_did: Some(foo::baz) }"
177 // becomes "(foo::bar, foo::baz)" and
178 // "WithOptConstParam { did: foo::bar, const_param_did: None }"
179 // becomes "(foo::bar, _)".
181 let did = StringComponent::Ref(self.did.to_self_profile_string(builder));
183 let const_param_did = if let Some(const_param_did) = self.const_param_did {
184 let const_param_did = builder.def_id_to_string_id(const_param_did);
185 StringComponent::Ref(const_param_did)
187 StringComponent::Value("_")
191 StringComponent::Value("("),
193 StringComponent::Value(", "),
195 StringComponent::Value(")"),
198 builder.profiler.alloc_string(&components[..])
202 impl<T0, T1> SpecIntoSelfProfilingString for (T0, T1)
204 T0: SpecIntoSelfProfilingString,
205 T1: SpecIntoSelfProfilingString,
207 fn spec_to_self_profile_string(
209 builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
211 let val0 = self.0.to_self_profile_string(builder);
212 let val1 = self.1.to_self_profile_string(builder);
215 StringComponent::Value("("),
216 StringComponent::Ref(val0),
217 StringComponent::Value(","),
218 StringComponent::Ref(val1),
219 StringComponent::Value(")"),
222 builder.profiler.alloc_string(components)
226 /// Allocate the self-profiling query strings for a single query cache. This
227 /// method is called from `alloc_self_profile_query_strings` which knows all
228 /// the queries via macro magic.
229 fn alloc_self_profile_query_strings_for_query_cache<'tcx, C>(
231 query_name: &'static str,
233 string_cache: &mut QueryKeyStringCache,
236 C::Key: Debug + Clone,
238 tcx.prof.with_profiler(|profiler| {
239 let event_id_builder = profiler.event_id_builder();
241 // Walk the entire query cache and allocate the appropriate
242 // string representations. Each cache entry is uniquely
243 // identified by its dep_node_index.
244 if profiler.query_key_recording_enabled() {
245 let mut query_string_builder = QueryKeyStringBuilder::new(profiler, tcx, string_cache);
247 let query_name = profiler.get_or_alloc_cached_string(query_name);
249 // Since building the string representation of query keys might
250 // need to invoke queries itself, we cannot keep the query caches
251 // locked while doing so. Instead we copy out the
252 // `(query_key, dep_node_index)` pairs and release the lock again.
253 let mut query_keys_and_indices = Vec::new();
254 query_cache.iter(&mut |k, _, i| query_keys_and_indices.push((k.clone(), i)));
256 // Now actually allocate the strings. If allocating the strings
257 // generates new entries in the query cache, we'll miss them but
258 // we don't actually care.
259 for (query_key, dep_node_index) in query_keys_and_indices {
260 // Translate the DepNodeIndex into a QueryInvocationId
261 let query_invocation_id = dep_node_index.into();
263 // Create the string version of the query-key
264 let query_key = query_key.to_self_profile_string(&mut query_string_builder);
265 let event_id = event_id_builder.from_label_and_arg(query_name, query_key);
267 // Doing this in bulk might be a good idea:
268 profiler.map_query_invocation_id_to_string(
270 event_id.to_string_id(),
274 // In this branch we don't allocate query keys
275 let query_name = profiler.get_or_alloc_cached_string(query_name);
276 let event_id = event_id_builder.from_label(query_name).to_string_id();
278 // FIXME(eddyb) make this O(1) by using a pre-cached query name `EventId`,
279 // instead of passing the `DepNodeIndex` to `finish_with_query_invocation_id`,
280 // when recording the event in the first place.
281 let mut query_invocation_ids = Vec::new();
282 query_cache.iter(&mut |_, _, i| {
283 query_invocation_ids.push(i.into());
286 profiler.bulk_map_query_invocation_id_to_single_string(
287 query_invocation_ids.into_iter(),
294 /// All self-profiling events generated by the query engine use
295 /// virtual `StringId`s for their `event_id`. This method makes all
296 /// those virtual `StringId`s point to actual strings.
298 /// If we are recording only summary data, the ids will point to
299 /// just the query names. If we are recording query keys too, we
300 /// allocate the corresponding strings here.
301 pub fn alloc_self_profile_query_strings(tcx: TyCtxt<'_>) {
302 if !tcx.prof.enabled() {
306 let mut string_cache = QueryKeyStringCache::new();
308 macro_rules! alloc_once {
310 $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($K:ty) -> $V:ty,)*
313 alloc_self_profile_query_strings_for_query_cache(
316 &tcx.query_caches.$name,
323 rustc_query_append! { [alloc_once!][<'tcx>] }