File tree 1 file changed +32
-0
lines changed
crates/bevy_ecs/src/entity
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -998,4 +998,36 @@ mod tests {
998
998
assert ! ( Entity :: new( 2 , 2 ) > Entity :: new( 1 , 2 ) ) ;
999
999
assert ! ( Entity :: new( 2 , 2 ) >= Entity :: new( 1 , 2 ) ) ;
1000
1000
}
1001
+
1002
+ // Feel free to change this test if needed, but it seemed like an important
1003
+ // part of the best-case performance changes in PR#9903.
1004
+ #[ test]
1005
+ fn entity_hash_keeps_similar_ids_together ( ) {
1006
+ use std:: hash:: BuildHasher ;
1007
+ let hash = bevy_utils:: EntityHash ;
1008
+
1009
+ let first_id = 0xC0FFEE << 8 ;
1010
+ let first_hash = hash. hash_one ( Entity :: from_raw ( first_id) ) ;
1011
+
1012
+ for i in 1 ..=255 {
1013
+ let id = first_id + i;
1014
+ let hash = hash. hash_one ( Entity :: from_raw ( id) ) ;
1015
+ assert_eq ! ( hash. wrapping_sub( first_hash) as u32 , i) ;
1016
+ }
1017
+ }
1018
+
1019
+ #[ test]
1020
+ fn entity_hash_id_bitflip_affects_high_7_bits ( ) {
1021
+ use std:: hash:: BuildHasher ;
1022
+ let hash = bevy_utils:: EntityHash ;
1023
+
1024
+ let first_id = 0xC0FFEE ;
1025
+ let first_hash = hash. hash_one ( Entity :: from_raw ( first_id) ) >> 57 ;
1026
+
1027
+ for bit in 0 ..u32:: BITS {
1028
+ let id = first_id ^ ( 1 << bit) ;
1029
+ let hash = hash. hash_one ( Entity :: from_raw ( id) ) >> 57 ;
1030
+ assert_ne ! ( hash, first_hash) ;
1031
+ }
1032
+ }
1001
1033
}
You can’t perform that action at this time.
0 commit comments