From 5133c6c9ce67e838d8defb026dcbb4e5b0ca8c0b Mon Sep 17 00:00:00 2001
From: John FrostFox <john.frostfox@gmail.com>
Date: Tue, 2 Nov 2021 20:45:42 +0300
Subject: [PATCH] LUA savestate loading optimization with hashtables

---
 src/hashtable.c   | 3 ---
 src/lua_script.c  | 3 ++-
 src/p_savenetrb.h | 1 +
 3 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/hashtable.c b/src/hashtable.c
index cb086d384..9a3f97f9b 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -69,16 +69,13 @@ void mobjnum_ht_linkedList_AddEntry (thinker_t* thinker)
     {
         if (!currentEntry->thinker)
         {
-            // CONS_Printf("mobj not exists, adding\n");
             currentEntry->thinker = thinker;
             break;
         }
         else
         {
-            // CONS_Printf("mobj exists, go next\n");
             if (!currentEntry->next)
             {
-                // CONS_Printf("next not exists, create\n");
                 currentEntry->next = malloc(sizeof(mobjnum_linkedList));
                 currentEntry->next->thinker = thinker;
                 currentEntry->next->next = NULL;
diff --git a/src/lua_script.c b/src/lua_script.c
index 86adc36e5..819ff696e 100644
--- a/src/lua_script.c
+++ b/src/lua_script.c
@@ -36,6 +36,7 @@
 
 #include "doomstat.h"
 #include "g_state.h"
+#include "p_savenetrb.h" //for P_FindNewPosition_Hashtable
 #include "hashtable.h"
 
 lua_State *gL = NULL;
@@ -1455,7 +1456,7 @@ static UINT8 UnArchiveValue(int TABLESINDEX)
 		LUA_PushUserdata(gL, &states[READUINT16(save_p)], META_STATE);
 		break;
 	case ARCH_MOBJ:
-		LUA_PushUserdata(gL, P_FindNewPosition(READUINT32(save_p)), META_MOBJ);
+		LUA_PushUserdata(gL, P_FindNewPosition_Hashtable(READUINT32(save_p)), META_MOBJ);
 		break;
 	case ARCH_PLAYER:
 		LUA_PushUserdata(gL, &players[READUINT8(save_p)], META_PLAYER);
diff --git a/src/p_savenetrb.h b/src/p_savenetrb.h
index 27dc01c1c..a67026d15 100755
--- a/src/p_savenetrb.h
+++ b/src/p_savenetrb.h
@@ -24,5 +24,6 @@ extern char netDebugText[10000];
 // TODO: Find GPLv2 hashtable library and optimize the lua object loading code
 void P_GameStateFreeMemory(savestate_t* savestate);
 void P_SaveGameState(savestate_t* savestate);
+mobj_t *P_FindNewPosition_Hashtable(UINT32 oldposition);
 
 boolean P_LoadGameState(const savestate_t* savestate);
\ No newline at end of file
-- 
GitLab