Skip to content

Commit 61d7280

Browse files
committed
Auto merge of #157275 - RalfJung:interpret-layout-cache, r=oli-obk
interpret: add per-interpreter layout cache Context: #157010
2 parents 8954863 + 8e28461 commit 61d7280

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use std::cell::RefCell;
2+
use std::collections::hash_map::Entry;
3+
14
use either::{Left, Right};
25
use rustc_abi::{Align, HasDataLayout, Size, TargetDataLayout};
6+
use rustc_data_structures::fx::FxHashMap;
37
use rustc_hir::def_id::DefId;
48
use rustc_hir::limit::Limit;
59
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
@@ -38,6 +42,9 @@ pub struct InterpCx<'tcx, M: Machine<'tcx>> {
3842
/// polymorphic context. This always uses `ty::TypingMode::PostAnalysis`.
3943
pub(super) typing_env: ty::TypingEnv<'tcx>,
4044

45+
/// The query cache is slow so we have our own cache in front of it.
46+
pub(super) layout_cache: RefCell<FxHashMap<Ty<'tcx>, rustc_abi::Layout<'tcx>>>,
47+
4148
/// The virtual memory system.
4249
pub memory: Memory<'tcx, M>,
4350

@@ -130,10 +137,19 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
130137
/// This inherent method takes priority over the trait method with the same name in LayoutOf,
131138
/// and allows wrapping the actual [LayoutOf::layout_of] with a tracing span.
132139
/// See [LayoutOf::layout_of] for the original documentation.
133-
#[inline(always)]
140+
#[inline]
134141
pub fn layout_of(&self, ty: Ty<'tcx>) -> Result<TyAndLayout<'tcx>, InterpErrorKind<'tcx>> {
135-
let _trace = enter_trace_span!(M, layouting::layout_of, ty = ?ty.kind());
136-
LayoutOf::layout_of(self, ty)
142+
match self.layout_cache.borrow_mut().entry(ty) {
143+
Entry::Occupied(occupied_entry) => {
144+
Ok(TyAndLayout { ty, layout: *occupied_entry.get() })
145+
}
146+
Entry::Vacant(vacant_entry) => {
147+
let _trace = enter_trace_span!(M, layouting::layout_of, ty = ?ty.kind());
148+
let layout = LayoutOf::layout_of(self, ty)?;
149+
vacant_entry.insert(layout.layout);
150+
Ok(layout)
151+
}
152+
}
137153
}
138154

139155
/// This inherent method takes priority over the trait method with the same name in FnAbiOf,
@@ -248,6 +264,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
248264
machine,
249265
tcx: tcx.at(root_span),
250266
typing_env,
267+
layout_cache: RefCell::new(FxHashMap::default()),
251268
memory: Memory::new(),
252269
recursion_limit: tcx.recursion_limit(),
253270
}

0 commit comments

Comments
 (0)