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