From 1da376e7775a3490ee674f705a5b040d2e1e9187 Mon Sep 17 00:00:00 2001 From: Sally Coolatta Date: Sat, 12 Oct 2024 23:02:31 -0400 Subject: [PATCH] Create doomednum lookup for loading mobjthinkers Cuts off 10% of the time it takes to load mobj thinkers. --- src/p_saveg.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/p_saveg.c b/src/p_saveg.c index b95e1a09e..920bda875 100644 --- a/src/p_saveg.c +++ b/src/p_saveg.c @@ -1944,6 +1944,21 @@ FUNCINLINE static ATTRINLINE mobj_t *AllocMobj(void) return mobj; } +static mobjtype_t g_doomednum_to_mobjtype[UINT16_MAX]; + +static void CalculateDoomednumToMobjtype(void) +{ + memset(g_doomednum_to_mobjtype, MT_NULL, sizeof(g_doomednum_to_mobjtype)); + + for (size_t i = MT_NULL+1; i < NUMMOBJTYPES; i++) + { + if (mobjinfo[i].doomednum > 0 && mobjinfo[i].doomednum <= UINT16_MAX) + { + g_doomednum_to_mobjtype[ mobjinfo[i].doomednum ] = i; + } + } +} + static void LoadMobjThinker(savebuffer_t *save, actionf_p1 thinker) { thinker_t *next; @@ -1993,18 +2008,23 @@ static void LoadMobjThinker(savebuffer_t *save, actionf_p1 thinker) mobj->type = READUINT32(save->p); else { - for (i = 0; i < NUMMOBJTYPES; i++) - if (mobj->spawnpoint && mobj->spawnpoint->type == mobjinfo[i].doomednum) - break; - if (i == NUMMOBJTYPES) + mobjtype_t new_type = MT_NULL; + if (mobj->spawnpoint) + { + new_type = g_doomednum_to_mobjtype[mobj->spawnpoint->type]; + } + + if (new_type <= MT_NULL || new_type >= NUMMOBJTYPES) { if (mobj->spawnpoint) - CONS_Alert(CONS_ERROR, "Found mobj with unknown map thing type %d\n", mobj->spawnpoint->type); + CONS_Alert(CONS_ERROR, "Found mobj with unknown map thing doomednum %d\n", mobj->spawnpoint->type); else - CONS_Alert(CONS_ERROR, "Found mobj with unknown map thing type NULL\n"); + CONS_Alert(CONS_ERROR, "Found mobj with unknown map thing doomednum NULL\n"); + I_Error("Savegame corrupted"); } - mobj->type = i; + + mobj->type = new_type; } mobj->info = &mobjinfo[mobj->type]; @@ -2793,6 +2813,10 @@ static void P_NetUnArchiveThinkers(savebuffer_t *save) if (READUINT32(save->p) != ARCHIVEBLOCK_THINKERS) I_Error("Bad $$$.sav at archive block Thinkers"); + // Pre-calculate this lookup, because it was wasting + // a shit ton of time loading mobj thinkers. + CalculateDoomednumToMobjtype(); + // remove all the current thinkers currentthinker = thinkercap.next; for (currentthinker = thinkercap.next; currentthinker != &thinkercap; currentthinker = next)