Skip to content

Commit

Permalink
Reduce size of low id record array
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 2, 2024
1 parent 6af33d5 commit 5223c54
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 126 deletions.
49 changes: 16 additions & 33 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,7 @@ struct ecs_world_t {
ecs_header_t hdr;

/* -- Type metadata -- */
ecs_id_record_t *id_index_lo;
ecs_id_record_t **id_index_lo;
ecs_map_t id_index_hi; /* map<id, ecs_id_record_t*> */
ecs_map_t type_info; /* map<type_id, type_info_t> */

Expand Down Expand Up @@ -15761,25 +15761,14 @@ static
void* ecs_os_api_malloc(ecs_size_t size) {
ecs_os_linc(&ecs_os_api_malloc_count);
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
void *ptr = malloc((size_t)size);
// if (size > 10000) {
// printf("[%p] malloc(%d)\n", ptr, size);
// flecs_dump_backtrace(stdout);
// }
return ptr;
return malloc((size_t)size);
}

static
void* ecs_os_api_calloc(ecs_size_t size) {
ecs_os_linc(&ecs_os_api_calloc_count);
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
void *ptr = calloc(1, (size_t)size);

// if (size > 10000) {
// printf("[%p] calloc(%d)\n", ptr, size);
// flecs_dump_backtrace(stdout);
// }
return ptr;
return calloc(1, (size_t)size);
}

static
Expand All @@ -15793,19 +15782,13 @@ void* ecs_os_api_realloc(void *ptr, ecs_size_t size) {
ecs_os_linc(&ecs_os_api_malloc_count);
}

void *res = realloc(ptr, (size_t)size);
// if (size > 10000) {
// printf("[%p] realloc(%p, %d)\n", res, ptr, size);
// flecs_dump_backtrace(stdout);
// }
return res;
return realloc(ptr, (size_t)size);
}

static
void ecs_os_api_free(void *ptr) {
if (ptr) {
ecs_os_linc(&ecs_os_api_free_count);
// printf("[%p] free\n");
}
free(ptr);
}
Expand Down Expand Up @@ -18732,7 +18715,8 @@ ecs_world_t *ecs_mini(void) {

ecs_map_init(&world->type_info, a);
ecs_map_init_w_params(&world->id_index_hi, &world->allocators.ptr);
world->id_index_lo = ecs_os_calloc_n(ecs_id_record_t, FLECS_HI_ID_RECORD_ID);
world->id_index_lo = ecs_os_calloc_n(
ecs_id_record_t*, FLECS_HI_ID_RECORD_ID);
flecs_observable_init(&world->observable);

world->pending_tables = ecs_os_calloc_t(ecs_sparse_t);
Expand Down Expand Up @@ -19321,6 +19305,7 @@ void flecs_fini_type_info(
while (ecs_map_next(&it)) {
ecs_type_info_t *ti = ecs_map_ptr(&it);
flecs_type_info_fini(ti);
ecs_os_free(ti);
}
ecs_map_fini(&world->type_info);
}
Expand Down Expand Up @@ -36064,12 +36049,12 @@ ecs_id_record_t* flecs_id_record_new(
{
ecs_id_record_t *idr, *idr_t = NULL;
ecs_id_t hash = flecs_id_record_hash(id);
idr = flecs_bcalloc(&world->allocators.id_record);

if (hash >= FLECS_HI_ID_RECORD_ID) {
idr = flecs_bcalloc(&world->allocators.id_record);
ecs_map_insert_ptr(&world->id_index_hi, hash, idr);
} else {
idr = &world->id_index_lo[hash];
ecs_os_zeromem(idr);
world->id_index_lo[hash] = idr;
}

ecs_table_cache_init(world, &idr->cache);
Expand Down Expand Up @@ -36345,11 +36330,12 @@ void flecs_id_record_free(
ecs_id_t hash = flecs_id_record_hash(id);
if (hash >= FLECS_HI_ID_RECORD_ID) {
ecs_map_remove(&world->id_index_hi, hash);
flecs_bfree(&world->allocators.id_record, idr);
} else {
idr->id = 0; /* Tombstone */
world->id_index_lo[hash] = NULL;
}

flecs_bfree(&world->allocators.id_record, idr);

if (ecs_should_log_1()) {
char *id_str = ecs_id_str(world, id);
ecs_dbg_1("#[green]id#[normal] %s #[red]deleted", id_str);
Expand Down Expand Up @@ -36386,10 +36372,7 @@ ecs_id_record_t* flecs_id_record_get(
if (hash >= FLECS_HI_ID_RECORD_ID) {
idr = ecs_map_get_deref(&world->id_index_hi, ecs_id_record_t, hash);
} else {
idr = &world->id_index_lo[hash];
if (!idr->id) {
idr = NULL;
}
idr = world->id_index_lo[hash];
}

return idr;
Expand Down Expand Up @@ -36544,8 +36527,8 @@ void flecs_fini_id_records(

int32_t i;
for (i = 0; i < FLECS_HI_ID_RECORD_ID; i ++) {
ecs_id_record_t *idr = &world->id_index_lo[i];
if (idr->id) {
ecs_id_record_t *idr = world->id_index_lo[i];
if (idr) {
flecs_id_record_release(world, idr);
}
}
Expand Down
23 changes: 3 additions & 20 deletions src/os_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,14 @@ static
void* ecs_os_api_malloc(ecs_size_t size) {
ecs_os_linc(&ecs_os_api_malloc_count);
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
void *ptr = malloc((size_t)size);
// if (size > 10000) {
// printf("[%p] malloc(%d)\n", ptr, size);
// flecs_dump_backtrace(stdout);
// }
return ptr;
return malloc((size_t)size);
}

static
void* ecs_os_api_calloc(ecs_size_t size) {
ecs_os_linc(&ecs_os_api_calloc_count);
ecs_assert(size > 0, ECS_INVALID_PARAMETER, NULL);
void *ptr = calloc(1, (size_t)size);

// if (size > 10000) {
// printf("[%p] calloc(%d)\n", ptr, size);
// flecs_dump_backtrace(stdout);
// }
return ptr;
return calloc(1, (size_t)size);
}

static
Expand All @@ -331,19 +320,13 @@ void* ecs_os_api_realloc(void *ptr, ecs_size_t size) {
ecs_os_linc(&ecs_os_api_malloc_count);
}

void *res = realloc(ptr, (size_t)size);
// if (size > 10000) {
// printf("[%p] realloc(%p, %d)\n", res, ptr, size);
// flecs_dump_backtrace(stdout);
// }
return res;
return realloc(ptr, (size_t)size);
}

static
void ecs_os_api_free(void *ptr) {
if (ptr) {
ecs_os_linc(&ecs_os_api_free_count);
// printf("[%p] free\n");
}
free(ptr);
}
Expand Down
2 changes: 1 addition & 1 deletion src/private_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ struct ecs_world_t {
ecs_header_t hdr;

/* -- Type metadata -- */
ecs_id_record_t *id_index_lo;
ecs_id_record_t **id_index_lo;
ecs_map_t id_index_hi; /* map<id, ecs_id_record_t*> */
ecs_map_t type_info; /* map<type_id, type_info_t> */

Expand Down
20 changes: 9 additions & 11 deletions src/storage/id_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ ecs_id_record_t* flecs_id_record_new(
{
ecs_id_record_t *idr, *idr_t = NULL;
ecs_id_t hash = flecs_id_record_hash(id);
idr = flecs_bcalloc(&world->allocators.id_record);

if (hash >= FLECS_HI_ID_RECORD_ID) {
idr = flecs_bcalloc(&world->allocators.id_record);
ecs_map_insert_ptr(&world->id_index_hi, hash, idr);
} else {
idr = &world->id_index_lo[hash];
ecs_os_zeromem(idr);
world->id_index_lo[hash] = idr;
}

ecs_table_cache_init(world, &idr->cache);
Expand Down Expand Up @@ -474,11 +474,12 @@ void flecs_id_record_free(
ecs_id_t hash = flecs_id_record_hash(id);
if (hash >= FLECS_HI_ID_RECORD_ID) {
ecs_map_remove(&world->id_index_hi, hash);
flecs_bfree(&world->allocators.id_record, idr);
} else {
idr->id = 0; /* Tombstone */
world->id_index_lo[hash] = NULL;
}

flecs_bfree(&world->allocators.id_record, idr);

if (ecs_should_log_1()) {
char *id_str = ecs_id_str(world, id);
ecs_dbg_1("#[green]id#[normal] %s #[red]deleted", id_str);
Expand Down Expand Up @@ -515,10 +516,7 @@ ecs_id_record_t* flecs_id_record_get(
if (hash >= FLECS_HI_ID_RECORD_ID) {
idr = ecs_map_get_deref(&world->id_index_hi, ecs_id_record_t, hash);
} else {
idr = &world->id_index_lo[hash];
if (!idr->id) {
idr = NULL;
}
idr = world->id_index_lo[hash];
}

return idr;
Expand Down Expand Up @@ -673,8 +671,8 @@ void flecs_fini_id_records(

int32_t i;
for (i = 0; i < FLECS_HI_ID_RECORD_ID; i ++) {
ecs_id_record_t *idr = &world->id_index_lo[i];
if (idr->id) {
ecs_id_record_t *idr = world->id_index_lo[i];
if (idr) {
flecs_id_record_release(world, idr);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,8 @@ ecs_world_t *ecs_mini(void) {

ecs_map_init(&world->type_info, a);
ecs_map_init_w_params(&world->id_index_hi, &world->allocators.ptr);
world->id_index_lo = ecs_os_calloc_n(ecs_id_record_t, FLECS_HI_ID_RECORD_ID);
world->id_index_lo = ecs_os_calloc_n(
ecs_id_record_t*, FLECS_HI_ID_RECORD_ID);
flecs_observable_init(&world->observable);

world->pending_tables = ecs_os_calloc_t(ecs_sparse_t);
Expand Down Expand Up @@ -1543,6 +1544,7 @@ void flecs_fini_type_info(
while (ecs_map_next(&it)) {
ecs_type_info_t *ti = ecs_map_ptr(&it);
flecs_type_info_fini(ti);
ecs_os_free(ti);
}
ecs_map_fini(&world->type_info);
}
Expand Down
1 change: 0 additions & 1 deletion test/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,6 @@
"entity_range_set_limit_to_lower_than_offset",
"entity_range_overlapping_new_id",
"entity_range_overlapping_new_bulk_id",
"dim",
"phases",
"phases_w_merging",
"phases_match_in_create",
Expand Down
53 changes: 0 additions & 53 deletions test/core/src/World.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,59 +371,6 @@ void World_get_tick(void) {
ecs_fini(world);
}

static int32_t malloc_count;

static
void *test_malloc(ecs_size_t size) {
malloc_count ++;
return malloc(size);
}

static
void *test_calloc(ecs_size_t size) {
malloc_count ++;
return calloc(size, 1);
}

static
void *test_realloc(void *old_ptr, ecs_size_t size) {
malloc_count ++;
return realloc(old_ptr, size);
}

void World_dim(void) {
ecs_os_set_api_defaults();
ecs_os_api_t os_api = ecs_os_api;
os_api.malloc_ = test_malloc;
os_api.calloc_ = test_calloc;
os_api.realloc_ = test_realloc;
ecs_os_set_api(&os_api);

ecs_world_t *world = ecs_mini();

ECS_COMPONENT(world, Position);

/* Create single entity so that the table exists. This makes the allocation
* counts more predictable, as new_w_count won't trigger table creation */
ecs_new_w(world, Position);

ecs_dim(world, 1100);

malloc_count = 0;

ecs_bulk_new(world, Position, 500);

test_int(malloc_count, 4);

malloc_count = 0;

ecs_bulk_new(world, Position, 500);

test_int(malloc_count, 2);

ecs_fini(world);
}

static
void TOnLoad(ecs_iter_t *it) {
Position *p = ecs_field(it, Position, 0);
Expand Down
7 changes: 1 addition & 6 deletions test/core/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,6 @@ void World_entity_range_set_limit_to_lower(void);
void World_entity_range_set_limit_to_lower_than_offset(void);
void World_entity_range_overlapping_new_id(void);
void World_entity_range_overlapping_new_bulk_id(void);
void World_dim(void);
void World_phases(void);
void World_phases_w_merging(void);
void World_phases_match_in_create(void);
Expand Down Expand Up @@ -9806,10 +9805,6 @@ bake_test_case World_testcases[] = {
"entity_range_overlapping_new_bulk_id",
World_entity_range_overlapping_new_bulk_id
},
{
"dim",
World_dim
},
{
"phases",
World_phases
Expand Down Expand Up @@ -11569,7 +11564,7 @@ static bake_test_suite suites[] = {
"World",
World_setup,
NULL,
67,
66,
World_testcases
},
{
Expand Down

0 comments on commit 5223c54

Please sign in to comment.