77use std:: fmt:: { self , Write } ;
88use std:: hash:: Hash ;
99
10+ use rustc_data_structures:: fx:: FxHashMap ;
1011use rustc_data_structures:: stable_hasher:: StableHasher ;
11- use rustc_data_structures:: unord:: UnordMap ;
1212use rustc_hashes:: Hash64 ;
1313use rustc_index:: IndexVec ;
1414use rustc_macros:: { BlobDecodable , Decodable , Encodable } ;
@@ -97,36 +97,35 @@ impl DefPathTable {
9797 }
9898}
9999
100- pub trait Disambiguator {
101- fn entry ( & mut self , parent : LocalDefId , data : DefPathData ) -> & mut u32 ;
100+ pub enum Disambiguator < ' a > {
101+ PerParent ( & ' a mut PerParentDisambiguatorState ) ,
102+ Full ( & ' a mut DisambiguatorState ) ,
102103}
103104
104105#[ derive( Debug , Default , Clone ) ]
105106pub struct PerParentDisambiguatorState {
106- next : UnordMap < DefPathData , u32 > ,
107+ #[ cfg( debug_assertions) ]
108+ parent : Option < LocalDefId > ,
109+ next : FxHashMap < DefPathData , u32 > ,
107110}
108111
109- impl Disambiguator for PerParentDisambiguatorState {
110- #[ inline]
111- fn entry ( & mut self , _: LocalDefId , data : DefPathData ) -> & mut u32 {
112- self . next . entry ( data) . or_insert ( 0 )
112+ impl PerParentDisambiguatorState {
113+ pub fn new ( #[ cfg( debug_assertions) ] parent : LocalDefId ) -> PerParentDisambiguatorState {
114+ PerParentDisambiguatorState {
115+ #[ cfg( debug_assertions) ]
116+ parent : Some ( parent) ,
117+ next : Default :: default ( ) ,
118+ }
113119 }
114120}
115121
116122#[ derive( Debug , Default , Clone ) ]
117123pub struct DisambiguatorState {
118- next : UnordMap < ( LocalDefId , DefPathData ) , u32 > ,
119- }
120-
121- impl Disambiguator for DisambiguatorState {
122- #[ inline]
123- fn entry ( & mut self , parent : LocalDefId , data : DefPathData ) -> & mut u32 {
124- self . next . entry ( ( parent, data) ) . or_insert ( 0 )
125- }
124+ next : FxHashMap < ( LocalDefId , DefPathData ) , u32 > ,
126125}
127126
128127impl DisambiguatorState {
129- pub const fn new ( ) -> Self {
128+ pub fn new ( ) -> Self {
130129 Self { next : Default :: default ( ) }
131130 }
132131
@@ -408,7 +407,7 @@ impl Definitions {
408407 & mut self ,
409408 parent : LocalDefId ,
410409 data : DefPathData ,
411- disambiguator : & mut impl Disambiguator ,
410+ disambiguator : Disambiguator < ' _ > ,
412411 ) -> LocalDefId {
413412 // We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
414413 // reference to `Definitions` and we're already holding a mutable reference.
@@ -422,7 +421,19 @@ impl Definitions {
422421
423422 // Find the next free disambiguator for this key.
424423 let disambiguator = {
425- let next_disamb = disambiguator. entry ( parent, data) ;
424+ let next_disamb = match disambiguator {
425+ Disambiguator :: PerParent ( d) => {
426+ debug_assert_eq ! (
427+ parent,
428+ d. parent. expect( "must be set" ) ,
429+ "provided parent and parent in disambiguator must be the same"
430+ ) ;
431+
432+ d. next . entry ( data) . or_insert ( 0 )
433+ }
434+ Disambiguator :: Full ( d) => d. next . entry ( ( parent, data) ) . or_insert ( 0 ) ,
435+ } ;
436+
426437 let disambiguator = * next_disamb;
427438 * next_disamb = next_disamb. checked_add ( 1 ) . expect ( "disambiguator overflow" ) ;
428439 disambiguator
0 commit comments