@@ -778,6 +778,9 @@ struct ecs_id_record_t {
778
778
779
779
/* Name lookup index (currently only used for ChildOf pairs) */
780
780
ecs_hashmap_t *name_index;
781
+
782
+ /* Cached pointer to type info for id */
783
+ const ecs_type_info_t *type_info;
781
784
};
782
785
783
786
typedef struct ecs_store_t {
@@ -1394,6 +1397,12 @@ ecs_id_record_t* flecs_ensure_id_record(
1394
1397
ecs_world_t *world,
1395
1398
ecs_id_t id);
1396
1399
1400
+ void flecs_register_for_id_record(
1401
+ ecs_world_t *world,
1402
+ ecs_id_t id,
1403
+ const ecs_table_t *table,
1404
+ ecs_table_record_t *tr);
1405
+
1397
1406
ecs_id_record_t* flecs_get_id_record(
1398
1407
const ecs_world_t *world,
1399
1408
ecs_id_t id);
@@ -2306,29 +2315,27 @@ void init_storage_table(
2306
2315
if (table->storage_table) {
2307
2316
return;
2308
2317
}
2309
-
2318
+
2310
2319
int32_t i, count = ecs_vector_count(table->type);
2311
2320
ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t);
2321
+ ecs_table_record_t *records = table->records;
2312
2322
ecs_ids_t storage_ids = {
2313
2323
.array = ecs_os_alloca_n(ecs_id_t, count)
2314
2324
};
2315
2325
2316
2326
for (i = 0; i < count; i ++) {
2317
- ecs_id_t id = ids[i];
2318
-
2319
- if ((id == ecs_id(EcsComponent)) ||
2320
- (ECS_PAIR_FIRST(id) == ecs_id(EcsIdentifier)))
2321
- {
2322
- storage_ids.array[storage_ids.count ++] = id;
2323
- continue;
2324
- }
2327
+ ecs_table_record_t *tr = &records[i];
2328
+ ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
2329
+ ecs_assert(idr->flags & ECS_TYPE_INFO_INITIALIZED,
2330
+ ECS_INTERNAL_ERROR, NULL);
2325
2331
2326
- const EcsComponent *comp = flecs_component_from_id(world, id);
2327
- if (!comp || !comp->size) {
2328
- continue;
2332
+ if (idr->type_info == NULL) {
2333
+ ecs_assert(ecs_get_typeid(world, ids[i]) == 0,
2334
+ ECS_INTERNAL_ERROR, NULL);
2335
+ continue; /* not a component */
2329
2336
}
2330
2337
2331
- storage_ids.array[storage_ids.count ++] = id ;
2338
+ storage_ids.array[storage_ids.count ++] = ids[i] ;
2332
2339
}
2333
2340
2334
2341
if (storage_ids.count && storage_ids.count != count) {
@@ -2374,7 +2381,6 @@ ecs_flags32_t type_info_flags(
2374
2381
2375
2382
static
2376
2383
void init_type_info(
2377
- ecs_world_t *world,
2378
2384
ecs_table_t *table)
2379
2385
{
2380
2386
ecs_table_t *storage_table = table->storage_table;
@@ -2390,20 +2396,18 @@ void init_type_info(
2390
2396
return;
2391
2397
}
2392
2398
2393
- ecs_type_t type = table->storage_type;
2394
- ecs_assert(type != NULL, ECS_INTERNAL_ERROR, NULL);
2395
-
2396
- ecs_id_t *ids = ecs_vector_first(type, ecs_id_t);
2397
- int32_t i, count = ecs_vector_count(type);
2398
-
2399
+ ecs_table_record_t *records = table->records;
2400
+ int32_t i, count = ecs_vector_count(table->type);
2399
2401
table->type_info = ecs_os_calloc_n(ecs_type_info_t, count);
2400
2402
2401
2403
for (i = 0; i < count; i ++) {
2402
- ecs_id_t id = ids[i];
2403
- ecs_entity_t t = ecs_get_typeid(world, id);
2404
-
2405
- /* Component type info must have been registered before using it */
2406
- const ecs_type_info_t *ti = flecs_get_type_info(world, t);
2404
+ ecs_table_record_t *tr = &records[i];
2405
+ ecs_id_record_t *idr = (ecs_id_record_t*)tr->hdr.cache;
2406
+ ecs_assert(idr->flags & ECS_TYPE_INFO_INITIALIZED,
2407
+ ECS_INTERNAL_ERROR, NULL);
2408
+
2409
+ /* All ids in the storage table must be components with type info */
2410
+ const ecs_type_info_t *ti = idr->type_info;
2407
2411
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);
2408
2412
table->flags |= type_info_flags(ti);
2409
2413
table->type_info[i] = *ti;
@@ -2415,7 +2419,7 @@ void flecs_table_init_data(
2415
2419
ecs_table_t *table)
2416
2420
{
2417
2421
init_storage_table(world, table);
2418
- init_type_info(world, table);
2422
+ init_type_info(table);
2419
2423
2420
2424
int32_t sw_count = table->sw_column_count = switch_column_count(table);
2421
2425
int32_t bs_count = table->bs_column_count = bitset_column_count(table);
@@ -2431,29 +2435,13 @@ void flecs_table_init_data(
2431
2435
}
2432
2436
2433
2437
if (count) {
2434
- ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t);
2435
2438
storage->columns = ecs_os_calloc_n(ecs_column_t, count);
2439
+ ecs_type_info_t *type_info = table->type_info;
2440
+ ecs_column_t *columns = storage->columns;
2436
2441
2437
2442
for (i = 0; i < count; i ++) {
2438
- ecs_entity_t id = ids[i];
2439
-
2440
- /* Bootstrap components */
2441
- if (id == ecs_id(EcsComponent)) {
2442
- storage->columns[i].size = ECS_SIZEOF(EcsComponent);
2443
- storage->columns[i].alignment = ECS_ALIGNOF(EcsComponent);
2444
- continue;
2445
- } else if (ECS_PAIR_FIRST(id) == ecs_id(EcsIdentifier)) {
2446
- storage->columns[i].size = ECS_SIZEOF(EcsIdentifier);
2447
- storage->columns[i].alignment = ECS_ALIGNOF(EcsIdentifier);
2448
- continue;
2449
- }
2450
-
2451
- const EcsComponent *component = flecs_component_from_id(world, id);
2452
- ecs_assert(component != NULL, ECS_INTERNAL_ERROR, NULL);
2453
- ecs_assert(component->size != 0, ECS_INTERNAL_ERROR, NULL);
2454
-
2455
- storage->columns[i].size = flecs_itoi16(component->size);
2456
- storage->columns[i].alignment = flecs_itoi16(component->alignment);
2443
+ columns[i].size = flecs_itoi16(type_info[i].size);
2444
+ columns[i].alignment = flecs_itoi16(type_info[i].alignment);
2457
2445
}
2458
2446
}
2459
2447
@@ -35198,7 +35186,7 @@ ecs_id_record_t* new_id_record(
35198
35186
ecs_id_record_t *idr_r = flecs_get_id_record(
35199
35187
world, ECS_PAIR_FIRST(id));
35200
35188
if (idr_r) {
35201
- idr->flags = idr_r->flags;
35189
+ idr->flags = ( idr_r->flags & ~ECS_TYPE_INFO_INITIALIZED) ;
35202
35190
}
35203
35191
} else {
35204
35192
rel = id & ECS_COMPONENT_MASK;
@@ -35910,6 +35898,26 @@ ecs_id_record_t* flecs_ensure_id_record(
35910
35898
return idr;
35911
35899
}
35912
35900
35901
+ void flecs_register_for_id_record(
35902
+ ecs_world_t *world,
35903
+ ecs_id_t id,
35904
+ const ecs_table_t *table,
35905
+ ecs_table_record_t *tr)
35906
+ {
35907
+ ecs_id_record_t *idr = flecs_ensure_id_record(world, id);
35908
+ ecs_table_cache_insert(&idr->cache, table, &tr->hdr);
35909
+
35910
+ /* When id record is used by table, make sure type info is initialized */
35911
+ if (!(idr->flags & ECS_TYPE_INFO_INITIALIZED)) {
35912
+ ecs_entity_t type = ecs_get_typeid(world, id);
35913
+ if (type) {
35914
+ idr->type_info = flecs_get_type_info(world, type);
35915
+ ecs_assert(idr->type_info != NULL, ECS_INTERNAL_ERROR, NULL);
35916
+ }
35917
+ idr->flags |= ECS_TYPE_INFO_INITIALIZED;
35918
+ }
35919
+ }
35920
+
35913
35921
ecs_id_record_t* flecs_get_id_record(
35914
35922
const ecs_world_t *world,
35915
35923
ecs_id_t id)
@@ -43312,10 +43320,7 @@ void register_table_for_id(
43312
43320
int32_t count,
43313
43321
ecs_table_record_t *tr)
43314
43322
{
43315
- id = ecs_strip_generation(id);
43316
-
43317
- ecs_id_record_t *idr = flecs_ensure_id_record(world, id);
43318
- ecs_table_cache_insert(&idr->cache, table, &tr->hdr);
43323
+ flecs_register_for_id_record(world, id, table, tr);
43319
43324
tr->column = column;
43320
43325
tr->count = count;
43321
43326
tr->id = id;
@@ -43390,7 +43395,6 @@ void flecs_table_records_register(
43390
43395
int32_t record_count = count + type_flag_count + (id_count != 0) +
43391
43396
(pair_count != 0) + ecs_map_count(&relations) + ecs_map_count(&objects)
43392
43397
+ 1 /* for any */;
43393
- int32_t r = 0;
43394
43398
43395
43399
if (!has_childof) {
43396
43400
record_count ++;
@@ -43399,18 +43403,31 @@ void flecs_table_records_register(
43399
43403
table->records = ecs_os_calloc_n(ecs_table_record_t, record_count);
43400
43404
table->record_count = record_count;
43401
43405
43402
- /* First initialize records for regular (non-wildcard) ids */
43406
+ /* First initialize records for regular (non-wildcard) ids. Make sure that
43407
+ * these table records line up with ids in table type. */
43408
+ int32_t first_role_id = -1;
43403
43409
for (i = 0; i < count; i ++) {
43404
- ecs_id_t id = ids[i];
43405
- register_table_for_id(world, table, id, i, 1, &table->records[r]);
43406
- r ++;
43407
-
43408
- ecs_entity_t role = id & ECS_ROLE_MASK;
43409
- if (role && role != ECS_PAIR) {
43410
- id &= ECS_COMPONENT_MASK;
43411
- id = ecs_pair(id, EcsWildcard);
43412
- register_table_for_id(world, table, id, i, 1, &table->records[r]);
43413
- r ++;
43410
+ register_table_for_id(world, table, ids[i], i, 1, &table->records[i]);
43411
+ if (first_role_id == -1) {
43412
+ ecs_entity_t role = ids[i] & ECS_ROLE_MASK;
43413
+ if (role && role != ECS_PAIR) {
43414
+ first_role_id = i;
43415
+ }
43416
+ }
43417
+ }
43418
+
43419
+ /* Initialize records for ids with roles */
43420
+ int32_t r = i;
43421
+ if (first_role_id != -1) {
43422
+ for (i = first_role_id; i < count; i ++) {
43423
+ ecs_id_t id = ids[i];
43424
+ ecs_entity_t role = id & ECS_ROLE_MASK;
43425
+ if (role && role != ECS_PAIR) {
43426
+ id &= ECS_COMPONENT_MASK;
43427
+ id = ecs_pair(id, EcsWildcard);
43428
+ register_table_for_id(world, table, id, i, 1, &table->records[r]);
43429
+ r ++;
43430
+ }
43414
43431
}
43415
43432
}
43416
43433
0 commit comments