Shrink TyKind::FnPtr.

By splitting the `FnSig` within `TyKind::FnPtr` into `FnSigTys` and
`FnHeader`, which can be packed more efficiently. This reduces the size
of the hot `TyKind` type from 32 bytes to 24 bytes on 64-bit platforms.
This reduces peak memory usage by a few percent on some benchmarks. It
also reduces cache misses and page faults similarly, though this doesn't
translate to clear cycles or wall-time improvements on CI.
This commit is contained in:
Nicholas Nethercote 2024-08-08 17:18:20 +10:00
parent 1ac76a2062
commit e785219238
12 changed files with 13 additions and 13 deletions

View File

@ -15,7 +15,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
} }
match cast_from.kind() { match cast_from.kind() {
ty::FnDef(..) | ty::FnPtr(_) => { ty::FnDef(..) | ty::FnPtr(..) => {
let mut applicability = Applicability::MaybeIncorrect; let mut applicability = Applicability::MaybeIncorrect;
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability); let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);
let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx); let to_nbits = utils::int_ty_to_nbits(cast_to, cx.tcx);

View File

@ -14,7 +14,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
_ => { /* continue to checks */ }, _ => { /* continue to checks */ },
} }
if let ty::FnDef(..) | ty::FnPtr(_) = cast_from.kind() { if let ty::FnDef(..) | ty::FnPtr(..) = cast_from.kind() {
let mut applicability = Applicability::MaybeIncorrect; let mut applicability = Applicability::MaybeIncorrect;
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability); let from_snippet = snippet_with_applicability(cx, cast_expr.span, "..", &mut applicability);

View File

@ -14,7 +14,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
_ => return, _ => return,
} }
match cast_from.kind() { match cast_from.kind() {
ty::FnDef(..) | ty::FnPtr(_) => { ty::FnDef(..) | ty::FnPtr(..) => {
let mut applicability = Applicability::MaybeIncorrect; let mut applicability = Applicability::MaybeIncorrect;
let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability); let from_snippet = snippet_with_applicability(cx, cast_expr.span, "x", &mut applicability);

View File

@ -236,7 +236,7 @@ fn fn_sig_opt<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<PolyFnSig<'
// We can't use `Ty::fn_sig` because it automatically performs args, this may result in FNs. // We can't use `Ty::fn_sig` because it automatically performs args, this may result in FNs.
match node_ty.kind() { match node_ty.kind() {
ty::FnDef(def_id, _) => Some(cx.tcx.fn_sig(*def_id).instantiate_identity()), ty::FnDef(def_id, _) => Some(cx.tcx.fn_sig(*def_id).instantiate_identity()),
ty::FnPtr(fn_sig) => Some(*fn_sig), ty::FnPtr(sig_tys, hdr) => Some(sig_tys.with(*hdr)),
_ => None, _ => None,
} }
} }

View File

@ -872,7 +872,7 @@ fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>,
| ty::Pat(..) | ty::Pat(..)
| ty::Float(_) | ty::Float(_)
| ty::RawPtr(..) | ty::RawPtr(..)
| ty::FnPtr(_) | ty::FnPtr(..)
| ty::Str | ty::Str
| ty::Slice(..) | ty::Slice(..)
| ty::Adt(..) | ty::Adt(..)

View File

@ -158,7 +158,7 @@ fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tc
cx.tcx.fn_sig(def).skip_binder().skip_binder() cx.tcx.fn_sig(def).skip_binder().skip_binder()
}, },
ty::FnPtr(sig) => sig.skip_binder(), ty::FnPtr(sig_tys, hdr) => sig_tys.with(*hdr).skip_binder(),
ty::Closure(_, subs) => cx ty::Closure(_, subs) => cx
.tcx .tcx
.signature_unclosure(subs.as_closure().sig(), Safety::Safe) .signature_unclosure(subs.as_closure().sig(), Safety::Safe)

View File

@ -58,7 +58,7 @@ fn try_get_caller_ty_name_and_method_name(
fn is_map_to_option(cx: &LateContext<'_>, map_arg: &Expr<'_>) -> bool { fn is_map_to_option(cx: &LateContext<'_>, map_arg: &Expr<'_>) -> bool {
let map_closure_ty = cx.typeck_results().expr_ty(map_arg); let map_closure_ty = cx.typeck_results().expr_ty(map_arg);
match map_closure_ty.kind() { match map_closure_ty.kind() {
ty::Closure(_, _) | ty::FnDef(_, _) | ty::FnPtr(_) => { ty::Closure(_, _) | ty::FnDef(_, _) | ty::FnPtr(..) => {
let map_closure_sig = match map_closure_ty.kind() { let map_closure_sig = match map_closure_ty.kind() {
ty::Closure(_, args) => args.as_closure().sig(), ty::Closure(_, args) => args.as_closure().sig(),
_ => map_closure_ty.fn_sig(cx.tcx), _ => map_closure_ty.fn_sig(cx.tcx),

View File

@ -166,7 +166,7 @@ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
ExprKind::Call(func, _) => { ExprKind::Call(func, _) => {
let typ = self.cx.typeck_results().expr_ty(func); let typ = self.cx.typeck_results().expr_ty(func);
match typ.kind() { match typ.kind() {
ty::FnDef(..) | ty::FnPtr(_) => { ty::FnDef(..) | ty::FnPtr(..) => {
let sig = typ.fn_sig(self.cx.tcx); let sig = typ.fn_sig(self.cx.tcx);
if self.cx.tcx.instantiate_bound_regions_with_erased(sig).output().kind() == &ty::Never { if self.cx.tcx.instantiate_bound_regions_with_erased(sig).output().kind() == &ty::Never {
self.report_diverging_sub_expr(e); self.report_diverging_sub_expr(e);

View File

@ -130,7 +130,7 @@ fn collect_unsafe_exprs<'tcx>(
ExprKind::Call(path_expr, _) => { ExprKind::Call(path_expr, _) => {
let sig = match *cx.typeck_results().expr_ty(path_expr).kind() { let sig = match *cx.typeck_results().expr_ty(path_expr).kind() {
ty::FnDef(id, _) => cx.tcx.fn_sig(id).skip_binder(), ty::FnDef(id, _) => cx.tcx.fn_sig(id).skip_binder(),
ty::FnPtr(sig) => sig, ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr),
_ => return Continue(Descend::Yes), _ => return Continue(Descend::Yes),
}; };
if sig.safety() == Safety::Unsafe { if sig.safety() == Safety::Unsafe {

View File

@ -79,7 +79,7 @@ fn check_arguments<'tcx>(
fn_kind: &str, fn_kind: &str,
) { ) {
match type_definition.kind() { match type_definition.kind() {
ty::FnDef(..) | ty::FnPtr(_) => { ty::FnDef(..) | ty::FnPtr(..) => {
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs(); let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
for (argument, parameter) in iter::zip(arguments, parameters) { for (argument, parameter) in iter::zip(arguments, parameters) {
match parameter.kind() { match parameter.kind() {

View File

@ -541,7 +541,7 @@ fn f(ty: Ty<'_>, count: usize, mutability: Mutability) -> (Ty<'_>, usize, Mutabi
/// Returns `true` if the given type is an `unsafe` function. /// Returns `true` if the given type is an `unsafe` function.
pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
match ty.kind() { match ty.kind() {
ty::FnDef(..) | ty::FnPtr(_) => ty.fn_sig(cx.tcx).safety() == Safety::Unsafe, ty::FnDef(..) | ty::FnPtr(..) => ty.fn_sig(cx.tcx).safety() == Safety::Unsafe,
_ => false, _ => false,
} }
} }
@ -721,7 +721,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'t
cx.tcx.item_super_predicates(def_id).iter_instantiated(cx.tcx, args), cx.tcx.item_super_predicates(def_id).iter_instantiated(cx.tcx, args),
cx.tcx.opt_parent(def_id), cx.tcx.opt_parent(def_id),
), ),
ty::FnPtr(sig) => Some(ExprFnSig::Sig(sig, None)), ty::FnPtr(sig_tys, hdr) => Some(ExprFnSig::Sig(sig_tys.with(hdr), None)),
ty::Dynamic(bounds, _, _) => { ty::Dynamic(bounds, _, _) => {
let lang_items = cx.tcx.lang_items(); let lang_items = cx.tcx.lang_items();
match bounds.principal() { match bounds.principal() {

View File

@ -441,7 +441,7 @@ fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
ty::FnDef(id, _) if self.cx.tcx.fn_sig(id).skip_binder().safety() == Safety::Unsafe => { ty::FnDef(id, _) if self.cx.tcx.fn_sig(id).skip_binder().safety() == Safety::Unsafe => {
self.is_unsafe = true; self.is_unsafe = true;
}, },
ty::FnPtr(sig) if sig.safety() == Safety::Unsafe => self.is_unsafe = true, ty::FnPtr(_, hdr) if hdr.safety == Safety::Unsafe => self.is_unsafe = true,
_ => walk_expr(self, e), _ => walk_expr(self, e),
}, },
ExprKind::Path(ref p) ExprKind::Path(ref p)