@@ -720,6 +720,9 @@ struct MirUsedCollector<'a, 'tcx> {
720
720
tcx : TyCtxt < ' tcx > ,
721
721
body : & ' a mir:: Body < ' tcx > ,
722
722
used_items : & ' a mut MonoItems < ' tcx > ,
723
+ /// See the comment in `collect_items_of_instance` for the purpose of this set.
724
+ /// Note that this contains *not-monomorphized* items!
725
+ used_mentioned_items : & ' a mut FxHashSet < MentionedItem < ' tcx > > ,
723
726
instance : Instance < ' tcx > ,
724
727
/// Spans for move size lints already emitted. Helps avoid duplicate lints.
725
728
move_size_spans : Vec < Span > ,
@@ -990,12 +993,18 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
990
993
match terminator. kind {
991
994
mir:: TerminatorKind :: Call { ref func, ref args, ref fn_span, .. } => {
992
995
let callee_ty = func. ty ( self . body , tcx) ;
996
+ // *Before* monomorphizing, record that we already handled this mention.
997
+ if let ty:: FnDef ( def_id, args) = callee_ty. kind ( ) {
998
+ self . used_mentioned_items . insert ( MentionedItem :: Fn ( * def_id, args) ) ;
999
+ }
993
1000
let callee_ty = self . monomorphize ( callee_ty) ;
994
1001
self . check_fn_args_move_size ( callee_ty, args, * fn_span, location) ;
995
1002
visit_fn_use ( self . tcx , callee_ty, true , source, & mut self . used_items )
996
1003
}
997
1004
mir:: TerminatorKind :: Drop { ref place, .. } => {
998
1005
let ty = place. ty ( self . body , self . tcx ) . ty ;
1006
+ // *Before* monomorphizing, record that we already handled this mention.
1007
+ self . used_mentioned_items . insert ( MentionedItem :: Drop ( ty) ) ;
999
1008
let ty = self . monomorphize ( ty) ;
1000
1009
visit_drop_use ( self . tcx , ty, true , source, self . used_items ) ;
1001
1010
}
@@ -1639,10 +1648,22 @@ fn collect_items_of_instance<'tcx>(
1639
1648
mode : CollectionMode ,
1640
1649
) {
1641
1650
let body = tcx. instance_mir ( instance. def ) ;
1651
+ // Naively, in "used" collection mode, all functions get added to *both* `used_items` and
1652
+ // `mentioned_items`. Mentioned items processing will then notice that they have already been
1653
+ // visited, but at that point each mentioned item has been monomorphized, added to the
1654
+ // `mentioned_items` worklist, and checked in the global set of visited items. To removes that
1655
+ // overhead, we have a special optimization that avoids adding items to `mentioned_items` when
1656
+ // they are already added in `used_items`. We could just scan `used_items`, but that's a linear
1657
+ // scan and not very efficient. Furthermore we can only do that *after* monomorphizing the
1658
+ // mentioned item. So instead we collect all pre-monomorphized `MentionedItem` that were already
1659
+ // added to `used_items` in a hash set, which can efficiently query in the
1660
+ // `body.mentioned_items` loop below.
1661
+ let mut used_mentioned_items = FxHashSet :: < MentionedItem < ' tcx > > :: default ( ) ;
1642
1662
let mut collector = MirUsedCollector {
1643
1663
tcx,
1644
1664
body,
1645
1665
used_items,
1666
+ used_mentioned_items : & mut used_mentioned_items,
1646
1667
instance,
1647
1668
move_size_spans : vec ! [ ] ,
1648
1669
visiting_call_terminator : false ,
@@ -1662,10 +1683,13 @@ fn collect_items_of_instance<'tcx>(
1662
1683
}
1663
1684
}
1664
1685
1665
- // Always gather mentioned items.
1686
+ // Always gather mentioned items. We try to avoid processing items that we have already added to
1687
+ // `used_items` above.
1666
1688
for item in & body. mentioned_items {
1667
- let item_mono = collector. monomorphize ( item. node ) ;
1668
- visit_mentioned_item ( tcx, & item_mono, item. span , mentioned_items) ;
1689
+ if !collector. used_mentioned_items . contains ( & item. node ) {
1690
+ let item_mono = collector. monomorphize ( item. node ) ;
1691
+ visit_mentioned_item ( tcx, & item_mono, item. span , mentioned_items) ;
1692
+ }
1669
1693
}
1670
1694
}
1671
1695
0 commit comments