Merge branch 'hanicef-mobj-cache' into 'master'

Cache and reuse removed mobjs when spawning mobjs

See merge request KartKrew/Kart!1607
This commit is contained in:
Oni 2023-11-09 12:29:53 +00:00
commit 6686215330
5 changed files with 29 additions and 3 deletions

View file

@ -55,6 +55,7 @@ struct thinker_t
// killough 11/98: count of how many other objects reference // killough 11/98: count of how many other objects reference
// this one using pointers. Used for garbage collection. // this one using pointers. Used for garbage collection.
INT32 references; INT32 references;
boolean cachable;
#ifdef PARANOIA #ifdef PARANOIA
INT32 debug_mobjtype; INT32 debug_mobjtype;

View file

@ -77,6 +77,7 @@ typedef enum
NUM_THINKERLISTS NUM_THINKERLISTS
} thinklistnum_t; /**< Thinker lists. */ } thinklistnum_t; /**< Thinker lists. */
extern thinker_t thlist[]; extern thinker_t thlist[];
extern mobj_t *mobjcache;
void P_InitThinkers(void); void P_InitThinkers(void);
void P_AddThinker(const thinklistnum_t n, thinker_t *thinker); void P_AddThinker(const thinklistnum_t n, thinker_t *thinker);

View file

@ -63,6 +63,8 @@ mobj_t *waypointcap = NULL;
// general purpose. // general purpose.
mobj_t *trackercap = NULL; mobj_t *trackercap = NULL;
mobj_t *mobjcache = NULL;
void P_InitCachedActions(void) void P_InitCachedActions(void)
{ {
actioncachehead.prev = actioncachehead.next = &actioncachehead; actioncachehead.prev = actioncachehead.next = &actioncachehead;
@ -10951,7 +10953,16 @@ mobj_t *P_SpawnMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type)
type = MT_RAY; type = MT_RAY;
} }
if (mobjcache != NULL)
{
mobj = mobjcache;
mobjcache = mobjcache->hnext;
memset(mobj, 0, sizeof(*mobj));
}
else
{
mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL);
}
// this is officially a mobj, declared as soon as possible. // this is officially a mobj, declared as soon as possible.
mobj->thinker.function.acp1 = (actionf_p1)P_MobjThinker; mobj->thinker.function.acp1 = (actionf_p1)P_MobjThinker;
@ -11859,7 +11870,9 @@ void P_RemoveMobj(mobj_t *mobj)
INT32 prevreferences; INT32 prevreferences;
if (!mobj->thinker.references) if (!mobj->thinker.references)
{ {
Z_Free(mobj); // No refrrences? Can be removed immediately! :D // no references, dump it directly in the mobj cache
mobj->hnext = mobjcache;
mobjcache = mobj;
return; return;
} }

View file

@ -8363,6 +8363,7 @@ boolean P_LoadLevel(boolean fromnetsave, boolean reloadinggamestate)
Patch_FreeTag(PU_PATCH_LOWPRIORITY); Patch_FreeTag(PU_PATCH_LOWPRIORITY);
Patch_FreeTag(PU_PATCH_ROTATED); Patch_FreeTag(PU_PATCH_ROTATED);
Z_FreeTags(PU_LEVEL, PU_PURGELEVEL - 1); Z_FreeTags(PU_LEVEL, PU_PURGELEVEL - 1);
mobjcache = NULL;
R_InitializeLevelInterpolators(); R_InitializeLevelInterpolators();

View file

@ -310,6 +310,7 @@ void P_AddThinker(const thinklistnum_t n, thinker_t *thinker)
thlist[n].prev = thinker; thlist[n].prev = thinker;
thinker->references = 0; // killough 11/98: init reference counter to 0 thinker->references = 0; // killough 11/98: init reference counter to 0
thinker->cachable = n == THINK_MOBJ;
#ifdef PARANOIA #ifdef PARANOIA
thinker->debug_mobjtype = MT_NULL; thinker->debug_mobjtype = MT_NULL;
@ -427,7 +428,16 @@ void P_UnlinkThinker(thinker_t *thinker)
I_Assert(thinker->references == 0); I_Assert(thinker->references == 0);
(next->prev = thinker->prev)->next = next; (next->prev = thinker->prev)->next = next;
if (thinker->cachable)
{
// put cachable thinkers in the mobj cache, so we can avoid allocations
((mobj_t *)thinker)->hnext = mobjcache;
mobjcache = (mobj_t *)thinker;
}
else
{
Z_Free(thinker); Z_Free(thinker);
}
} }
// //