@@ -58,6 +58,34 @@ impl Optimizer {
5858 // when TypeArenaWithDSU is dropped
5959 }
6060 }
61+ // drop(ufarena);
62+
63+ // let sets = schema.arena.find_disjoint_sets(|a, b| {
64+ // if let (Some(a), Some(b)) = (a.as_union(), b.as_union()) {
65+ // self.to_merge_same_unions && (a.types == b.types)
66+ // } else {
67+ // // TODO: merge same array?
68+ // false
69+ // }
70+ // });
71+ // for (leader, mut set) in sets.into_iter() {
72+ // if ufarena.get(leader).map(|r#type| {
73+ // self.to_merge_same_unions && r#type.is_union()
74+ // }) == Some(true)
75+ // {
76+ // set.insert(leader); // leader in disjoint set is now a follower
77+
78+ // let compact_set = set
79+ // .iter()
80+ // .cloned()
81+ // .filter(|&r#type| ufarena.contains(r#type))
82+ // .collect::<Vec<ArenaIndex>>();
83+ // // unioned is now the new leader
84+ // let _leader = union(&mut ufarena, compact_set);
85+ // // References to non-representative AreneIndex will be replaced automatically
86+ // // when TypeArenaWithDSU is dropped
87+ // }
88+ // }
6189 // Although unioner always keeps the first map slot intact, there is no guarantee that
6290 // root would always be the first map in types to be unioned. So update it if necessary.
6391 schema. root = ufarena. find_representative ( schema. root ) . unwrap ( ) ;
@@ -120,6 +148,7 @@ pub struct TypeArenaWithDSU<'a> {
120148
121149impl < ' a > TypeArenaWithDSU < ' a > {
122150 fn from_type_arena ( arena : & ' a mut TypeArena ) -> Self {
151+ dbg ! ( arena. len( ) ) ;
123152 let imap: Bimap < usize , ArenaIndex > =
124153 Bimap :: from_hash_map ( arena. iter ( ) . map ( |( index, _) | index) . enumerate ( ) . collect ( ) ) ;
125154
@@ -189,7 +218,7 @@ impl<'a> TypeArenaWithDSU<'a> {
189218 for r#type in dangling_types. into_iter ( ) {
190219 // TODO: Should these all removed during unioning?
191220 println ! ( "removed dangling: {:?}" , r#type) ;
192- assert ! ( self . arena. remove( r#type) . is_none( ) ) ;
221+ // debug_assert !(self.arena.remove(r#type).is_none());
193222 }
194223 }
195224
@@ -237,21 +266,29 @@ impl<'a> ITypeArena for TypeArenaWithDSU<'a> {
237266 // Note: It is not removed from DSU. So just ignore non-existing types when iterating DSU.
238267 // As get/get_mut wraps DSU internally, unioner won't get panicked.
239268 DerefMut :: deref_mut ( self ) . remove ( i)
269+ // DerefMut::deref_mut(self).get(i).cloned()
240270 }
241271
242272 /// Remove the type denoted by the index i and union i into j in the DSU
243273 fn remove_in_favor_of ( & mut self , i : ArenaIndex , j : ArenaIndex ) -> Option < Type > {
274+ dbg ! ( i, j, self . imap. len( ) , self . arena. len( ) ) ;
275+
244276 self . dsu . union (
245277 * self . imap . get_rev ( & i) . unwrap ( ) ,
246278 * self . imap . get_rev ( & j) . unwrap ( ) ,
247279 ) ;
248- DerefMut :: deref_mut ( self ) . remove ( i)
280+ // DerefMut::deref_mut(self).remove(i)
281+ DerefMut :: deref_mut ( self ) . get ( i) . cloned ( )
249282 }
250283
251284 #[ inline( always) ]
252285 fn insert ( & mut self , value : Type ) -> ArenaIndex {
253286 // Note: The DSU is not updated.
254- DerefMut :: deref_mut ( self ) . insert ( value)
287+ debug_assert_eq ! ( self . dsu. len( ) , self . imap. len( ) ) ;
288+ let i = self . dsu . alloc ( ) ;
289+ let arni = DerefMut :: deref_mut ( self ) . insert ( value) ;
290+ self . imap . insert ( i, arni) ;
291+ arni
255292 }
256293
257294 #[ inline( always) ]
0 commit comments