@@ -187,7 +187,7 @@ impl Identity {
187
187
}
188
188
189
189
/// Stores the data that (almost) uniquely identifies a tracked struct.
190
- /// This includes the ingredient index of that struct type plus the hash of its id fields.
190
+ /// This includes the ingredient index of that struct type plus the hash of its untracked fields.
191
191
/// This is mapped to a disambiguator -- a value that starts as 0 but increments each round,
192
192
/// allowing for multiple tracked structs with the same hash and ingredient_index
193
193
/// created within the query to each have a unique id.
@@ -222,10 +222,7 @@ impl IdentityMap {
222
222
pub ( crate ) fn insert ( & mut self , key : Identity , id : Id ) -> Option < Id > {
223
223
use hashbrown:: hash_map:: RawEntryMut ;
224
224
225
- let eq_modulo_hash = |k : & Identity | {
226
- k. ingredient_index == key. ingredient_index && k. disambiguator == key. disambiguator
227
- } ;
228
- let entry = self . map . raw_entry_mut ( ) . from_hash ( key. hash , eq_modulo_hash) ;
225
+ let entry = self . map . raw_entry_mut ( ) . from_hash ( key. hash , |k| * k == key) ;
229
226
match entry {
230
227
RawEntryMut :: Occupied ( mut occupied) => Some ( occupied. insert ( id) ) ,
231
228
RawEntryMut :: Vacant ( vacant) => {
@@ -236,12 +233,9 @@ impl IdentityMap {
236
233
}
237
234
238
235
pub ( crate ) fn get ( & self , key : & Identity ) -> Option < Id > {
239
- let eq_modulo_hash = |k : & Identity | {
240
- k. ingredient_index == key. ingredient_index && k. disambiguator == key. disambiguator
241
- } ;
242
236
self . map
243
237
. raw_entry ( )
244
- . from_hash ( key. hash , eq_modulo_hash )
238
+ . from_hash ( key. hash , |k| * k == * key )
245
239
. map ( |( _, & v) | v)
246
240
}
247
241
@@ -318,8 +312,7 @@ impl DisambiguatorMap {
318
312
pub ( crate ) fn disambiguate ( & mut self , key : IdentityHash ) -> Disambiguator {
319
313
use hashbrown:: hash_map:: RawEntryMut ;
320
314
321
- let eq_modulo_hash = |k : & IdentityHash | k. ingredient_index == key. ingredient_index ;
322
- let entry = self . map . raw_entry_mut ( ) . from_hash ( key. hash , eq_modulo_hash) ;
315
+ let entry = self . map . raw_entry_mut ( ) . from_hash ( key. hash , |k| * k == key) ;
323
316
let disambiguator = match entry {
324
317
RawEntryMut :: Occupied ( occupied) => occupied. into_mut ( ) ,
325
318
RawEntryMut :: Vacant ( vacant) => {
@@ -388,7 +381,7 @@ where
388
381
389
382
let identity = Identity {
390
383
hash : identity_hash. hash ,
391
- ingredient_index : self . ingredient_index ,
384
+ ingredient_index : identity_hash . ingredient_index ,
392
385
disambiguator,
393
386
} ;
394
387
@@ -845,3 +838,101 @@ where
845
838
& self . syncs
846
839
}
847
840
}
841
+
842
+ #[ cfg( test) ]
843
+ mod tests {
844
+ use super :: * ;
845
+
846
+ #[ test]
847
+ fn disambiguate_map_works ( ) {
848
+ let mut d = DisambiguatorMap :: default ( ) ;
849
+ // set up all 4 permutations of differing field values
850
+ let h1 = IdentityHash {
851
+ ingredient_index : IngredientIndex :: from ( 0 ) ,
852
+ hash : 0 ,
853
+ } ;
854
+ let h2 = IdentityHash {
855
+ ingredient_index : IngredientIndex :: from ( 1 ) ,
856
+ hash : 0 ,
857
+ } ;
858
+ let h3 = IdentityHash {
859
+ ingredient_index : IngredientIndex :: from ( 0 ) ,
860
+ hash : 1 ,
861
+ } ;
862
+ let h4 = IdentityHash {
863
+ ingredient_index : IngredientIndex :: from ( 1 ) ,
864
+ hash : 1 ,
865
+ } ;
866
+ assert_eq ! ( d. disambiguate( h1) , Disambiguator ( 0 ) ) ;
867
+ assert_eq ! ( d. disambiguate( h1) , Disambiguator ( 1 ) ) ;
868
+ assert_eq ! ( d. disambiguate( h2) , Disambiguator ( 0 ) ) ;
869
+ assert_eq ! ( d. disambiguate( h2) , Disambiguator ( 1 ) ) ;
870
+ assert_eq ! ( d. disambiguate( h3) , Disambiguator ( 0 ) ) ;
871
+ assert_eq ! ( d. disambiguate( h3) , Disambiguator ( 1 ) ) ;
872
+ assert_eq ! ( d. disambiguate( h4) , Disambiguator ( 0 ) ) ;
873
+ assert_eq ! ( d. disambiguate( h4) , Disambiguator ( 1 ) ) ;
874
+ }
875
+
876
+ #[ test]
877
+ fn identity_map_works ( ) {
878
+ let mut d = IdentityMap :: default ( ) ;
879
+ // set up all 8 permutations of differing field values
880
+ let i1 = Identity {
881
+ ingredient_index : IngredientIndex :: from ( 0 ) ,
882
+ hash : 0 ,
883
+ disambiguator : Disambiguator ( 0 ) ,
884
+ } ;
885
+ let i2 = Identity {
886
+ ingredient_index : IngredientIndex :: from ( 1 ) ,
887
+ hash : 0 ,
888
+ disambiguator : Disambiguator ( 0 ) ,
889
+ } ;
890
+ let i3 = Identity {
891
+ ingredient_index : IngredientIndex :: from ( 0 ) ,
892
+ hash : 1 ,
893
+ disambiguator : Disambiguator ( 0 ) ,
894
+ } ;
895
+ let i4 = Identity {
896
+ ingredient_index : IngredientIndex :: from ( 1 ) ,
897
+ hash : 1 ,
898
+ disambiguator : Disambiguator ( 0 ) ,
899
+ } ;
900
+ let i5 = Identity {
901
+ ingredient_index : IngredientIndex :: from ( 0 ) ,
902
+ hash : 0 ,
903
+ disambiguator : Disambiguator ( 1 ) ,
904
+ } ;
905
+ let i6 = Identity {
906
+ ingredient_index : IngredientIndex :: from ( 1 ) ,
907
+ hash : 0 ,
908
+ disambiguator : Disambiguator ( 1 ) ,
909
+ } ;
910
+ let i7 = Identity {
911
+ ingredient_index : IngredientIndex :: from ( 0 ) ,
912
+ hash : 1 ,
913
+ disambiguator : Disambiguator ( 1 ) ,
914
+ } ;
915
+ let i8 = Identity {
916
+ ingredient_index : IngredientIndex :: from ( 1 ) ,
917
+ hash : 1 ,
918
+ disambiguator : Disambiguator ( 1 ) ,
919
+ } ;
920
+ assert_eq ! ( d. insert( i1, Id :: from_u32( 0 ) ) , None ) ;
921
+ assert_eq ! ( d. insert( i2, Id :: from_u32( 1 ) ) , None ) ;
922
+ assert_eq ! ( d. insert( i3, Id :: from_u32( 2 ) ) , None ) ;
923
+ assert_eq ! ( d. insert( i4, Id :: from_u32( 3 ) ) , None ) ;
924
+ assert_eq ! ( d. insert( i5, Id :: from_u32( 4 ) ) , None ) ;
925
+ assert_eq ! ( d. insert( i6, Id :: from_u32( 5 ) ) , None ) ;
926
+ assert_eq ! ( d. insert( i7, Id :: from_u32( 6 ) ) , None ) ;
927
+ assert_eq ! ( d. insert( i8 , Id :: from_u32( 7 ) ) , None ) ;
928
+
929
+ assert_eq ! ( d. get( & i1) , Some ( Id :: from_u32( 0 ) ) ) ;
930
+ assert_eq ! ( d. get( & i2) , Some ( Id :: from_u32( 1 ) ) ) ;
931
+ assert_eq ! ( d. get( & i3) , Some ( Id :: from_u32( 2 ) ) ) ;
932
+ assert_eq ! ( d. get( & i4) , Some ( Id :: from_u32( 3 ) ) ) ;
933
+ assert_eq ! ( d. get( & i5) , Some ( Id :: from_u32( 4 ) ) ) ;
934
+ assert_eq ! ( d. get( & i6) , Some ( Id :: from_u32( 5 ) ) ) ;
935
+ assert_eq ! ( d. get( & i7) , Some ( Id :: from_u32( 6 ) ) ) ;
936
+ assert_eq ! ( d. get( & i8 ) , Some ( Id :: from_u32( 7 ) ) ) ;
937
+ }
938
+ }
0 commit comments