From 7cb2b480b3e126a287adb9eba3f9b7951c015132 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 13 May 2021 15:44:08 +0200 Subject: [PATCH] do incremental entity cleanup --- code/game/header/profiler.h | 2 ++ code/game/source/debug_ui.c | 2 ++ code/game/source/game.c | 37 +++++++++++++++++++++++----------- code/game/source/profiler.c | 2 ++ code/game/source/world/world.c | 4 +++- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/code/game/header/profiler.h b/code/game/header/profiler.h index bff7e69..226464f 100644 --- a/code/game/header/profiler.h +++ b/code/game/header/profiler.h @@ -5,7 +5,9 @@ typedef enum { PROF_MAIN_LOOP, PROF_WORLD_WRITE, PROF_RENDER, + PROF_UPDATE_SYSTEMS, PROF_ENTITY_LERP, + PROF_ENTITY_REMOVAL, MAX_PROF, PROF_FORCE_UINT8 = UINT8_MAX diff --git a/code/game/source/debug_ui.c b/code/game/source/debug_ui.c index b3ffb1f..59ddf3a 100644 --- a/code/game/source/debug_ui.c +++ b/code/game/source/debug_ui.c @@ -86,7 +86,9 @@ static debug_item items[] = { { .kind = DITEM_RAW, .val = PROF_MAIN_LOOP, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_WORLD_WRITE, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_RENDER, .proc = DrawProfilerDelta }, + { .kind = DITEM_RAW, .val = PROF_UPDATE_SYSTEMS, .proc = DrawProfilerDelta }, { .kind = DITEM_RAW, .val = PROF_ENTITY_LERP, .proc = DrawProfilerDelta }, + { .kind = DITEM_RAW, .val = PROF_ENTITY_REMOVAL, .proc = DrawProfilerDelta }, { .kind = DITEM_END }, }, //.is_collapsed = 1 diff --git a/code/game/source/game.c b/code/game/source/game.c index 7223d9e..d2922eb 100644 --- a/code/game/source/game.c +++ b/code/game/source/game.c @@ -9,6 +9,7 @@ #include "world_view.h" #include "entity_view.h" #include "camera.h" +#include "profiler.h" #include "flecs/flecs.h" #include "flecs/flecs_dash.h" @@ -156,7 +157,7 @@ void game_update() { network_client_tick(); } else world_update(); - + game_world_cleanup_entities(); } @@ -168,19 +169,31 @@ void game_action_send_keystate(float x, float y, uint8_t use, uint8_t sprint) { pkt_send_keystate_send(active_viewer->view_id, x, y, use, sprint); } +#define GAME_ENT_REMOVAL_TIME 10000 +#define GAME_ENT_REMOVAL_TRESHOLD 500 + void game_world_cleanup_entities(void) { // TODO(zaklaus): not the best approach to do a cleanup, let memory stay for a while as it might be reused later on anyway. -#if 0 - for (int i = 0; i < zpl_buffer_count(world_viewers); i += 1){ - entity_view_tbl *view = &world_viewers[i].entities; - - for (int j = 0; j < zpl_array_count(view->entries); j += 1){ - entity_view *e = &view->entries[j].value; - if (e->tran_effect == ETRAN_REMOVE) { - entity_view_tbl_remove(view, e->ent_id); - } - } - +#if 1 + profile(PROF_ENTITY_REMOVAL) { + static uint64_t last_removal_time = 0; + if (last_removal_time > zpl_time_rel_ms()) return; + last_removal_time = zpl_time_rel_ms() + GAME_ENT_REMOVAL_TIME; + + for (int i = 0; i < zpl_buffer_count(world_viewers); i += 1){ + entity_view_tbl *view = &world_viewers[i].entities; + uint32_t deletions = 0; + + for (int j = 0; j < zpl_array_count(view->entries); j += 1) { + if (deletions > GAME_ENT_REMOVAL_TRESHOLD) return; + + entity_view *e = &view->entries[j].value; + if (e->tran_effect == ETRAN_REMOVE) { + entity_view_tbl_remove(view, e->ent_id); + deletions++; + } + } + } } #endif } diff --git a/code/game/source/profiler.c b/code/game/source/profiler.c index 580264b..53b3d71 100644 --- a/code/game/source/profiler.c +++ b/code/game/source/profiler.c @@ -9,7 +9,9 @@ static profiler profilers[] = { { .id = PROF_MAIN_LOOP, .name = "measured time" }, { .id = PROF_WORLD_WRITE, .name = "world write" }, { .id = PROF_RENDER, .name = "render" }, + { .id = PROF_UPDATE_SYSTEMS, .name = "update systems" }, { .id = PROF_ENTITY_LERP, .name = "entity lerp" }, + { .id = PROF_ENTITY_REMOVAL, .name = "entity removal" }, }; static_assert((sizeof(profilers)/sizeof(profilers[0])) == MAX_PROF, "mismatched profilers"); diff --git a/code/game/source/world/world.c b/code/game/source/world/world.c index 4c743cc..cfcaff7 100644 --- a/code/game/source/world/world.c +++ b/code/game/source/world/world.c @@ -202,7 +202,9 @@ static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius) int32_t world_update() { - ecs_progress(world.ecs, 0.0f); + profile (PROF_UPDATE_SYSTEMS) { + ecs_progress(world.ecs, 0.0f); + } world_tracker_update(0, WORLD_TRACKER_UPDATE_FAST_MS, 2); world_tracker_update(1, WORLD_TRACKER_UPDATE_NORMAL_MS, 4);