diff --git a/src/d_think.h b/src/d_think.h index b907c17fd..6b8ee113a 100644 --- a/src/d_think.h +++ b/src/d_think.h @@ -49,6 +49,7 @@ typedef struct thinker_s // killough 11/98: count of how many other objects reference // this one using pointers. Used for garbage collection. INT32 references; + boolean cachable; } thinker_t; #endif diff --git a/src/p_local.h b/src/p_local.h index bb7c62046..6b69da480 100644 --- a/src/p_local.h +++ b/src/p_local.h @@ -66,6 +66,7 @@ // both the head and tail of the thinker list extern thinker_t thinkercap; +extern mobj_t *mobjcache; void P_InitThinkers(void); void P_AddThinker(thinker_t *thinker); diff --git a/src/p_mobj.c b/src/p_mobj.c index 12653d39d..b7746a6ac 100644 --- a/src/p_mobj.c +++ b/src/p_mobj.c @@ -48,6 +48,7 @@ actioncache_t actioncachehead; static mobj_t *overlaycap = NULL; static mobj_t *shadowcap = NULL; mobj_t *waypointcap = NULL; +mobj_t *mobjcache = NULL; void P_InitCachedActions(void) { @@ -9510,7 +9511,18 @@ mobj_t *P_SpawnMobj(fixed_t x, fixed_t y, fixed_t z, mobjtype_t type) { const mobjinfo_t *info = &mobjinfo[type]; state_t *st; - mobj_t *mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); + mobj_t *mobj; + + if (mobjcache != NULL) + { + mobj = mobjcache; + mobjcache = mobjcache->hnext; + memset(mobj, 0, sizeof(*mobj)); + } + else + { + mobj = Z_Calloc(sizeof (*mobj), PU_LEVEL, NULL); + } // this is officially a mobj, declared as soon as possible. mobj->thinker.function.acp1 = (actionf_p1)P_MobjThinker; @@ -10255,7 +10267,9 @@ void P_RemoveMobj(mobj_t *mobj) // Invalidate mobj_t data to cause crashes if accessed! memset(mobj, 0xff, sizeof(mobj_t)); #endif - Z_Free(mobj); // No refrences? Can be removed immediately! :D + // no references, dump it directly in the mobj cache + mobj->hnext = mobjcache; + mobjcache = mobj; } else { // Add thinker just to delay removing it until refrences are gone. diff --git a/src/p_setup.c b/src/p_setup.c index d8dcbb877..951c24933 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -2995,6 +2995,8 @@ boolean P_SetupLevel(boolean skipprecip) R_FlushTranslationColormapCache(); Z_FreeTags(PU_LEVEL, PU_PURGELEVEL - 1); + + mobjcache = NULL; #if defined (WALLSPLATS) || defined (FLOORSPLATS) // clear the splats from previous level diff --git a/src/p_tick.c b/src/p_tick.c index 38900db9f..a70815b86 100644 --- a/src/p_tick.c +++ b/src/p_tick.c @@ -201,6 +201,7 @@ void P_AddThinker(thinker_t *thinker) thinkercap.prev = thinker; thinker->references = 0; // killough 11/98: init reference counter to 0 + thinker->cachable = thinker->function.acp1 == (actionf_p1)P_MobjThinker; } // @@ -236,7 +237,16 @@ void P_RemoveThinkerDelayed(void *pthinker) (next->prev = currentthinker = thinker->prev)->next = next; } R_DestroyLevelInterpolators(thinker); - Z_Free(thinker); + 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); + } } }