Skip to content

Commit 08d9517

Browse files
self-profile: Cache more query key strings when doing self-profiling.
1 parent 3fbed17 commit 08d9517

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/librustc_middle/ty/query/profiling_support.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::ty::context::TyCtxt;
2+
use crate::ty::WithOptConstParam;
23
use measureme::{StringComponent, StringId};
34
use rustc_data_structures::fx::FxHashMap;
45
use rustc_data_structures::profiling::SelfProfiler;
5-
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
6+
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
67
use rustc_hir::definitions::DefPathData;
78
use rustc_query_system::query::QueryCache;
89
use rustc_query_system::query::QueryState;
@@ -154,6 +155,49 @@ impl SpecIntoSelfProfilingString for DefIndex {
154155
}
155156
}
156157

158+
impl SpecIntoSelfProfilingString for LocalDefId {
159+
fn spec_to_self_profile_string(
160+
&self,
161+
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
162+
) -> StringId {
163+
builder.def_id_to_string_id(DefId { krate: LOCAL_CRATE, index: self.local_def_index })
164+
}
165+
}
166+
167+
impl<T: SpecIntoSelfProfilingString> SpecIntoSelfProfilingString for WithOptConstParam<T> {
168+
fn spec_to_self_profile_string(
169+
&self,
170+
builder: &mut QueryKeyStringBuilder<'_, '_, '_>,
171+
) -> StringId {
172+
// We print `WithOptConstParam` values as tuples to make them shorter
173+
// and more readable, without losing information:
174+
//
175+
// "WithOptConstParam { did: foo::bar, const_param_did: Some(foo::baz) }"
176+
// becomes "(foo::bar, foo::baz)" and
177+
// "WithOptConstParam { did: foo::bar, const_param_did: None }"
178+
// becomes "(foo::bar, _)".
179+
180+
let did = StringComponent::Ref(self.did.to_self_profile_string(builder));
181+
182+
let const_param_did = if let Some(const_param_did) = self.const_param_did {
183+
let const_param_did = builder.def_id_to_string_id(const_param_did);
184+
StringComponent::Ref(const_param_did)
185+
} else {
186+
StringComponent::Value("_")
187+
};
188+
189+
let components = [
190+
StringComponent::Value("("),
191+
did,
192+
StringComponent::Value(", "),
193+
const_param_did,
194+
StringComponent::Value(")"),
195+
];
196+
197+
builder.profiler.alloc_string(&components[..])
198+
}
199+
}
200+
157201
impl<T0, T1> SpecIntoSelfProfilingString for (T0, T1)
158202
where
159203
T0: SpecIntoSelfProfilingString,

0 commit comments

Comments
 (0)