]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
added a small simple optimization of prog->knownstring add/remove
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 3 Jun 2005 15:43:21 +0000 (15:43 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 3 Jun 2005 15:43:21 +0000 (15:43 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@5392 d7cf8633-e32d-0410-b094-e92efae38249

progsvm.h
prvm_edict.c

index ab85fe9780c91f5517064fb63be501112bb23df3..18768887c00b9051a4f677e522b82d2cd8478605 100644 (file)
--- 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)
index 81822b9faf40c3442346098b661d025d4cfbdd18..29ee0c1cdaf9e1d29255e315837d0954e71462a3 100644 (file)
@@ -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);