@@ -15,7 +15,7 @@ use crate::{
15
15
prelude:: World ,
16
16
query:: DebugCheckedUnwrap ,
17
17
storage:: { SparseSetIndex , SparseSets , Storages , Table , TableRow } ,
18
- world:: { unsafe_world_cell:: UnsafeWorldCell , DeferredWorld } ,
18
+ world:: { unsafe_world_cell:: UnsafeWorldCell } ,
19
19
} ;
20
20
use bevy_ptr:: { ConstNonNull , OwningPtr } ;
21
21
use bevy_utils:: all_tuples;
@@ -637,205 +637,183 @@ impl<'w> BundleInserter<'w> {
637
637
location : EntityLocation ,
638
638
bundle : T ,
639
639
) -> EntityLocation {
640
- #[ inline]
641
- fn trigger_hooks (
642
- entity : Entity ,
643
- bundle_info : & BundleInfo ,
644
- add_bundle : & AddBundle ,
645
- archetype : & Archetype ,
646
- mut world : DeferredWorld ,
647
- ) {
648
- if archetype. has_on_add ( ) {
649
- // SAFETY: All components in the bundle are guarenteed to exist in the World
650
- // as they must be initialized before creating the BundleInfo.
651
- unsafe {
652
- world. trigger_on_add (
653
- entity,
654
- bundle_info
655
- . iter_components ( )
656
- . zip ( add_bundle. bundle_status . iter ( ) )
657
- . filter ( |( _, & status) | status == ComponentStatus :: Added )
658
- . map ( |( id, _) | id) ,
659
- ) ;
660
- }
661
- }
662
- if archetype. has_on_insert ( ) {
663
- // SAFETY: All components in the bundle are guarenteed to exist in the World
664
- // as they must be initialized before creating the BundleInfo.
665
- unsafe { world. trigger_on_insert ( entity, bundle_info. iter_components ( ) ) }
666
- }
667
- }
668
-
669
640
let bundle_info = self . bundle_info . as_ref ( ) ;
670
641
let add_bundle = self . add_bundle . as_ref ( ) ;
671
642
let table = self . table . as_mut ( ) ;
672
643
let archetype = self . archetype . as_mut ( ) ;
673
644
674
- match & mut self . result {
645
+ let new_location = match & mut self . result {
675
646
InsertBundleResult :: SameArchetype => {
676
- {
677
- // SAFETY: Mutable references do not alias and will be dropped after this block
678
- let sparse_sets = {
679
- let world = self . world . world_mut ( ) ;
680
- & mut world. storages . sparse_sets
681
- } ;
647
+ // SAFETY: Mutable references do not alias and will be dropped after this block
648
+ let sparse_sets = {
649
+ let world = self . world . world_mut ( ) ;
650
+ & mut world. storages . sparse_sets
651
+ } ;
652
+
653
+ bundle_info. write_components (
654
+ table,
655
+ sparse_sets,
656
+ add_bundle,
657
+ entity,
658
+ location. table_row ,
659
+ self . change_tick ,
660
+ bundle,
661
+ ) ;
682
662
683
- bundle_info. write_components (
684
- table,
685
- sparse_sets,
686
- add_bundle,
687
- entity,
688
- location. table_row ,
689
- self . change_tick ,
690
- bundle,
691
- ) ;
692
- }
693
- // SAFETY: We have no outstanding mutable references to world as they were dropped
694
- let deferred_world = unsafe { self . world . into_deferred ( ) } ;
695
- trigger_hooks ( entity, bundle_info, add_bundle, archetype, deferred_world) ;
696
663
location
697
664
}
698
665
InsertBundleResult :: NewArchetypeSameTable { new_archetype } => {
699
666
let new_archetype = new_archetype. as_mut ( ) ;
700
667
701
- let new_location = {
702
- // SAFETY: Mutable references do not alias and will be dropped after this block
703
- let ( sparse_sets, entities) = {
704
- let world = self . world . world_mut ( ) ;
705
- ( & mut world. storages . sparse_sets , & mut world. entities )
706
- } ;
707
-
708
- let result = archetype. swap_remove ( location. archetype_row ) ;
709
- if let Some ( swapped_entity) = result. swapped_entity {
710
- let swapped_location =
711
- // SAFETY: If the swap was successful, swapped_entity must be valid.
712
- unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
713
- entities. set (
714
- swapped_entity. index ( ) ,
715
- EntityLocation {
716
- archetype_id : swapped_location. archetype_id ,
717
- archetype_row : location. archetype_row ,
718
- table_id : swapped_location. table_id ,
719
- table_row : swapped_location. table_row ,
720
- } ,
721
- ) ;
722
- }
723
- let new_location = new_archetype. allocate ( entity, result. table_row ) ;
724
- entities. set ( entity. index ( ) , new_location) ;
725
- bundle_info. write_components (
726
- table,
727
- sparse_sets,
728
- add_bundle,
729
- entity,
730
- result. table_row ,
731
- self . change_tick ,
732
- bundle,
733
- ) ;
734
- new_location
668
+ // SAFETY: Mutable references do not alias and will be dropped after this block
669
+ let ( sparse_sets, entities) = {
670
+ let world = self . world . world_mut ( ) ;
671
+ ( & mut world. storages . sparse_sets , & mut world. entities )
735
672
} ;
736
673
737
- // SAFETY: We have no outstanding mutable references to world as they were dropped
738
- let deferred_world = unsafe { self . world . into_deferred ( ) } ;
739
- trigger_hooks ( entity, bundle_info, add_bundle, archetype, deferred_world) ;
674
+ let result = archetype. swap_remove ( location. archetype_row ) ;
675
+ if let Some ( swapped_entity) = result. swapped_entity {
676
+ let swapped_location =
677
+ // SAFETY: If the swap was successful, swapped_entity must be valid.
678
+ unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
679
+ entities. set (
680
+ swapped_entity. index ( ) ,
681
+ EntityLocation {
682
+ archetype_id : swapped_location. archetype_id ,
683
+ archetype_row : location. archetype_row ,
684
+ table_id : swapped_location. table_id ,
685
+ table_row : swapped_location. table_row ,
686
+ } ,
687
+ ) ;
688
+ }
740
689
690
+ let new_location = new_archetype. allocate ( entity, result. table_row ) ;
691
+ entities. set ( entity. index ( ) , new_location) ;
692
+ bundle_info. write_components (
693
+ table,
694
+ sparse_sets,
695
+ add_bundle,
696
+ entity,
697
+ result. table_row ,
698
+ self . change_tick ,
699
+ bundle,
700
+ ) ;
741
701
new_location
742
702
}
743
703
InsertBundleResult :: NewArchetypeNewTable {
744
704
new_archetype,
745
705
new_table,
746
706
} => {
747
707
let new_table = new_table. as_mut ( ) ;
708
+ let new_archetype = new_archetype. as_mut ( ) ;
748
709
749
- let new_location = {
750
- // SAFETY: Mutable references do not alias and will be dropped after this block
751
- let ( archetypes_ptr, sparse_sets, entities) = {
752
- let world = self . world . world_mut ( ) ;
753
- let archetype_ptr: * mut Archetype =
754
- world. archetypes . archetypes . as_mut_ptr ( ) ;
755
- (
756
- archetype_ptr,
757
- & mut world. storages . sparse_sets ,
758
- & mut world. entities ,
759
- )
760
- } ;
761
- let new_archetype = new_archetype. as_mut ( ) ;
762
- let result = archetype. swap_remove ( location. archetype_row ) ;
763
- if let Some ( swapped_entity) = result. swapped_entity {
764
- let swapped_location =
765
- // SAFETY: If the swap was successful, swapped_entity must be valid.
766
- unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
767
- entities. set (
768
- swapped_entity. index ( ) ,
769
- EntityLocation {
770
- archetype_id : swapped_location. archetype_id ,
771
- archetype_row : location. archetype_row ,
772
- table_id : swapped_location. table_id ,
773
- table_row : swapped_location. table_row ,
774
- } ,
710
+ // SAFETY: Mutable references do not alias and will be dropped after this block
711
+ let ( archetypes_ptr, sparse_sets, entities) = {
712
+ let world = self . world . world_mut ( ) ;
713
+ let archetype_ptr: * mut Archetype =
714
+ world. archetypes . archetypes . as_mut_ptr ( ) ;
715
+ (
716
+ archetype_ptr,
717
+ & mut world. storages . sparse_sets ,
718
+ & mut world. entities ,
719
+ )
720
+ } ;
721
+
722
+ let result = archetype. swap_remove ( location. archetype_row ) ;
723
+ if let Some ( swapped_entity) = result. swapped_entity {
724
+ let swapped_location =
725
+ // SAFETY: If the swap was successful, swapped_entity must be valid.
726
+ unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
727
+ entities. set (
728
+ swapped_entity. index ( ) ,
729
+ EntityLocation {
730
+ archetype_id : swapped_location. archetype_id ,
731
+ archetype_row : location. archetype_row ,
732
+ table_id : swapped_location. table_id ,
733
+ table_row : swapped_location. table_row ,
734
+ } ,
735
+ ) ;
736
+ }
737
+ // PERF: store "non bundle" components in edge, then just move those to avoid
738
+ // redundant copies
739
+ let move_result = table. move_to_superset_unchecked ( result. table_row , new_table) ;
740
+ let new_location = new_archetype. allocate ( entity, move_result. new_row ) ;
741
+ entities. set ( entity. index ( ) , new_location) ;
742
+
743
+ // if an entity was moved into this entity's table spot, update its table row
744
+ if let Some ( swapped_entity) = move_result. swapped_entity {
745
+ let swapped_location =
746
+ // SAFETY: If the swap was successful, swapped_entity must be valid.
747
+ unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
748
+
749
+ entities. set (
750
+ swapped_entity. index ( ) ,
751
+ EntityLocation {
752
+ archetype_id : swapped_location. archetype_id ,
753
+ archetype_row : swapped_location. archetype_row ,
754
+ table_id : swapped_location. table_id ,
755
+ table_row : result. table_row ,
756
+ } ,
757
+ ) ;
758
+
759
+ if archetype. id ( ) == swapped_location. archetype_id {
760
+ archetype. set_entity_table_row (
761
+ swapped_location. archetype_row ,
762
+ result. table_row ,
775
763
) ;
776
- }
777
- // PERF: store "non bundle" components in edge, then just move those to avoid
778
- // redundant copies
779
- let move_result = table. move_to_superset_unchecked ( result. table_row , new_table) ;
780
- let new_location = new_archetype. allocate ( entity, move_result. new_row ) ;
781
- entities. set ( entity. index ( ) , new_location) ;
782
-
783
- // if an entity was moved into this entity's table spot, update its table row
784
- if let Some ( swapped_entity) = move_result. swapped_entity {
785
- let swapped_location =
786
- // SAFETY: If the swap was successful, swapped_entity must be valid.
787
- unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
788
-
789
- entities. set (
790
- swapped_entity. index ( ) ,
791
- EntityLocation {
792
- archetype_id : swapped_location. archetype_id ,
793
- archetype_row : swapped_location. archetype_row ,
794
- table_id : swapped_location. table_id ,
795
- table_row : result. table_row ,
796
- } ,
764
+ } else if new_archetype. id ( ) == swapped_location. archetype_id {
765
+ new_archetype. set_entity_table_row (
766
+ swapped_location. archetype_row ,
767
+ result. table_row ,
797
768
) ;
798
-
799
- if archetype. id ( ) == swapped_location. archetype_id {
800
- archetype. set_entity_table_row (
769
+ } else {
770
+ // SAFETY: the only two borrowed archetypes are above and we just did collision checks
771
+ ( * archetypes_ptr. add ( swapped_location. archetype_id . index ( ) ) )
772
+ . set_entity_table_row (
801
773
swapped_location. archetype_row ,
802
774
result. table_row ,
803
775
) ;
804
- } else if new_archetype. id ( ) == swapped_location. archetype_id {
805
- new_archetype. set_entity_table_row (
806
- swapped_location. archetype_row ,
807
- result. table_row ,
808
- ) ;
809
- } else {
810
- // SAFETY: the only two borrowed archetypes are above and we just did collision checks
811
- ( * archetypes_ptr. add ( swapped_location. archetype_id . index ( ) ) )
812
- . set_entity_table_row (
813
- swapped_location. archetype_row ,
814
- result. table_row ,
815
- ) ;
816
- }
817
776
}
777
+ }
818
778
819
- bundle_info. write_components (
820
- new_table,
821
- sparse_sets,
822
- add_bundle,
823
- entity,
824
- move_result. new_row ,
825
- self . change_tick ,
826
- bundle,
827
- ) ;
828
-
829
- new_location
830
- } ;
831
-
832
- // SAFETY: We have no outstanding mutable references to world as they were dropped
833
- let deferred_world = unsafe { self . world . into_deferred ( ) } ;
834
- trigger_hooks ( entity, bundle_info, add_bundle, archetype, deferred_world) ;
779
+ bundle_info. write_components (
780
+ new_table,
781
+ sparse_sets,
782
+ add_bundle,
783
+ entity,
784
+ move_result. new_row ,
785
+ self . change_tick ,
786
+ bundle,
787
+ ) ;
835
788
836
789
new_location
837
790
}
791
+ } ;
792
+
793
+ // SAFETY: We have no outstanding mutable references to world as they were dropped
794
+ let mut deferred_world = unsafe { self . world . into_deferred ( ) } ;
795
+
796
+ if archetype. has_on_add ( ) {
797
+ // SAFETY: All components in the bundle are guarenteed to exist in the World
798
+ // as they must be initialized before creating the BundleInfo.
799
+ unsafe {
800
+ deferred_world. trigger_on_add (
801
+ entity,
802
+ bundle_info
803
+ . iter_components ( )
804
+ . zip ( add_bundle. bundle_status . iter ( ) )
805
+ . filter ( |( _, & status) | status == ComponentStatus :: Added )
806
+ . map ( |( id, _) | id) ,
807
+ ) ;
808
+ }
838
809
}
810
+ if archetype. has_on_insert ( ) {
811
+ // SAFETY: All components in the bundle are guarenteed to exist in the World
812
+ // as they must be initialized before creating the BundleInfo.
813
+ unsafe { deferred_world. trigger_on_insert ( entity, bundle_info. iter_components ( ) ) }
814
+ }
815
+
816
+ new_location
839
817
}
840
818
841
819
#[ inline]
0 commit comments