From: havoc Date: Sat, 11 Mar 2006 19:01:54 +0000 (+0000) Subject: use hash lookups in Cvar_FindVar, this gives a ~40% increase in fps with high numbers... X-Git-Tag: xonotic-v0.1.0preview~4211 X-Git-Url: http://git.xonotic.org/?a=commitdiff_plain;h=d7806f79c4f18721e664022ea8a852b7e90c5ca8;p=xonotic%2Fdarkplaces.git use hash lookups in Cvar_FindVar, this gives a ~40% increase in fps with high numbers of bots in nexuiz git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@6107 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/cvar.c b/cvar.c index f3b9e70e..b5fdf2b7 100644 --- a/cvar.c +++ b/cvar.c @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" cvar_t *cvar_vars = NULL; +cvar_t *cvar_hashtable[65536]; char *cvar_null_string = ""; /* @@ -31,9 +32,12 @@ Cvar_FindVar */ cvar_t *Cvar_FindVar (const char *var_name) { + int hashindex; cvar_t *var; - for (var = cvar_vars;var;var = var->next) + // use hash lookup to minimize search time + hashindex = CRC_Block((const unsigned char *)var_name, strlen(var_name)); + for (var = cvar_hashtable[hashindex];var;var = var->nextonhashchain) if (!strcasecmp (var_name, var->name)) return var; @@ -312,6 +316,7 @@ Adds a freestanding variable to the variable list. */ void Cvar_RegisterVariable (cvar_t *variable) { + int hashindex; cvar_t *current, *next, *cvar; char *oldstr; @@ -387,6 +392,11 @@ void Cvar_RegisterVariable (cvar_t *variable) cvar_vars = variable; } variable->next = next; + + // link to head of list in this hash table index + hashindex = CRC_Block((const unsigned char *)variable->name, strlen(variable->name)); + variable->nextonhashchain = cvar_hashtable[hashindex]; + cvar_hashtable[hashindex] = variable; } /* @@ -398,7 +408,8 @@ Adds a newly allocated variable to the variable list or sets its value. */ cvar_t *Cvar_Get (const char *name, const char *value, int flags) { - cvar_t *cvar; + int hashindex; + cvar_t *current, *next, *cvar; if (developer.integer) Con_Printf("Cvar_Get(\"%s\", \"%s\", %i);\n", name, value, flags); @@ -443,8 +454,20 @@ cvar_t *Cvar_Get (const char *name, const char *value, int flags) cvar->description = "custom cvar"; // link the variable in - cvar->next = cvar_vars; - cvar_vars = cvar; +// alphanumerical order + for( current = NULL, next = cvar_vars ; next && strcmp( next->name, cvar->name ) < 0 ; current = next, next = next->next ) + ; + if( current ) + current->next = cvar; + else + cvar_vars = cvar; + cvar->next = next; + + // link to head of list in this hash table index + hashindex = CRC_Block((const unsigned char *)cvar->name, strlen(cvar->name)); + cvar->nextonhashchain = cvar_hashtable[hashindex]; + cvar_hashtable[hashindex] = cvar; + return cvar; } diff --git a/cvar.h b/cvar.h index d2b57158..de379f8f 100644 --- a/cvar.h +++ b/cvar.h @@ -120,6 +120,7 @@ typedef struct cvar_s //menucvar_t menuinfo; struct cvar_s *next; + struct cvar_s *nextonhashchain; } cvar_t; /*