Skip to content

Commit

Permalink
Don't emit unnecessary TableCreate/TableDelete events
Browse files Browse the repository at this point in the history
SanderMertens committed May 25, 2023

Verified

This commit was signed with the committer’s verified signature.
SanderMertens Sander Mertens
1 parent 3a0b85e commit 49a5ad4
Showing 7 changed files with 74 additions and 32 deletions.
46 changes: 31 additions & 15 deletions flecs.c
Original file line number Diff line number Diff line change
@@ -3287,6 +3287,16 @@ void flecs_table_init(

flecs_table_init_storage_table(world, table);
flecs_table_init_data(world, table);

if (table->flags & EcsTableHasOnTableCreate) {
flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &table->type,
.event = EcsOnTableCreate,
.table = table,
.flags = EcsEventTableOnly,
.observable = world
});
}
}

static
@@ -3698,13 +3708,15 @@ void flecs_table_free(
ecs_assert(table->refcount == 0, ECS_INTERNAL_ERROR, NULL);

if (!is_root && !(world->flags & EcsWorldQuit)) {
flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &table->type,
.event = EcsOnTableDelete,
.table = table,
.flags = EcsEventTableOnly,
.observable = world
});
if (table->flags & EcsTableHasOnTableDelete) {
flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &table->type,
.event = EcsOnTableDelete,
.table = table,
.flags = EcsEventTableOnly,
.observable = world
});
}
}

if (ecs_should_log_2()) {
@@ -51554,6 +51566,12 @@ ecs_flags32_t flecs_id_flag_for_event(
if (e == EcsOnTableEmpty) {
return EcsIdHasOnTableEmpty;
}
if (e == EcsOnTableCreate) {
return EcsIdHasOnTableCreate;
}
if (e == EcsOnTableDelete) {
return EcsIdHasOnTableDelete;
}
return 0;
}

@@ -56573,14 +56591,6 @@ ecs_table_t *flecs_create_table(

flecs_init_table(world, result, prev);

flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &result->type,
.event = EcsOnTableCreate,
.table = result,
.flags = EcsEventTableOnly,
.observable = world
});

/* Update counters */
world->info.table_count ++;
world->info.table_record_count += result->record_count;
@@ -60757,6 +60767,12 @@ ecs_id_record_t* flecs_id_record_new(
if (flecs_check_observers_for_event(world, id, EcsOnTableEmpty)) {
idr->flags |= EcsIdHasOnTableEmpty;
}
if (flecs_check_observers_for_event(world, id, EcsOnTableCreate)) {
idr->flags |= EcsIdHasOnTableCreate;
}
if (flecs_check_observers_for_event(world, id, EcsOnTableDelete)) {
idr->flags |= EcsIdHasOnTableDelete;
}

if (ecs_should_log_1()) {
char *id_str = ecs_id_str(world, id);
7 changes: 6 additions & 1 deletion flecs.h
Original file line number Diff line number Diff line change
@@ -335,9 +335,12 @@ extern "C" {
#define EcsIdHasUnSet (1u << 19)
#define EcsIdHasOnTableFill (1u << 20)
#define EcsIdHasOnTableEmpty (1u << 21)
#define EcsIdHasOnTableCreate (1u << 22)
#define EcsIdHasOnTableDelete (1u << 23)
#define EcsIdEventMask\
(EcsIdHasOnAdd|EcsIdHasOnRemove|EcsIdHasOnSet|EcsIdHasUnSet|\
EcsIdHasOnTableFill|EcsIdHasOnTableEmpty)
EcsIdHasOnTableFill|EcsIdHasOnTableEmpty|EcsIdHasOnTableCreate|\
EcsIdHasOnTableDelete)

#define EcsIdMarkedForDelete (1u << 30)

@@ -416,6 +419,8 @@ extern "C" {
#define EcsTableHasUnSet (1u << 19u)
#define EcsTableHasOnTableFill (1u << 20u)
#define EcsTableHasOnTableEmpty (1u << 21u)
#define EcsTableHasOnTableCreate (1u << 22u)
#define EcsTableHasOnTableDelete (1u << 23u)

#define EcsTableHasObserved (1u << 25u)
#define EcsTableHasTarget (1u << 26u)
7 changes: 6 additions & 1 deletion include/flecs/private/api_flags.h
Original file line number Diff line number Diff line change
@@ -75,9 +75,12 @@ extern "C" {
#define EcsIdHasUnSet (1u << 19)
#define EcsIdHasOnTableFill (1u << 20)
#define EcsIdHasOnTableEmpty (1u << 21)
#define EcsIdHasOnTableCreate (1u << 22)
#define EcsIdHasOnTableDelete (1u << 23)
#define EcsIdEventMask\
(EcsIdHasOnAdd|EcsIdHasOnRemove|EcsIdHasOnSet|EcsIdHasUnSet|\
EcsIdHasOnTableFill|EcsIdHasOnTableEmpty)
EcsIdHasOnTableFill|EcsIdHasOnTableEmpty|EcsIdHasOnTableCreate|\
EcsIdHasOnTableDelete)

#define EcsIdMarkedForDelete (1u << 30)

@@ -156,6 +159,8 @@ extern "C" {
#define EcsTableHasUnSet (1u << 19u)
#define EcsTableHasOnTableFill (1u << 20u)
#define EcsTableHasOnTableEmpty (1u << 21u)
#define EcsTableHasOnTableCreate (1u << 22u)
#define EcsTableHasOnTableDelete (1u << 23u)

#define EcsTableHasObserved (1u << 25u)
#define EcsTableHasTarget (1u << 26u)
6 changes: 6 additions & 0 deletions src/id_record.c
Original file line number Diff line number Diff line change
@@ -256,6 +256,12 @@ ecs_id_record_t* flecs_id_record_new(
if (flecs_check_observers_for_event(world, id, EcsOnTableEmpty)) {
idr->flags |= EcsIdHasOnTableEmpty;
}
if (flecs_check_observers_for_event(world, id, EcsOnTableCreate)) {
idr->flags |= EcsIdHasOnTableCreate;
}
if (flecs_check_observers_for_event(world, id, EcsOnTableDelete)) {
idr->flags |= EcsIdHasOnTableDelete;
}

if (ecs_should_log_1()) {
char *id_str = ecs_id_str(world, id);
6 changes: 6 additions & 0 deletions src/observer.c
Original file line number Diff line number Diff line change
@@ -50,6 +50,12 @@ ecs_flags32_t flecs_id_flag_for_event(
if (e == EcsOnTableEmpty) {
return EcsIdHasOnTableEmpty;
}
if (e == EcsOnTableCreate) {
return EcsIdHasOnTableCreate;
}
if (e == EcsOnTableDelete) {
return EcsIdHasOnTableDelete;
}
return 0;
}

26 changes: 19 additions & 7 deletions src/table.c
Original file line number Diff line number Diff line change
@@ -673,6 +673,16 @@ void flecs_table_init(

flecs_table_init_storage_table(world, table);
flecs_table_init_data(world, table);

if (table->flags & EcsTableHasOnTableCreate) {
flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &table->type,
.event = EcsOnTableCreate,
.table = table,
.flags = EcsEventTableOnly,
.observable = world
});
}
}

static
@@ -1084,13 +1094,15 @@ void flecs_table_free(
ecs_assert(table->refcount == 0, ECS_INTERNAL_ERROR, NULL);

if (!is_root && !(world->flags & EcsWorldQuit)) {
flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &table->type,
.event = EcsOnTableDelete,
.table = table,
.flags = EcsEventTableOnly,
.observable = world
});
if (table->flags & EcsTableHasOnTableDelete) {
flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &table->type,
.event = EcsOnTableDelete,
.table = table,
.flags = EcsEventTableOnly,
.observable = world
});
}
}

if (ecs_should_log_2()) {
8 changes: 0 additions & 8 deletions src/table_graph.c
Original file line number Diff line number Diff line change
@@ -560,14 +560,6 @@ ecs_table_t *flecs_create_table(

flecs_init_table(world, result, prev);

flecs_emit(world, world, &(ecs_event_desc_t) {
.ids = &result->type,
.event = EcsOnTableCreate,
.table = result,
.flags = EcsEventTableOnly,
.observable = world
});

/* Update counters */
world->info.table_count ++;
world->info.table_record_count += result->record_count;

0 comments on commit 49a5ad4

Please sign in to comment.