Skip to content

Commit d810139

Browse files
committed
Deduplicate vtables within a single evaluation
1 parent 849a0e9 commit d810139

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use rustc::mir::interpret::{
2727
EvalResult, EvalErrorKind,
2828
truncate, sign_extend,
2929
};
30+
use rustc_data_structures::fx::FxHashMap;
3031

3132
use syntax::source_map::{self, Span};
3233

@@ -50,6 +51,9 @@ pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'a, 'mir, 'tcx>> {
5051

5152
/// The virtual call stack.
5253
pub(crate) stack: Vec<Frame<'mir, 'tcx, M::PointerTag>>,
54+
55+
/// A cache for deduplicating vtables
56+
pub(super) vtables: FxHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>), AllocId>,
5357
}
5458

5559
/// A stack frame.
@@ -207,6 +211,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tc
207211
param_env,
208212
memory: Memory::new(tcx, memory_data),
209213
stack: Vec::new(),
214+
vtables: FxHashMap(),
210215
}
211216
}
212217

src/librustc_mir/interpret/traits.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
2828
) -> EvalResult<'tcx, Pointer<M::PointerTag>> {
2929
debug!("get_vtable(trait_ref={:?})", trait_ref);
3030

31-
// FIXME: Cache this!
31+
if let Some(&vtable) = self.vtables.get(&(ty, trait_ref)) {
32+
return Ok(Pointer::from(vtable).with_default_tag());
33+
}
3234

3335
let layout = self.layout_of(trait_ref.self_ty())?;
3436
assert!(!layout.is_unsized(), "can't create a vtable for an unsized type");
@@ -64,6 +66,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
6466
}
6567

6668
self.memory.mark_immutable(vtable.alloc_id)?;
69+
assert!(self.vtables.insert((ty, trait_ref), vtable.alloc_id).is_none());
6770

6871
Ok(vtable)
6972
}

0 commit comments

Comments
 (0)