@@ -120,8 +120,6 @@ pub struct Archetype {
120
120
entities : Vec < Entity > ,
121
121
edges : Edges ,
122
122
table_info : TableInfo ,
123
- table_components : Box < [ ComponentId ] > ,
124
- sparse_set_components : Box < [ ComponentId ] > ,
125
123
pub ( crate ) unique_components : SparseSet < ComponentId , Column > ,
126
124
pub ( crate ) components : SparseSet < ComponentId , ArchetypeComponentInfo > ,
127
125
}
@@ -130,31 +128,25 @@ impl Archetype {
130
128
pub fn new (
131
129
id : ArchetypeId ,
132
130
table_id : TableId ,
133
- table_components : Box < [ ComponentId ] > ,
134
- sparse_set_components : Box < [ ComponentId ] > ,
135
- table_archetype_components : Vec < ArchetypeComponentId > ,
136
- sparse_set_archetype_components : Vec < ArchetypeComponentId > ,
131
+ table_components : impl Iterator < Item = ( ComponentId , ArchetypeComponentId ) > ,
132
+ sparse_set_components : impl Iterator < Item = ( ComponentId , ArchetypeComponentId ) > ,
137
133
) -> Self {
138
- let mut components =
139
- SparseSet :: with_capacity ( table_components. len ( ) + sparse_set_components. len ( ) ) ;
140
- for ( component_id, archetype_component_id) in
141
- table_components. iter ( ) . zip ( table_archetype_components)
142
- {
134
+ let ( min_table, _) = table_components. size_hint ( ) ;
135
+ let ( min_sparse, _) = table_components. size_hint ( ) ;
136
+ let mut components = SparseSet :: with_capacity ( min_table + min_sparse) ;
137
+ for ( component_id, archetype_component_id) in table_components {
143
138
components. insert (
144
- * component_id,
139
+ component_id,
145
140
ArchetypeComponentInfo {
146
141
storage_type : StorageType :: Table ,
147
142
archetype_component_id,
148
143
} ,
149
144
) ;
150
145
}
151
146
152
- for ( component_id, archetype_component_id) in sparse_set_components
153
- . iter ( )
154
- . zip ( sparse_set_archetype_components)
155
- {
147
+ for ( component_id, archetype_component_id) in sparse_set_components {
156
148
components. insert (
157
- * component_id,
149
+ component_id,
158
150
ArchetypeComponentInfo {
159
151
storage_type : StorageType :: SparseSet ,
160
152
archetype_component_id,
@@ -168,8 +160,6 @@ impl Archetype {
168
160
entity_rows : Default :: default ( ) ,
169
161
} ,
170
162
components,
171
- table_components,
172
- sparse_set_components,
173
163
unique_components : SparseSet :: new ( ) ,
174
164
entities : Default :: default ( ) ,
175
165
edges : Default :: default ( ) ,
@@ -197,13 +187,19 @@ impl Archetype {
197
187
}
198
188
199
189
#[ inline]
200
- pub fn table_components ( & self ) -> & [ ComponentId ] {
201
- & self . table_components
190
+ pub fn table_components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
191
+ self . components
192
+ . iter ( )
193
+ . filter ( |( _, component) | component. storage_type == StorageType :: Table )
194
+ . map ( |( id, _) | * id)
202
195
}
203
196
204
197
#[ inline]
205
- pub fn sparse_set_components ( & self ) -> & [ ComponentId ] {
206
- & self . sparse_set_components
198
+ pub fn sparse_set_components ( & self ) -> impl Iterator < Item = ComponentId > + ' _ {
199
+ self . components
200
+ . iter ( )
201
+ . filter ( |( _, component) | component. storage_type == StorageType :: SparseSet )
202
+ . map ( |( id, _) | * id)
207
203
}
208
204
209
205
#[ inline]
@@ -380,10 +376,8 @@ impl Default for Archetypes {
380
376
archetypes. archetypes . push ( Archetype :: new (
381
377
ArchetypeId :: RESOURCE ,
382
378
TableId :: empty ( ) ,
383
- Box :: new ( [ ] ) ,
384
- Box :: new ( [ ] ) ,
385
- Vec :: new ( ) ,
386
- Vec :: new ( ) ,
379
+ std:: iter:: empty ( ) ,
380
+ std:: iter:: empty ( ) ,
387
381
) ) ;
388
382
archetypes
389
383
}
@@ -476,38 +470,33 @@ impl Archetypes {
476
470
table_components : Vec < ComponentId > ,
477
471
sparse_set_components : Vec < ComponentId > ,
478
472
) -> ArchetypeId {
479
- let table_components = table_components. into_boxed_slice ( ) ;
480
- let sparse_set_components = sparse_set_components. into_boxed_slice ( ) ;
481
473
let archetype_identity = ArchetypeIdentity {
482
- sparse_set_components : sparse_set_components. clone ( ) ,
483
- table_components : table_components. clone ( ) ,
474
+ sparse_set_components : sparse_set_components. clone ( ) . into_boxed_slice ( ) ,
475
+ table_components : table_components. clone ( ) . into_boxed_slice ( ) ,
484
476
} ;
485
477
486
478
let archetypes = & mut self . archetypes ;
487
479
let archetype_component_count = & mut self . archetype_component_count ;
488
- let mut next_archetype_component_id = move || {
489
- let id = ArchetypeComponentId ( * archetype_component_count) ;
490
- * archetype_component_count += 1 ;
491
- id
492
- } ;
493
480
* self
494
481
. archetype_ids
495
482
. entry ( archetype_identity)
496
483
. or_insert_with ( move || {
497
484
let id = ArchetypeId ( archetypes. len ( ) ) ;
498
- let table_archetype_components = ( 0 ..table_components. len ( ) )
499
- . map ( |_| next_archetype_component_id ( ) )
500
- . collect ( ) ;
501
- let sparse_set_archetype_components = ( 0 ..sparse_set_components. len ( ) )
502
- . map ( |_| next_archetype_component_id ( ) )
503
- . collect ( ) ;
485
+ let table_start = * archetype_component_count;
486
+ * archetype_component_count += table_components. len ( ) ;
487
+ let table_archetype_components =
488
+ ( table_start..* archetype_component_count) . map ( ArchetypeComponentId ) ;
489
+ let sparse_start = * archetype_component_count;
490
+ * archetype_component_count += sparse_set_components. len ( ) ;
491
+ let sparse_set_archetype_components =
492
+ ( sparse_start..* archetype_component_count) . map ( ArchetypeComponentId ) ;
504
493
archetypes. push ( Archetype :: new (
505
494
id,
506
495
table_id,
507
- table_components,
508
- sparse_set_components,
509
- table_archetype_components ,
510
- sparse_set_archetype_components,
496
+ table_components. into_iter ( ) . zip ( table_archetype_components ) ,
497
+ sparse_set_components
498
+ . into_iter ( )
499
+ . zip ( sparse_set_archetype_components) ,
511
500
) ) ;
512
501
id
513
502
} )
0 commit comments