Skip to content

Commit 2797662

Browse files
committed
#680 remove type info lookups from table creation
1 parent 2d40ed7 commit 2797662

File tree

8 files changed

+162
-126
lines changed

8 files changed

+162
-126
lines changed

flecs.c

Lines changed: 80 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ struct ecs_id_record_t {
778778

779779
/* Name lookup index (currently only used for ChildOf pairs) */
780780
ecs_hashmap_t *name_index;
781+
782+
/* Cached pointer to type info for id */
783+
const ecs_type_info_t *type_info;
781784
};
782785

783786
typedef struct ecs_store_t {
@@ -1394,6 +1397,12 @@ ecs_id_record_t* flecs_ensure_id_record(
13941397
ecs_world_t *world,
13951398
ecs_id_t id);
13961399

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+
13971406
ecs_id_record_t* flecs_get_id_record(
13981407
const ecs_world_t *world,
13991408
ecs_id_t id);
@@ -2306,29 +2315,27 @@ void init_storage_table(
23062315
if (table->storage_table) {
23072316
return;
23082317
}
2309-
2318+
23102319
int32_t i, count = ecs_vector_count(table->type);
23112320
ecs_id_t *ids = ecs_vector_first(table->type, ecs_id_t);
2321+
ecs_table_record_t *records = table->records;
23122322
ecs_ids_t storage_ids = {
23132323
.array = ecs_os_alloca_n(ecs_id_t, count)
23142324
};
23152325

23162326
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);
23252331

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 */
23292336
}
23302337

2331-
storage_ids.array[storage_ids.count ++] = id;
2338+
storage_ids.array[storage_ids.count ++] = ids[i];
23322339
}
23332340

23342341
if (storage_ids.count && storage_ids.count != count) {
@@ -2374,7 +2381,6 @@ ecs_flags32_t type_info_flags(
23742381

23752382
static
23762383
void init_type_info(
2377-
ecs_world_t *world,
23782384
ecs_table_t *table)
23792385
{
23802386
ecs_table_t *storage_table = table->storage_table;
@@ -2390,20 +2396,18 @@ void init_type_info(
23902396
return;
23912397
}
23922398

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);
23992401
table->type_info = ecs_os_calloc_n(ecs_type_info_t, count);
24002402

24012403
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;
24072411
ecs_assert(ti != NULL, ECS_INTERNAL_ERROR, NULL);
24082412
table->flags |= type_info_flags(ti);
24092413
table->type_info[i] = *ti;
@@ -2415,7 +2419,7 @@ void flecs_table_init_data(
24152419
ecs_table_t *table)
24162420
{
24172421
init_storage_table(world, table);
2418-
init_type_info(world, table);
2422+
init_type_info(table);
24192423

24202424
int32_t sw_count = table->sw_column_count = switch_column_count(table);
24212425
int32_t bs_count = table->bs_column_count = bitset_column_count(table);
@@ -2431,29 +2435,13 @@ void flecs_table_init_data(
24312435
}
24322436

24332437
if (count) {
2434-
ecs_entity_t *ids = ecs_vector_first(type, ecs_entity_t);
24352438
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;
24362441

24372442
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);
24572445
}
24582446
}
24592447

@@ -35198,7 +35186,7 @@ ecs_id_record_t* new_id_record(
3519835186
ecs_id_record_t *idr_r = flecs_get_id_record(
3519935187
world, ECS_PAIR_FIRST(id));
3520035188
if (idr_r) {
35201-
idr->flags = idr_r->flags;
35189+
idr->flags = (idr_r->flags & ~ECS_TYPE_INFO_INITIALIZED);
3520235190
}
3520335191
} else {
3520435192
rel = id & ECS_COMPONENT_MASK;
@@ -35910,6 +35898,26 @@ ecs_id_record_t* flecs_ensure_id_record(
3591035898
return idr;
3591135899
}
3591235900

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+
3591335921
ecs_id_record_t* flecs_get_id_record(
3591435922
const ecs_world_t *world,
3591535923
ecs_id_t id)
@@ -43312,10 +43320,7 @@ void register_table_for_id(
4331243320
int32_t count,
4331343321
ecs_table_record_t *tr)
4331443322
{
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);
4331943324
tr->column = column;
4332043325
tr->count = count;
4332143326
tr->id = id;
@@ -43390,7 +43395,6 @@ void flecs_table_records_register(
4339043395
int32_t record_count = count + type_flag_count + (id_count != 0) +
4339143396
(pair_count != 0) + ecs_map_count(&relations) + ecs_map_count(&objects)
4339243397
+ 1 /* for any */;
43393-
int32_t r = 0;
4339443398

4339543399
if (!has_childof) {
4339643400
record_count ++;
@@ -43399,18 +43403,31 @@ void flecs_table_records_register(
4339943403
table->records = ecs_os_calloc_n(ecs_table_record_t, record_count);
4340043404
table->record_count = record_count;
4340143405

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;
4340343409
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+
}
4341443431
}
4341543432
}
4341643433

flecs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ typedef int32_t ecs_size_t;
354354

355355
#define ECS_ID_EXCLUSIVE (1u << 6)
356356
#define ECS_ID_DONT_INHERIT (1u << 7)
357+
#define ECS_TYPE_INFO_INITIALIZED (1u << 8)
357358

358359
/* Utilities for converting from flags to delete policies and vice versa */
359360
#define ECS_ID_ON_DELETE(flags) \

include/flecs/private/api_defines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ typedef int32_t ecs_size_t;
197197

198198
#define ECS_ID_EXCLUSIVE (1u << 6)
199199
#define ECS_ID_DONT_INHERIT (1u << 7)
200+
#define ECS_TYPE_INFO_INITIALIZED (1u << 8)
200201

201202
/* Utilities for converting from flags to delete policies and vice versa */
202203
#define ECS_ID_ON_DELETE(flags) \

src/private_api.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ ecs_id_record_t* flecs_ensure_id_record(
139139
ecs_world_t *world,
140140
ecs_id_t id);
141141

142+
void flecs_register_for_id_record(
143+
ecs_world_t *world,
144+
ecs_id_t id,
145+
const ecs_table_t *table,
146+
ecs_table_record_t *tr);
147+
142148
ecs_id_record_t* flecs_get_id_record(
143149
const ecs_world_t *world,
144150
ecs_id_t id);

src/private_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ struct ecs_id_record_t {
506506

507507
/* Name lookup index (currently only used for ChildOf pairs) */
508508
ecs_hashmap_t *name_index;
509+
510+
/* Cached pointer to type info for id */
511+
const ecs_type_info_t *type_info;
509512
};
510513

511514
typedef struct ecs_store_t {

0 commit comments

Comments
 (0)