Skip to content

Commit d81126f

Browse files
committed
Auto merge of rust-lang#129761 - saethlin:cacher-location, r=<try>
Rub come caching on caller_location Inspired by the huge perf wins from making track_caller a no-op: rust-lang#129704, I started poking around and it looks like we have no deduplication of identical `panic::Location` allocations. Nora says I can use a query here, but I'm scared of query overhead so I'm going to perf something very crude first. r? `@ghost`
2 parents 784d444 + b691f26 commit d81126f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

compiler/rustc_const_eval/src/util/caller_location.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::hash_map::Entry;
2+
13
use rustc_hir::LangItem;
24
use rustc_middle::query::TyCtxtAt;
35
use rustc_middle::ty::layout::LayoutOf;
@@ -57,6 +59,11 @@ pub(crate) fn const_caller_location_provider(
5759
col: u32,
5860
) -> mir::ConstValue<'_> {
5961
trace!("const_caller_location: {}:{}:{}", file, line, col);
62+
let mut cache = tcx.caller_location_cache.lock();
63+
let entry = match cache.entry((file, line, col)) {
64+
Entry::Occupied(oe) => return *oe.get(),
65+
Entry::Vacant(ve) => ve,
66+
};
6067
let mut ecx = mk_eval_cx_to_read_const_val(
6168
tcx.tcx,
6269
tcx.span,
@@ -68,5 +75,7 @@ pub(crate) fn const_caller_location_provider(
6875
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
6976
bug!("intern_const_alloc_recursive should not error in this case")
7077
}
71-
mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
78+
let val = mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx));
79+
entry.insert(val);
80+
val
7281
}

compiler/rustc_middle/src/ty/context.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::metadata::ModChild;
6666
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
6767
use crate::middle::{resolve_bound_vars, stability};
6868
use crate::mir::interpret::{self, Allocation, ConstAllocation};
69-
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
69+
use crate::mir::{Body, ConstValue, Local, Place, PlaceElem, ProjectionKind, Promoted};
7070
use crate::query::plumbing::QuerySystem;
7171
use crate::query::{IntoQueryParam, LocalCrate, Providers, TyCtxtAt};
7272
use crate::thir::Thir;
@@ -1295,6 +1295,8 @@ pub struct GlobalCtxt<'tcx> {
12951295
/// Stores memory for globals (statics/consts).
12961296
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
12971297

1298+
pub caller_location_cache: Lock<FxHashMap<(Symbol, u32, u32), ConstValue<'tcx>>>,
1299+
12981300
current_gcx: CurrentGcx,
12991301
}
13001302

@@ -1525,6 +1527,7 @@ impl<'tcx> TyCtxt<'tcx> {
15251527
canonical_param_env_cache: Default::default(),
15261528
data_layout,
15271529
alloc_map: Lock::new(interpret::AllocMap::new()),
1530+
caller_location_cache: Default::default(),
15281531
current_gcx,
15291532
}
15301533
}

0 commit comments

Comments
 (0)