From fc6eff65c2ca0df40b5c3cfa5e20910f2cb59baf Mon Sep 17 00:00:00 2001 From: toaster Date: Tue, 13 Dec 2022 16:15:31 +0000 Subject: [PATCH] Cache the result of M_UnlockableSkinNum/FollowerNum/MapNum/Cup Improves performance on mapheader iteration for M_Map/CupLocked significantly. --- src/deh_soc.c | 2 ++ src/k_menudraw.c | 17 ++++------ src/m_cond.c | 85 ++++++++++++++++++++++++++++++++++++++---------- src/m_cond.h | 2 ++ 4 files changed, 78 insertions(+), 28 deletions(-) diff --git a/src/deh_soc.c b/src/deh_soc.c index ce7ac6515..f49e1df20 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -2278,11 +2278,13 @@ void readunlockable(MYFILE *f, INT32 num) unlockables[num].type = SECRET_ITEMFINDER; else unlockables[num].type = (INT16)i; + unlockables[num].stringVarCache = -1; } else if (fastcmp(word, "VAR")) { Z_Free(unlockables[num].stringVar); unlockables[num].stringVar = Z_StrDup(word2); + unlockables[num].stringVarCache = -1; unlockables[num].variable = (INT16)i; } else if (fastcmp(word, "ICON")) diff --git a/src/k_menudraw.c b/src/k_menudraw.c index 3f7ded054..8f2cb4a34 100644 --- a/src/k_menudraw.c +++ b/src/k_menudraw.c @@ -4650,16 +4650,13 @@ static void M_DrawChallengePreview(INT32 x, INT32 y) } case SECRET_MAP: { - if (ref->stringVar && ref->stringVar[0]) - { - UINT16 mapnum = G_MapNumber(ref->stringVar); - K_DrawMapThumbnail( - (x-30)<stringVar && unlock->stringVar[0]) { + INT32 skinnum; + + if (unlock->stringVarCache != -1) + { + return unlock->stringVarCache; + } + // Get the skin from the string. - INT32 skinnum = R_SkinAvailable(unlock->stringVar); + skinnum = R_SkinAvailable(unlock->stringVar); if (skinnum != -1) { + unlock->stringVarCache = skinnum; return skinnum; } } @@ -1172,10 +1178,18 @@ INT32 M_UnlockableFollowerNum(unlockable_t *unlock) if (unlock->stringVar && unlock->stringVar[0]) { + INT32 skinnum; + + if (unlock->stringVarCache != -1) + { + return unlock->stringVarCache; + } + // Get the skin from the string. - INT32 skinnum = K_FollowerAvailable(unlock->stringVar); + skinnum = K_FollowerAvailable(unlock->stringVar); if (skinnum != -1) { + unlock->stringVarCache = skinnum; return skinnum; } } @@ -1193,6 +1207,7 @@ INT32 M_UnlockableFollowerNum(unlockable_t *unlock) cupheader_t *M_UnlockableCup(unlockable_t *unlock) { cupheader_t *cup = kartcupheaders; + INT16 val = unlock->variable-1; if (unlock->type != SECRET_CUP) { @@ -1202,28 +1217,62 @@ cupheader_t *M_UnlockableCup(unlockable_t *unlock) if (unlock->stringVar && unlock->stringVar[0]) { - // Get the cup from the string. - while (cup) + if (unlock->stringVarCache == -1) { - if (!strcmp(cup->name, unlock->stringVar)) - break; - cup = cup->next; + // Get the cup from the string. + while (cup) + { + if (!strcmp(cup->name, unlock->stringVar)) + break; + cup = cup->next; + } + + if (cup) + { + unlock->stringVarCache = cup->id; + } + return cup; } + + val = unlock->stringVarCache; } - else + else if (val == -1) { - // Use the number directly. - while (cup) - { - if (cup->id == unlock->variable) - break; - cup = cup->next; - } + return NULL; + } + + // Use the number directly. + while (cup) + { + if (cup->id == val) + break; + cup = cup->next; } return cup; } +INT16 M_UnlockableMapNum(unlockable_t *unlock) +{ + if (unlock->type != SECRET_MAP) + { + // This isn't a map unlockable... + return NEXTMAP_INVALID; + } + + if (unlock->stringVar && unlock->stringVar[0]) + { + if (unlock->stringVarCache == -1) + { + unlock->stringVarCache = G_MapNumber(unlock->stringVar); + } + + return unlock->stringVarCache; + } + + return NEXTMAP_INVALID; +} + // ---------------- // Misc Emblem shit // ---------------- diff --git a/src/m_cond.h b/src/m_cond.h index fd8037e2f..7dca580da 100644 --- a/src/m_cond.h +++ b/src/m_cond.h @@ -89,6 +89,7 @@ struct unlockable_t INT16 type; INT16 variable; char *stringVar; + INT16 stringVarCache; UINT8 majorunlock; }; @@ -225,6 +226,7 @@ UINT8 M_GotLowEnoughTime(INT32 tictime); INT32 M_UnlockableSkinNum(unlockable_t *unlock); INT32 M_UnlockableFollowerNum(unlockable_t *unlock); cupheader_t *M_UnlockableCup(unlockable_t *unlock); +INT16 M_UnlockableMapNum(unlockable_t *unlock); INT32 M_EmblemSkinNum(emblem_t *emblem);