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 @@ -952,4 +952,36 @@ mod tests {
952
952
assert ! ( Entity :: new( 2 , 2 ) > Entity :: new( 1 , 2 ) ) ;
953
953
assert ! ( Entity :: new( 2 , 2 ) >= Entity :: new( 1 , 2 ) ) ;
954
954
}
955
+
956
+ // Feel free to change this test if needed, but it seemed like an important
957
+ // part of the best-case performance changes in PR#9903.
958
+ #[ test]
959
+ fn entity_hash_keeps_similar_ids_together ( ) {
960
+ use std:: hash:: BuildHasher ;
961
+ let hash = bevy_utils:: EntityHash ;
962
+
963
+ let first_id = 0xC0FFEE << 8 ;
964
+ let first_hash = hash. hash_one ( Entity :: from_raw ( first_id) ) ;
965
+
966
+ for i in 1 ..=255 {
967
+ let id = first_id + i;
968
+ let hash = hash. hash_one ( Entity :: from_raw ( id) ) ;
969
+ assert_eq ! ( hash. wrapping_sub( first_hash) as u32 , i) ;
970
+ }
971
+ }
972
+
973
+ #[ test]
974
+ fn entity_hash_id_bitflip_affects_high_7_bits ( ) {
975
+ use std:: hash:: BuildHasher ;
976
+ let hash = bevy_utils:: EntityHash ;
977
+
978
+ let first_id = 0xC0FFEE ;
979
+ let first_hash = hash. hash_one ( Entity :: from_raw ( first_id) ) >> 57 ;
980
+
981
+ for bit in 0 ..u32:: BITS {
982
+ let id = first_id ^ ( 1 << bit) ;
983
+ let hash = hash. hash_one ( Entity :: from_raw ( id) ) >> 57 ;
984
+ assert_ne ! ( hash, first_hash) ;
985
+ }
986
+ }
955
987
}
You can’t perform that action at this time.
0 commit comments