Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache type sizes in type-size limit visitor #127288

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions compiler/rustc_middle/src/ty/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::ty::{
self, EarlyBinder, GenericArgs, GenericArgsRef, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable,
TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor,
};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def::Namespace;
Expand Down Expand Up @@ -388,21 +389,33 @@ impl<'tcx> InstanceKind<'tcx> {
}

fn type_length<'tcx>(item: impl TypeVisitable<TyCtxt<'tcx>>) -> usize {
struct Visitor {
struct Visitor<'tcx> {
type_length: usize,
cache: FxHashMap<Ty<'tcx>, usize>,
}
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor {
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for Visitor<'tcx> {
fn visit_ty(&mut self, t: Ty<'tcx>) {
if let Some(&value) = self.cache.get(&t) {
self.type_length += value;
return;
}

let prev = self.type_length;
self.type_length += 1;
t.super_visit_with(self);

// We don't try to use the cache if the type is fairly small.
if self.type_length > 16 {
self.cache.insert(t, self.type_length - prev);
}
}

fn visit_const(&mut self, ct: ty::Const<'tcx>) {
self.type_length += 1;
ct.super_visit_with(self);
}
}
let mut visitor = Visitor { type_length: 0 };
let mut visitor = Visitor { type_length: 0, cache: Default::default() };
item.visit_with(&mut visitor);

visitor.type_length
Expand Down
Loading