From a953951712448641f8f9263add258b6a0fa58dbc Mon Sep 17 00:00:00 2001 From: havoc Date: Fri, 3 Jun 2005 15:43:21 +0000 Subject: [PATCH] added a small simple optimization of prog->knownstring add/remove git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5392 d7cf8633-e32d-0410-b094-e92efae38249 --- progsvm.h | 3 +++ prvm_edict.c | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/progsvm.h b/progsvm.h index ab85fe97..18768887 100644 --- a/progsvm.h +++ b/progsvm.h @@ -268,6 +268,9 @@ typedef struct prvm_prog_s int maxknownstrings; int numknownstrings; + // this is updated whenever a string is removed or added + // (simple optimization of the free string search) + int firstfreeknownstring; const char **knownstrings; // all memory allocations related to this vm_prog (code, edicts, strings) diff --git a/prvm_edict.c b/prvm_edict.c index 81822b9f..29ee0c1c 100644 --- a/prvm_edict.c +++ b/prvm_edict.c @@ -1825,7 +1825,7 @@ int PRVM_SetEngineString(const char *s) // new unknown engine string if (developer.integer >= 3) Con_Printf("new engine string %p\n", s); - for (i = 0;i < prog->numknownstrings;i++) + for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++) if (!prog->knownstrings[i]) break; if (i >= prog->numknownstrings) @@ -1840,6 +1840,7 @@ int PRVM_SetEngineString(const char *s) } prog->numknownstrings++; } + prog->firstfreeknownstring = i + 1; prog->knownstrings[i] = s; return -1 - i; } @@ -1849,7 +1850,7 @@ int PRVM_AllocString(int bufferlength, char **pointer) int i; if (!bufferlength) return 0; - for (i = 0;i < prog->numknownstrings;i++) + for (i = prog->firstfreeknownstring;i < prog->numknownstrings;i++) if (!prog->knownstrings[i]) break; if (i >= prog->numknownstrings) @@ -1864,6 +1865,7 @@ int PRVM_AllocString(int bufferlength, char **pointer) } prog->numknownstrings++; } + prog->firstfreeknownstring = i + 1; prog->knownstrings[i] = PRVM_Alloc(bufferlength); if (pointer) *pointer = (char *)(prog->knownstrings[i]); @@ -1883,6 +1885,7 @@ void PRVM_FreeString(int num) Host_Error("PRVM_FreeString: attempt to free a non-existent or already freed string\n"); PRVM_Free((char *)prog->knownstrings[num]); prog->knownstrings[num] = NULL; + prog->firstfreeknownstring = min(prog->firstfreeknownstring, num); } else Host_Error("PRVM_FreeString: invalid string offset %i\n", num); -- 2.39.2