Skip to content

Commit 96312fb

Browse files
committed
cache type sizes in type-size limit visitor
1 parent 1cfd47f commit 96312fb

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

compiler/rustc_middle/src/ty/instance.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ty::{
55
self, EarlyBinder, GenericArgs, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
66
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
77
};
8+
use rustc_data_structures::fx::FxHashMap;
89
use rustc_errors::ErrorGuaranteed;
910
use rustc_hir as hir;
1011
use rustc_hir::def::Namespace;
@@ -388,21 +389,32 @@ impl<'tcx> InstanceKind<'tcx> {
388389
}
389390

390391
fn type_length<'tcx>(item: impl TypeVisitable<TyCtxt<'tcx>>) -> usize {
391-
struct Visitor {
392+
struct Visitor<'tcx> {
392393
type_length: usize,
394+
cache: FxHashMap<Ty<'tcx>, usize>,
393395
}
394-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor {
396+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor<'tcx> {
395397
fn visit_ty(&mut self, t: Ty<'tcx>) {
398+
if let Some(&value) = self.cache.get(&t) {
399+
self.type_length += value;
400+
}
401+
402+
let prev = self.type_length;
396403
self.type_length += 1;
397404
t.super_visit_with(self);
405+
406+
// We don't try to use the cache if the type is fairly small.
407+
if self.type_length > 16 {
408+
self.cache.insert(t, self.type_length - prev);
409+
}
398410
}
399411

400412
fn visit_const(&mut self, ct: ty::Const<'tcx>) {
401413
self.type_length += 1;
402414
ct.super_visit_with(self);
403415
}
404416
}
405-
let mut visitor = Visitor { type_length: 0 };
417+
let mut visitor = Visitor { type_length: 0, cache: Default::default() };
406418
item.visit_with(&mut visitor);
407419

408420
visitor.type_length

0 commit comments

Comments
 (0)